当前位置:网站首页>[database] final review: SQL statement, definition and judgment of normal form, ER diagram to relational mode

[database] final review: SQL statement, definition and judgment of normal form, ER diagram to relational mode

2022-06-09 14:03:00 karshey

SQL sentence

Inquire about select

Student list s, The curriculum c, Student selection table sc.

Query the student ( The student number is A) There are several elective courses ?

select count(cno) from sc
where sno='A'

The age of inquiry is 18-19 Student information between .

select * from s
where sage between 18 and 19

Query the information of students surnamed Liu .

select * from s
where sname like ' Liu %'

If the name is two characters and the surname is Liu , Then for ’ Liu _'.

Query the course information without prerequisite courses .

select * from c
where cpno is null

Ask the average score of students in each course , Display the course number and the corresponding average grade

select cno,avg(grade) from sc
group by cno

There are 2 The course number of the course selected by more than students .

select cno from sc
group by cno
having count(sno)>2

group by You can't where want having

Inquire about the requirements of students' elective courses , Show student name 、 Course name 、 achievement ( Three table query ).

select sname,cname,grade from s,c,sc
where s.sno=sc.sno and c.cno=sc.cno

Check the names of the students who have taken all the courses

select sname from s// Here are all the selected courses 
where not exists
(
	// There is no elective course here 
	select * from c
	where not exists
	(
		// Here is the selected class 
		select * from sc
		where s.sno=sc.sno and c.cno=sc.cno
	)
)

other

Create table dept_age, contain 2 A field : Department and average age . Average age by department , Then put the Department name and average age into the new table .

create table dept_age
(
	dept char(20) primary key,/* It's the main code */
	avg-age smallint
)

If it is a foreign code , The reference is A In the table dept, So be it :dept char(20) references A(dept)

Delete S surface .

drop table s

Subtract... From the age of the girl 1 year .

update sage     /* Table name */
set sage=sage-1  /* Pair attribute sage The operation of */
where ssex=' Woman '

Set the grades of all students in the Department of information science as 100 branch .

update sc
set grade=100
where sno in
(
	select sno from s
	where dept=' information science '
)

Delete 200215121 Students' course selection information .

delete from sc
where sno='200215121'

Delete the student information of the computer science department in the transcript .

delete from sc
where sno in
(
	select sno from s
	where dept=' Computer science '
)

Create view view grade, The content is : The student's name 、 Course name 、 achievement .

create view as select 
sname,cname,grade
from s,c,sc
where s.sno=sc.sno and c.cno=sc.cno

Definition and judgment of paradigm

step

  1. Seeking closure ( Combination of elements on the left )
  2. Candidate code : When the combined closure of the elements on the left of the previous step can push out all the elements , It is the candidate code
  3. Main attribute : The attributes that make up the candidate code are the primary attributes
  4. Non primary property : Either the primary attribute or the non primary attribute
  5. Determine which paradigm :NF,2NF,3NF,BCNF

NF: Every element is indivisible .
Such as : Student ( full name , Gender , Members of the family )—— The name and gender are inseparable ( Such as gender : Either male or female ), But family members are multi-element mixed( Parents, sisters and brothers ), therefore “ Student ” No NF.

1NF VS 2NF——2NF: There is no partial dependency of non primary attributes on candidate codes .
translate : Select a part of the candidate code , It can deduce non primary attributes .
Such as :
There are candidate codes BC, Non primary property D. If you have any B->D or C->D, So this is “ Partial dependence of non primary attributes on candidate codes ”, So it is not satisfied 2NF, Can only be NF.

2NF VS 3NF——3NF: There is no transfer dependency of non primary attributes on candidate codes .
Such as :
There are candidate codes AB, And AB->C,C->D, So in fact AB->D, namely D Passing depends on candidate code AB, Then it is not satisfied 3NF, At most 2NF.

3NF VS BCNF——BCNF: There is no partial or transitive dependency of the primary attribute on the candidate code

Some examples

Example 1
R(A,B,C),F={AB->C}.

Explain :
Candidate code :AB.
Main attribute :A、B.
Non primary property :C.

yes NF,2NF( non-existent A->C or B->C),3NF( There is no transfer dependency of non primary attributes on candidate codes ),BCNF( There is no partial or transitive dependency of the primary attribute on the candidate code ).

So it's a BCNF.

Example 2:
R(A,B,C),F={B->C,AC->B}.

Explain :
Candidate code :AB、AC.
Main attribute :A、B、C
Non primary property : nothing .( So it's at least one 3NF)

about B->C,C It's the main attribute ,B Is part of the candidate code , There is a partial dependency of the primary attribute on the candidate code , So it's not BCNF.

So it's a 3NF.

Example 3:
R(A,B,C),F={B->C,B->A,A->BC}.

Candidate code :A、B
Main attribute :A、B
Non primary property :C

about B->C, This is a direct dependency ( Because the candidate code is B, If the candidate code is BD, be B yes BD Part of , That is partial dependence ), So it is 2NF.
There is no transfer dependency of non primary attributes on candidate codes , yes 3NF.
There is no part of the primary attribute pair candidate code / Transitive dependency , yes BCNF.

So it is BCNF.
Be careful :
A->B->A Not transitive dependencies .
To have partial dependencies , Then the candidate code cannot be a unit .( The candidate code of this question is the unit )

Example 4:
R(A,B,C),F={A->C,A->B}.

Explain :
Candidate code :A.
Main attribute :A.
Non primary property :B、C.

answer :BCNF.

Tips :
When a relationship is a binary relationship group , Then it is a BCNF.

For this question ,F={A->C,A->B} It can be synthesized into A->BC, This is a binary relation group .

Example 5:
R(A,B,C,D),F={A->C,AD->B}.

Explain :
Candidate code :AD
Main attribute :A、D
Non primary property :B、C

about A->C, It is the partial dependence of non primary attributes on candidate codes , Therefore, it does not conform to 2NF.
yes NF.

Example 6:
R(A,B,C),F={A->C,BC->D}.

Explain :

How to find candidate codes can be seen here : First step , Combination of elements on the left
Candidate code :AB
Main attribute :A、B
Non primary property :C、D

about A->C, It is the partial dependence of non primary attributes on candidate codes , Not satisfied 2NF, It is NF.

ER Graph to relational model

Underline ( A straight line ) It's the main code , The wavy line is the outer code .

Yes 1:1、1:n、n:m Three corresponding relationships .
1:1
Select one attribute and add another main code .

1:n
stay n End plus 1 The main code of the terminal —— This is a n External code of terminal .
If the relationship ( triangle ) There are also attributes on , Add it to n End attribute .

n:m
The relationship is transformed into an entity , The attribute is the main code at both ends , Together, they are the primary code of the entity . Underline + Wavy lines .

Examples in the video :
 Insert picture description here
Relationship model :
 Insert picture description here

The theory and examples of the following screenshots are from : How to put ER Convert model to relational schema
theory
 Insert picture description here
Example
 Insert picture description here
Explain :
 Insert picture description here

Other examples : The case explains how to ER The graph is transformed into a relational model

Reference material

Example query statement
Examples of other sentences
Definition and judgment of paradigm
ER Graph to relational model
Principle of database system ------ER The graph is transformed into a relational schema

原网站

版权声明
本文为[karshey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091245103503.html