實驗一:E-R圖和數據庫
E-R圖:
數據庫表:
Drop table if EXISTS `school`;
CREATE table `school`(
`id` int(11) not null auto_increment,
`name` varchar(255) not null,
`location` varchar(255) not null,
PRIMARY key(`id`)
)
Drop table if EXISTS `department`;
CREATE table `department`(
`id` int(11) not null auto_increment,
`name` varchar(255) not null,
`description` varchar(255) not null,
`schoolid` int(11) not null,
PRIMARY key(`id`)
)
Drop table if EXISTS `class`;
CREATE table `class`(
`id` int(11) not null auto_increment,
`name` varchar(255) not null,
`monitor` varchar(255) not null,
`departmentid` int(11) not null,
PRIMARY key(`id`)
)
Drop table if EXISTS `student`;
CREATE table `student`(
`id` int(11) not null auto_increment,
`name` varchar(255) not null,
`birthplace` varchar(255) not null,
`classid` int(11) not null,
PRIMARY key(`id`)
)
Drop table if EXISTS `course`;
CREATE table `course`(
`id` int(11) not null auto_increment,
`name` varchar(255) not null,
`course_teacher` varchar(255) not null,
`studentid` int(11) not null,
PRIMARY key(`id`)
)
Drop table if EXISTS `lab`;
CREATE table `lab`(
`id` int(11) not null auto_increment,
`name` varchar(255) not null,
`departmentid` int(11) not null,
PRIMARY key(`id`)
)
Drop table if EXISTS `teacher`;
CREATE table `teacher`(
`id` int(11) not null auto_increment,
`name` varchar(255) not null,
`jobtitle` VARCHAR(255) not null,
`labid` int(11) not null,
PRIMARY key(`id`)
)
Drop table if EXISTS `graduate_student`;
CREATE table `graduate_student`(
`id` int(11) not null auto_increment,
`name` varchar(255) not null,
`birthplace` varchar(255) not null,
`teacherid` int(11) not null,
PRIMARY key(`id`)
)
Drop table if EXISTS `score`;
CREATE table `score`(
`course_id` int(11) not null,
`student_id` int(11) not null,
`num` int(11) not null
)
實驗二:創建數據庫表&增刪改查&可視化操作
增刪該查:
部分可視化操作;
部分SQL語句;
#插入語句
insert into `Student` VALUES('201215123','王敏','女','18','MA');
insert into `Student` VALUES('201210125','張立','男','19','IS');
insert into `Course` VALUES('1','數據庫','5','4');
insert into `Course` VALUES('2','數學',null,'2');
insert into `Course` VALUES('3','信息系統','1','4');
insert into `Course` VALUES('4','操作系統','6','3');
insert into `Course` VALUES('5','數據結構','7','4');
insert into `Course` VALUES('6','數據處理',null,'42');
insert into `Course` VALUES('7','PASCAL語言','6','4');
INSERT into `SC` VALUES('201215121','1','92');
INSERT into `SC` VALUES('201215121','2','85');
INSERT into `SC` VALUES('201215121','3','88');
INSERT into `SC` VALUES('201215122','2','90');
INSERT into `SC` VALUES('201215122','3','80');
#增
insert into `Course` VALUES('8','C++程序設計','2','3');
#更新
UPDATE `Course` set Cname='Java程序設計' where Cno='8';
#刪除
DELETE from Course where Cname='Java程序設計';
#查詢
SELECT * from Student;
SELECT * from course;
SELECT * from SC;
#精確查詢 asc 升序 desc 降序
SELECT * from student where student.Ssex='女' ORDER BY student.Sage asc;
#其他條件查詢
SELECT Sname,Ssex from student where Sno='201215121';
SELECT * from student,sc where student.sno=sc.Sno;
基本sql語句
#test
alter table student add S_entrance DATE;
SELECT * from Student;
#書上例子
#數據查詢
select Sno,Sname from Student;
SELECT Sname,Sno,Sdept from student;
SELECT * from student;
SELECT Sname,2019-Sage from student;
SELECT Sname,'Year of birth: ',2019-Sage,LOWER(Sdept) FROM student;
SELECT DISTINCT Sno from sc;
select Sname,Sage from student where Sage<20;
SELECT DISTINCT Sno from sc WHERE Grade<60;
SELECT Sname,Sdept,Sage from student where Sage BETWEEN 20 and 30;
SELECT Sname,Sdept,Sage from student where Sage not BETWEEN 20 and 30;
SELECT Sname,Ssex from student where Sdept in('CS','MA','IS');
SELECT * from student WHERE Sno like '201215121';
SELECT Sname,Sno,Sage from student where Sname like '劉%';
insert into `Course` VALUES('8','DB_Design','7','4');
SELECT * from course;
##該語句有錯誤
#SELECT Cno,Ccredit from course where Cname LIKE `DB\_Design` ESCAPE `\`;
##SELECT * from course where Cname like 'DB\_%i__' ESCAPE `\`;
SELECT Sno,Cno from sc where Grade is not null;
#ORDER BY
SELECT Sno,Grade from sc where Cno ='3' ORDER BY Grade DESC;
#聚集函數 只能用於select 子句和Group By 的Having 子句
SELECT COUNT(*) from student;
SELECT AVG(Grade) from sc where Cno='1';
SELECT MAX(Grade) from sc where Cno='1';
SELECT sum(Ccredit) from course,sc where Sno='201215012' and sc.Cno=course.Cno;
#GROUP BY 分組 細化聚集函數的作用對象
SELECT Cno,COUNT(Sno) from sc GROUP BY Cno;
#先用 GROUP BY 按 Sno 進行分組, 再用聚集函數count 對每一組計數,
SELECT Sno from sc GROUP BY Sno Having count(*)>1;
#錯誤語句 where 中不能有聚集函數
#SELECT Sno,AVG(Grade) from sc where AVG(Grade)>=90 GROUP BY Sno;
SELECT Sno,AVG(Grade) from sc Group by Sno HAVING AVG(Grade)>=90;
#連接查詢
SELECT student.*,sc.* from student,sc where student.Sno = sc.Sno;
SELECT student.Sno, Sname,Ssex,Sage,Sdept,Cno,Grade from student,sc where student.Sno=sc.Sno;
#去掉 student 報錯
#SELECT Sno, Sname,Ssex,Sage,Sdept,Cno,Grade from student,sc where student.Sno=sc.Sno;
#先修課的先修課
SELECT FIRST.Cno,SECOND.Cpno from Course FIRST,Course SECOND where FIRST.Cpno=SECOND.Cno;
#左外連接
SELECT student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from student left OUTER join sc on(student.Sno = sc.Sno);
SELECT student.Sno,Sname,Cname,Grade from student,sc,course where student.Sno=sc.Sno and sc.Cno = course.Cno;
#各院系選修數據庫的人數
SELECT Sdept,COUNT(student.Sno) from sc,student,course where sc.Cno = course.Cno and student.Sno=sc.Sno and course.Cname='信息系統' GROUP BY Sdept;
實驗三
建立學生選課數據庫,並對每張表錄入實驗數據
--創建表
Drop TABLE if EXISTS `Student`;
CREATE table `Student`(
Sno varchar(255) not null,
Sname varchar(255) not null,
Ssex VARCHAR(255) not null,
Sage int(11) not null,
Sdept varchar(255) not null,
PRIMARY key(Sno)
)
Drop table if exists Course;
create TABLE Course(
Cno varchar(255) PRIMARY key,
Cname varchar(255) not null,
Cpno varchar(255),
Ccredit varchar(255) not null
)
Drop TABLE if EXISTS SC;
CREATE table SC(
Sno varchar(255) not null,
Cno varchar(255) not null,
Grade int(11) not NULL,
PRIMARY KEY(Sno,Cno),
FOREIGN Key(Sno) REFERENCES Student(Sno),
FOREIGN KEY(Cno) REFERENCES Course(Cno)
)
--插入數據
insert into Student VALUES('201215121','李勇','男',20,'CS');
SELECT * from Student;
insert into Student VALUES('201215122','劉晨','女',19,'CS');
insert into Student VALUES('201215123','王敏','女',18,'MA');
insert into Student VALUES('201215125','張力','男',19,'IS');
INSERT into Course values('1','數據庫','5','4');
SELECT * from Course;
insert into `Course` VALUES('2','數學',null,'2');
insert into `Course` VALUES('3','信息系統','1','4');
insert into `Course` VALUES('4','操作系統','6','3');
insert into `Course` VALUES('5','數據結構','7','4');
insert into `Course` VALUES('6','數據處理',null,'42');
update Course set Ccredit = '2' where Cno = '6';
insert into `Course` VALUES('7','PASCAL語言','6','4');
insert into SC VALUES('201215121','1','92');
SELECT * from SC;
INSERT into `SC` VALUES('201215121','2','85');
INSERT into `SC` VALUES('201215121','3','88');
INSERT into `SC` VALUES('201215122','2','90');
INSERT into `SC` VALUES('201215122','3','80');
聯繫各類查詢語句,增加、刪除、修改等語句(選取一些將SQL語句與查詢結果截圖附)
--查詢語句
SELECT Sno,Sname from Student;
SELECT Sname,Sno,Sdept from student;
SELECT * from student;
SELECT Sname,2019-Sage from student;
SELECT Sname,'Year of birth: ',2019-Sage,LOWER(Sdept) FROM student;
SELECT DISTINCT Sno from sc;
select Sname,Sage from student where Sage<20;
SELECT DISTINCT Sno from sc WHERE Grade<60;
SELECT Sname,Sdept,Sage from student where Sage BETWEEN 20 and 30;
SELECT Sname,Sdept,Sage from student where Sage not BETWEEN 20 and 30;
SELECT Sname,Ssex from student where Sdept in('CS','MA','IS');
SELECT * from student WHERE Sno like '201215121';
SELECT Sname,Sno,Sage from student where Sname like '劉%';
insert into `Course` VALUES('8','DB_Design',null,'4');
--語法有錯誤
--SELECT Cno,Ccredit from course where Cname like 'DB\_Design' ESCAPE "\";
SELECT Sno,Cno from sc where Grade is not null;
--ORDER BY
SELECT Sno,Grade from sc where Cno ='3' ORDER BY Grade DESC;
--聚集函數 只能用於select 子句和Group By 的Having 子句
SELECT COUNT(*) from student;
SELECT AVG(Grade) from sc where Cno='1';
SELECT MAX(Grade) from sc where Cno='1';
SELECT sum(Ccredit) from course,sc where Sno='201215122' and sc.Cno=course.Cno;
--GROUP BY 分組 細化聚集函數的作用對象
SELECT Cno,COUNT(Sno) from sc GROUP BY Cno;
--先用 GROUP BY 按 Sno 進行分組, 再用聚集函數count 對每一組計數,
SELECT Sno from sc GROUP BY Sno Having count(*)>1;
--錯誤語句 where 中不能有聚集函數
--SELECT Sno,AVG(Grade) from sc where AVG(Grade)>=90 GROUP BY Sno;
SELECT Sno,AVG(Grade) from sc Group by Sno HAVING AVG(Grade)>=90;
--連接查詢
SELECT student.*,sc.* from student,sc where student.Sno = sc.Sno;
SELECT student.Sno, Sname,Ssex,Sage,Sdept,Cno,Grade from student,sc where student.Sno=sc.Sno;
--去掉 student 報錯
--SELECT Sno, Sname,Ssex,Sage,Sdept,Cno,Grade from student,sc where student.Sno=sc.Sno;
SELECT Student.Sno,Sname from Student,SC where Student.Sno = SC.Sno AND SC.Cno = '2' AND SC.Grade>90;
--先修課的先修課
SELECT FIRST.Cno,SECOND.Cpno from Course FIRST,Course SECOND where FIRST.Cpno=SECOND.Cno;
--左外連接
SELECT student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from student left OUTER join sc on(student.Sno = sc.Sno);
SELECT student.Sno,Sname,Cname,Grade from student,sc,course where student.Sno=sc.Sno and sc.Cno = course.Cno;
--各院系選修數據庫的人數
SELECT Sdept,COUNT(student.Sno) from sc,student,course where sc.Cno = course.Cno and student.Sno=sc.Sno and course.Cname='信息系統' GROUP BY Sdept;
--嵌套查詢 不相關子查詢
select Sno,Sname,Sdept From Student where Sdept IN (SELECT Sdept from student where Sname = '劉晨');
SELECT Sno,Sname from Student where Sno in (
SELECT Sno from SC where Cno in (
SELECT Cno from Course where Cname = '信息系統'
)
);
--相關自查詢
SELECT Sno,Cno from SC x where Grade>=(
SELECT AVG(Grade) from SC y where y.Sno = x.Sno);
--ANY ALL
SELECT Sname,Sage from Student where Sage<ANY (
SELECT Sage From student where Sdept = 'CS') AND Sdept != 'CS';
SELECT Sname,Sage from Student where Sage<ALL (
SELECT Sage From student where Sdept = 'CS') AND Sdept <> 'CS';
--EXISTS
SELECT Sname from student WHERE exists (
SELECT * from SC where Sno = student.Sno and Cno = '1');
SELECT Sname from student WHERE not exists (
SELECT * from SC where Sno = student.Sno and Cno = '1');
--查詢選修了全部課程的學生姓名 #沒有一門課程是他不選修的
SELECT Sname from Student where not exists (
SELECT * from Course where not exists (
SELECT * from SC where Sno = Student.Sno And Cno = Course.Cno));
--查詢至少選修了學生201215122選修的全部課程的學生號碼
--不存在這樣的課程y,學生201215122選修了y,而學生x沒選
SELECT DISTINCT Sno from SC SCX where NOT EXISTS(
SELECT * from SC SCY where SCY.Sno = '201215122' And not EXISTS (
SELECT * from SC SCZ WHERE SCZ.Sno = SCX.Sno AND SCZ.Cno = SCY.Cno));
--集合查詢 系統會自動去掉重複元組
--UNION 並
SELECT Sno from SC where Cno = '1' UNION SELECT Sno from SC where Cno = '2';
--intersect 交操作 不行 mysql不支持
--SELECT * from student where Sdept = 'CS' INTERSECT SELECT * from student where Sage<=19;
SELECT * from student where Sdept = 'CS' and Sage <=19;
--except 差 mysql 不支持 返回第一個表一有,表二沒有的
--select * from Student where Sdept = 'CS' EXCEPT SELECT * from Student WHERE Sdept = 'CS' and Sage>=19;
SELECT * from Student where Sdept = 'CS' AND Sage >19;
--派生表的查詢 未成功
--SELECT Sno,Cno from SC, (SELECT Sno,AVG(Grade) from SC GROUP BY Sno)
--AS Avg_sc(avg_sno , avg_grade)
--WHERE SC.Sno = Avg_sc.avg_sno and SC.Grade >= Avg_sc.avg_grade;
--數據更新
insert into Student VALUES ('201215126','張成民','男',18,'CS');
SELECT * from student;
update Student set Sage = 22 where Sno = '201215121';
delete from Student where Sno = '201215126';
實驗四(第三章課後習題4-9)
建表&插入數據
--建表
--供應商表
DROP table if exists `S`;
CREATE table `S`(
SNO varchar(255) not null,
SNAME varchar(255) not null,
STATU int(11) not null, #STATUS 爲關鍵字
CITY varchar(255) not null,
PRIMARY key(SNO)
)
--零件表
drop table if EXISTS `P`;
CREATE table `P`(
PNO varchar(255) not null,
PNAME varchar(255) not null,
COLOR varchar(255) not null,
WEIGHT int(11) not null,
PRIMARY key(PNO)
)
--工程項目表
drop table if EXISTS `J`;
CREATE table `J`(
JNO varchar(255) not null,
JNAME varchar(255) not null,
CITY varchar(255) not null,
PRIMARY key(JNO)
)
--供應情況表
#restrict 如果從S,P,J中刪除一條數據,SPJ中有 ,則會報錯不允許刪除
#cascade 對應數據一起刪除
drop TABLE if exists `SPJ`;
CREATE table `SPJ`(
SNO VARCHAR(255) not null,
PNO varchar(255) not null,
JNO varchar(255) not null,
QTY int(11) not null,
PRIMARY key(SNO,PNO,JNO),
foreign key(SNO) references S(SNO),
foreign key(PNO) references P(PNO),
foreign key(JNO) references J(JNO)
)
select * from S;
insert into S VALUES('S1','精益',20,'天津');
insert into S VALUES('S2','盛錫',10,'北京');
insert into S VALUES('S3','東方紅',30,'北京');
insert into S VALUES('S4','豐泰盛',20,'天津');
insert into S VALUES('S5','爲民',30,'上海');
SELECT * from P;
insert into P values('P1','螺母','紅',12);
insert into P values('P2','螺栓','綠',17);
insert into P values('P3','螺絲刀','藍',14);
insert into P values('P4','螺絲刀','紅',14);
insert into P values('P5','凸輪','藍',40);
insert into P values('P6','齒輪','紅',30);
SELECT * from J;
insert into J VALUES('J1','三建','北京');
insert into J VALUES('J2','一汽','長春');
insert into J VALUES('J3','彈簧廠','天津');
insert into J VALUES('J4','造船廠','天津');
insert into J VALUES('J5','機車廠','唐山');
insert into J VALUES('J6','無線電廠','常州');
insert into J VALUES('J7','半導體廠','南京');
SELECT * from SPJ;
insert into SPJ VALUES('S1','P1','J1',200);
insert into SPJ VALUES('S1','P1','J3',100);
insert into SPJ VALUES('S1','P1','J4',700);
insert into SPJ VALUES('S1','P2','J2',100);
insert into SPJ VALUES('S2','P3','J1',400);
insert into SPJ VALUES('S2','P3','J2',200);
insert into SPJ VALUES('S2','P3','J4',500);
insert into SPJ VALUES('S2','P3','J5',400);
insert into SPJ VALUES('S2','P5','J1',400);
insert into SPJ VALUES('S2','P5','J2',100);
insert into SPJ VALUES('S3','P1','J1',200);
insert into SPJ VALUES('S3','P3','J1',200);
insert into SPJ VALUES('S4','P5','J1',100);
insert into SPJ VALUES('S4','P6','J3',300);
insert into SPJ VALUES('S4','P6','J4',200);
insert into SPJ VALUES('S5','P2','J4',100);
insert into SPJ VALUES('S5','P3','J1',200);
insert into SPJ VALUES('S5','P6','J2',200);
insert into SPJ VALUES('S5','P6','J4',500);
查詢
--查詢
--第四題
SELECT DISTINCT SNO from SPJ where JNO='J1';
SELECT DISTINCT SNO from SPJ where JNO='J1' and PNO='P1';
SELECT SNO from SPJ where JNO='J1' and PNO in(SELECT PNO from P where COLOR='紅');
SELECT JNO from J where not exists (
SELECT * from SPJ where SNO in(
SELECT SNO from S where CITY = '天津')
and PNO in(
SELECT PNO from P where COLOR='紅')
and SPJ.JNO = J.JNO
)
--等價於
SELECT JNO from J where not EXISTS (
SELECT * from SPJ,S,P where SPJ.PNO=P.PNO and SPJ.SNO=S.SNO and S.CITY = '天津' and P.COLOR='紅' and JNO=J.JNO);
SELECT DISTINCT PNO from SPJ where SNO='S1';
SELECT JNO from SPJ where PNO='P1' and SNO='S1' and JNO in (SELECT JNO from SPJ where PNO='P2' and SNO='S1') ;
--等價於
SELECT JNO from J where not EXISTS(
SELECT * from SPJ x where x.SNO='S1' and not EXISTS(
SELECT * from SPJ y where y.PNO=X.PNO and y.JNO=J.JNO and y.SNO='S1')
)
--第五題
SELECT SNAME,CITY from S;
SELECT PNAME,COLOR,WEIGHT from P;
SELECT JNO from SPJ where SNO = 'S1';
SELECT PNAME,QTY from SPJ,P where JNO = 'J2' and SPJ.PNO=P.PNO;
SELECT DISTINCT PNO from S,SPJ where CITY='上海' and SPJ.SNO = S.SNO;
SELECT DISTINCT JNAME from S,J,SPJ where S.CITY = '上海' and S.SNO = SPJ.SNO and J.JNO=SPJ.JNO;
SELECT DISTINCT JNO from J where JNO not in(
SELECT JNO from S,SPJ where CITY = '天津' and S.SNO =SPJ.SNO)
SELECT * from P;
UPDATE P set COLOR = '藍' where COLOR = '紅';
SELECT * from SPJ;
update SPJ set SNO='S3' where SNO='S5' and JNO='J4' and PNO='P6';
SELECT * from S;
DELETE from S where SNO='S2';
INSERT into SPJ VALUES('S2','P4','J6',200);
視圖
基本表是本身獨立存在的表,在SQL一個關係對應一個表。視圖是從一個或幾個基本表中導出的表。視圖本身不獨立存儲在數據庫中,是一個虛表。即數據庫中只存放視圖的定義而不存放視圖對應的數據,這些數據仍然存放在導出視圖的基本表中。視圖在概念上與基本表等同,用戶可以如同基本表那樣使用視圖,可以在視圖上再定義視圖。
視圖優點
1、視圖可以簡化用戶的操作;
使用戶將注意力集中在所關心的數據上
2、視圖使用戶能以多種角度看待同一數據
3、視圖對重構數據庫提供了一定程度的邏輯獨立性
數據的物理獨立性:用戶的應用程序不依賴於數據庫的物理結構
數據的邏輯獨立性:當數據庫重構造時,如增加新的關係或對原有關係增加新的字段等,用戶的應用程序不會受影響。
4、視圖能夠對機密數據提供安全保護
在設計數據庫應用系統時對不同的用戶定義不同的視圖,使機密數據不出現在不應看到這些數據的用戶視圖上。
視圖更新
行列子集視圖:一個視圖是從單個基本表導出的,並且只是去掉了基本表的某些行和某些列,但保留了主碼,則稱這類視圖爲行列子集視圖。
哪類視圖可以更新:一般的,行列子集視圖是可以更新的。
規定不可更新:
1、若視圖是優兩個以上基本表導出的,則此視圖不允許更新;
2、若視圖的字段來自字段表達式或常數,則不允許對此視圖執行insert和update操作,但允許執行delete操作;
3、若視圖的字段來自聚集函數,對此視圖不允許更新;
4、若視圖定義中含有GROUP BY 子句,則不允許更新;
5、若視圖中含有distinct短語,則不允許更新;
6、若視圖定義中有嵌套查詢,並且內層查詢的FROM子句中涉及的表也是導出該視圖的基本表,則此視圖不允許更新;
CODE
--視圖
CREATE VIEW VSP
AS SELECT SNO,SPJ.PNO,QTY from SPJ,J
where SPJ.JNO=J.JNO and J.JNAME='三建';
SELECT * from VSP;
SELECT PNO,sum(QTY) from vsp GROUP BY PNO;
SELECT * from VSP where SNO='S3';
實驗五
實驗目的:
通過實驗體會關係數據理論,函數依賴以及範式
實驗內容:
1.設計一個關係,使之滿足1NF而不滿足2NF。
2.設計一個關係,使之滿足2NF而不滿足3NF。
3.設計一個關係,使之滿足3NF而不滿足BCNF。
4.詳細描述各關係的語義,分析各關係中存在的函數依賴。
5.使用商用數據庫SQL Server或MySQL設計實現,錄入數據。體會數據冗餘、插入異常、刪除異常、修改複雜。
實驗步驟:
1、設計一個關係,使之滿足1NF而不滿足2NF。
關係模式Student(Sno,Cno,Sname,Sage,Sdept,Mname,Grade),其中Mname爲系主任名字。
Student
學號(Sno) | 課程號(Cno) | 姓名(Sname) | 年齡(Sage) | 所在系(Sdept) | 系主任(Mname) | 成績(Grade) |
---|---|---|---|---|---|---|
碼爲(Sno,Cno)
函數依賴
由以上函數依賴可以看出有部分函數依賴。所以不滿足第二範式。
2、設計一個關係,使之滿足2NF而不滿足3NF。
消除上面Student的部分函數依賴。分解成兩個關係模式:
SC(Sno,Cno,Grade);
S(Sno,Sname,Sage,Sdept,Mname);
S表滿足2NF而不滿足3NF。
碼是Sno
S表的函數依賴
KaTeX parse error: No such environment: split at position 8:
\begin{̲s̲p̲l̲i̲t̲}̲
&{Sno\rightarr…
由函數依賴得知:
每一個非主屬性完全函數依賴於碼。滿足2NF。
但是存在傳遞依賴:
所以S表不滿足3NF。
3.設計一個關係,使之滿足3NF而不滿足BCNF。
對於BCNF:滿足3NF,每函數依賴的x必是候選碼。
關係模式STJ(S,T,J)
關係模式STJ(S,T,J)中,S表示學生,T表示教師,J表示課程。每一個教師只教一門課。每門課有 若干個教師,某一學生選定某門課,就對應一個固定的教師。
函數依賴
KaTeX parse error: No such environment: split at position 8:
\begin{̲s̲p̲l̲i̲t̲}̲
&{(S,J)\righta…
(S,J), (S,T) 都是候選碼。
因爲沒有任何非主屬性對碼傳遞依賴或部分依賴。STJ是3NF。
但是T是決定因素,T不包含碼。所以STJ不滿足BCNF。
5.使用商用數據庫SQL Server或MySQL設計實現,錄入數據。體會數據冗餘、插入異常、刪除異常、修改複雜。
對於上面關係模式Student設計實現:
Code:
drop table if exists `Student`;
CREATE table `Student`(
Sno varchar(255) not null,
Cno varchar(255) not null,
Sname varchar(255) not null,
Sage int(11) ,
Sdept varchar(255) ,
Mname varchar(255) ,
Grade int(11) ,
PRIMARY key(Sno,Cno)
)
//
insert into Student VALUES('S1','C1','李二',23,'CS','張明',99);
insert into Student VALUES('S2','C2','阿三',22,'CS','張明',98);
SELECT * from Student;
(1)插入異常
//插入異常
INSERT into Student(Sno,Sname,Sdept,Mname) VALUES('S7','劉明','保密','張傑');
報錯:不能插入數據。
由於該生還未選課,即這個學生無Cno,這樣的元組不能插入Student中。因爲插入元組時必須給定碼值,而這時的碼值一部分爲空,因而學生的固有信息無法插入。
(2)刪除異常
//刪除異常
delete from Student where Cno = 'C2';
如上表,學生S2只選 了一門課C2, 現在C2這門課他不選了,那麼C2這個數據項就要刪除。
刪除C2, 因爲C2爲主屬性,所以整個元組一起被刪除,使得S2的其他信息也被刪除了,從而造成刪除異常。
(3)修改複雜
學生S2從計算機(CS)轉到數學系(MA) ,本來只需要修改學生元組的Sdept分量即可,但因爲關係模式Student中還含有系的系主任Mname屬性,學生轉系將同時改變系主任,因此還必須修改元組中 的 Mname 分量。另外,如果這個學生選修了k門課,Sdept,Mname重複了k次,不僅存儲冗餘度打,而且必須無遺漏的修改k個元組中全部的Sdept,Mname分量,造成修改的複雜化。
(4)數據冗餘
每一個系的系主任姓名重複出項。重複 次數與該系所有學生的課程成績出現次數相同,這將浪費大量的存儲空間。
實驗六
實驗目的:
通過實驗體會關係數據理論,函數依賴以及範式
實驗內容:
1.關係模式R(員工編號,日期,零件數,部門名稱,部門經理),表示某個工廠裏每個員工的日生產零件數以及員工所在的部門和經理信息。假設:每個員工每天只有一個日生產零件數,每個員工只在一個部門工作,每個部門只有一個經理:
(1)寫出模式R的基本函數依賴和候選碼。
(2)R是否是2NF,如果不是,把R分解成2NF。
(3)進一步將R分解爲3NF。
2.《數據庫系統》課本P202,習題2