🎯目的:
熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性的定义和维护方法;掌握用户自定义完整性的定义和维护方法。
🎯内容:
(一)用SQL语句建立如下mystudent数据库,包括student、course、sc三个基本表,要求实现关系的三类完整性。
学生表student由学号Sno、姓名Sname、性别 Ssex、年龄 Sage、所在系Sdept组成。
表1-1 student表
Sno |
Sname |
Ssex |
Sage |
Sdept |
201215121 |
李勇 |
男 |
20 |
CS |
201215122 |
刘晨 |
女 |
19 |
CS |
201215123 |
王敏 |
女 |
18 |
MA |
201215125 |
张立 |
男 |
19 |
IS |
代码如下:
CREATE TABLE Student (Sno CHAR(9) PRIMARY KEY, /* 列级完整性约束条件,Sno是主码*/ Sname CHAR(20) UNIQUE, /* Sname取唯一值*/ Ssex CHAR(2), Sage SMALLINT, Sdept CHAR(20) )charset utf8;
插入数据:
INSERT INTO student VALUES ("201215121","李勇","男",20,"CS"), ("201215122","刘晨","女",19,"CS"), ("201215123","王敏","女",18,"MA"), ("201215125","张立","男",19,"IS");
课程表course由课程号Cno、课程名Cname、先行课Cpno、学分Ccredit组成。
表1-2 course表
Cno |
Cname |
Cpno |
Ccredit |
1 |
数据库 |
5 |
4 |
2 |
数学 |
2 |
3 |
信息系统 |
1 |
4 |
4 |
操作系统 |
6 |
3 |
5 |
数据结构 |
7 |
4 |
6 |
数据处理 |
2 |
|
7 |
PASCAL语言 |
6 |
4 |
代码如下:
CREATE TABLE Course (Cno CHAR(4) PRIMARY KEY, Cname CHAR(40), Cpno CHAR(4), Ccredit SMALLINT, FOREIGN KEY (Cpno) REFERENCES Course(Cno) ) charset utf8;
插入数据:
INSERT INTO course (Cno,Cname,Ccredit) VALUES ("1","数据库",4), ("2","数学",2), ("3","信息系统",4), ("4","操作系统",3), ("5","数据结构",4), ("6","数据处理",2), ("7","PASCAL语言",4); update course set Cpno = case Cno when '1' then '5' when '3' then '1' when '4' then '6' when '5' then '7' WHEN '7' then '6' end WHERE Cno IN('1','3','4','5','7');
选课表sc由学号Sno、课程号Cno、成绩Grade组成。
表1-3 sc表
Sno |
Cno |
Grade |
201215121 |
1 |
92 |
201215121 |
2 |
85 |
201215121 |
3 |
88 |
201215122 |
2 |
90 |
201215122 |
3 |
80 |
代码如下:
CREATE TABLE SC (Sno CHAR(9), Cno CHAR(4), Grade SMALLINT, PRIMARY KEY (Sno,Cno), FOREIGN KEY (Sno) REFERENCES Student(Sno), FOREIGN KEY (Cno)REFERENCES Course(Cno) ) charset utf8;
插入数据:
INSERT INTO sc(Sno,Cno,Grade) values ("201215121","1",92), ("201215121","2",85), ("201215121","3",88), ("201215122","2",90), ("201215122","3",80);
(二)用SQL语句建立如下mySPJ数据库,包括S,P,J,和SPJ四个基本表(教材第二章习题5中的四个表),要求实现关系的三类完整性。
S(SNO,SNAME,STATUS,CITY);
P(PNO,PNAME,COLOR,WEIGHT);
J(JNO,JNAME,CITY);
SPJ(SNO,PNO,JNO,QTY)
供应商表S由供应商代码(SNO)、供应商姓名(SNAME)、供应商状态(STATUS)、供应商所在城市(CITY)组成。
表1-4 供应商表S
SNO |
SNAME |
STATUS |
CITY |
S1 |
精益 |
20 |
天津 |
S2 |
盛锡 |
10 |
北京 |
S3 |
东方红 |
30 |
北京 |
S4 |
丰泰盛 |
20 |
天津 |
S5 |
为民 |
30 |
上海 |
代码如下:
CREATE TABLE S (SNO CHAR(4) PRIMARY KEY, SNAME CHAR(4), STATUS CHAR(4), CITY CHAR(4) )CHARSET utf8; 插入数据: INSERT INTO s VALUES ("S1","精益","20","天津"), ("S2","盛锡","10","北京"), ("S3","东方红","30","北京"), ("S4","丰泰盛","20","天津"), ("S5","为民","30","上海");
零件表P由零件代码(PNO)、零件名(PNAME)、颜色(COLOR)、重量(WEIGHT)组成。
表1-5 零件表P
PNO |
PNAME |
COLOR |
WEIGHT |
P1 |
螺母 |
红 |
12 |
P2 |
螺丝 |
绿 |
17 |
P3 |
螺丝刀 |
蓝 |
14 |
P4 |
螺丝刀 |
红 |
14 |
P5 |
凸轮 |
蓝 |
40 |
P6 |
齿轮 |
红 |
30 |
代码如下:
CREATE TABLE p (PNO CHAR(4) PRIMARY KEY, PNAME CHAR(4), COLOR CHAR(4), WEIGHT char(4) )CHARSET utf8; 插入数据: INSERT INTO p VALUES ("P1","螺母","红","12"), ("P2","螺丝","绿","17"), ("P3","螺丝刀","蓝","14"), ("P4","螺丝刀","红","14"), ("P5","凸轮","蓝","40"), ("P6","齿轮","红","30");
工程项目表J由工程项目代码(JNO)、工程项目名(JNAME)、工程项目所在城市(CITY)组成。
表1-6 工程项目表J
JNO |
JNAME |
CITY |
J1 |
三建 |
北京 |
J2 |
一汽 |
长春 |
J3 |
弹 簧 厂 |
天津 |
J4 |
造 船 厂 |
天津 |
J5 |
机 车 厂 |
唐山 |
J6 |
无线电厂 |
常州 |
J7 |
半导体厂 |
南京 |
代码如下:
CREATE TABLE j (JNO CHAR(4), JNAME CHAR(4), CITY CHAR(4) )CHARSET utf8; 插入数据: INSERT INTO j VALUES ("J1","三建","北京"), ("J2","一汽","长春"), ("J3","弹簧厂","天津"), ("J4","造船厂","天津"), ("J5","机车厂","唐山"), ("J6","无线电厂","常州"), ("J7","半导体厂","南京");
供应情况表SPJ由供应商代码(SNO)、零件代码(PNO)、工程项目代码(JNO)、供应数量(QTY)组成,表示某供应商 供应某种零件 给某工程项目的数量为QTY。
表1-7 供应情况表SPJ
SNO |
PNO |
JNO |
QTY |
S1 |
P1 |
J1 |
200 |
S1 |
P1 |
J2 |
5000 |
S1 |
P1 |
J3 |
100 |
S1 |
P1 |
J4 |
700 |
S1 |
P2 |
J2 |
100 |
S2 |
P3 |
J1 |
400 |
S2 |
P3 |
J2 |
200 |
S2 |
P3 |
J4 |
500 |
S2 |
P3 |
J5 |
400 |
S2 |
P5 |
J1 |
400 |
S2 |
P5 |
J2 |
100 |
S3 |
P1 |
J1 |
200 |
S3 |
P3 |
J1 |
200 |
S4 |
P5 |
J1 |
100 |
S4 |
P6 |
J3 |
300 |
S4 |
P6 |
J4 |
200 |
S5 |
P2 |
J4 |
100 |
S5 |
P3 |
J1 |
200 |
S5 |
P6 |
J2 |
200 |
S5 |
P6 |
J4 |
500 |
代码如下:
CREATE TABLE spj (SNO CHAR(4), PNO CHAR(4), JNO CHAR(4), QTY char(4), PRIMARY KEY (SNO,PNO,JNO), FOREIGN KEY (SNO) REFERENCES s(SNO), FOREIGN KEY (PNO) REFERENCES p(PNO), FOREIGN KEY (JNO) REFERENCES j(JNO) )CHARSET utf8;
插入数据:
INSERT INTO spj VALUES ("S1","P1","J1","200"), ("S1","P1","J2","5000"), ("S1","P1","J3","100"), ("S1","P1","J4","700"), ("S1","P2","J2","100"), ("S2","P3","J1","400"), ("S2","P3","J2","200"), ("S2","P3","J4","500"), ("S2","P3","J5","400"), ("S2","P5","J1","400"), ("S2","P5","J2","100"), ("S3","P1","J1","200"), ("S3","P3","J1","200"), ("S4","P5","J1","100"), ("S4","P6","J3","300"), ("S4","P6","J4","200"), ("S5","P2","J4","100"), ("S5","P3","J1","200"), ("S5","P6","J2","200"), ("S5","P6","J4","500"); SELECT *FROM spj;
🎯运行结果:
(一)用SQL语句建立如下mystudent数据库,包括student、course、sc三个基本表,要求实现关系的三类完整性。
1.创建一张学生表
2.创建一张课程表
3.创建一张选课表
(二)用SQL语句建立如下mySPJ数据库,包括S,P,J,和SPJ四个基本表(教材第二章习题5中的四个表),要求实现关系的三类完整性。
1.创建供应商表S由供应商代码(SNO)、供应商姓名(SNAME)、供应商状态(STATUS)、供应商所在城市(CITY)组成。
2.创建零件表P由零件代码(PNO)、零件名(PNAME)、颜色(COLOR)、重量(WEIGHT)组成。
3.创建工程项目表J由工程项目代码(JNO)、工程项目名(JNAME)、工程项目所在城市(CITY)组成。
4.创建供应情况表SPJ由供应商代码(SNO)、零件代码(PNO)、工程项目代码(JNO)、供应数量(QTY)组成,表示某供应商 供应某种零件 给某工程项目的数量为QTY。
🎯问题及解决:
问题一:主码可以为可以为空吗?
答:主码并不能为空值,必须取值。
问题二:为什么有的主码无法删除,如果想要删除,应如何做?
答:因为有的主码会被其他表中的外码中作为参考,无法删除。要想删除,必须先把其他表中的外码删除,再删除这个主码。
问题三:如果外码和其参考码在同一个表中应如何编辑数据?
答:应该先插入参考码和其他码(外码先不插入数据),插入完成之后,再去更新(一定要是update更新,而不是insert插入)外码的值。