当前位置:网站首页>[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】
List of articles
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 :
- Seeking closure ( Combination of elements on the left )
- 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
- Main attribute : The attributes that make up the candidate code are the primary attributes
- Non primary property : Either the primary attribute or the non primary attribute
- 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 :
Relationship model :
The theory and examples of the following screenshots are from : How to put ER Convert model to relational schema
theory :
Example :
Explain :
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
边栏推荐
猜你喜欢
随机推荐
mysql中的delete,drop和truncate有什么区别
2022.5.29-----leetcode.468
记录下bilibili(b站)小火箭页面上划动画效果的实现
原型链?新的歪理解
[leetcode weekly race record] record of the 296th weekly race
面试题 08.02. 迷路的机器人
An intranet test for an Express Hotel
【计网】思科 期末选择题复习
网络原理-TCP
ARIMA加法季节模型
Yunna administrative unit fixed assets management system, unit fixed assets management measures
记忆化搜索+状态压缩leetcode.464
15 Uncaught TypeError: Cannot set properties of null (setting ‘onclick‘)
小众专业如何解决“成长”的烦恼?
2022.6.1-----leetcode.473
Uniswapv2 peripheral contract learning (IX) -- examplecombinedswapadddremoveliquidity sol
发邮件:错排问题的分析
Digital transformation: how to gain organizational recognition?
面试题 08.07. 无重复字符串的排列组合
浅谈RedisTemplate和StringRedisTemplate的区别








