MySQL "Data Constraint Experiment"

/* 在查询分析器中用Create命令创建的“score_info”数据库中定义基本表:学生表(Student),课程表(Course),选修表(SC)。 */
-- 创建库  “score_info”
DROP DATABASE IF EXISTS score_info;
CREATE DATABASE IF NOT EXISTS score_info;
USE  score_info;

-- 创建学生表 Student,字符集 utf8
DROP TABLE IF EXISTS student;
CREATE TABLE student (
sno int not null primary key comment '学号', 
sname varchar(20) not null unique comment '学生姓名',
ssex enum('男','女') comment '性别',
sage smallint default 18 comment '年龄',
sdept varchar(30) comment '所在院系'
) charset utf8;

-- 创建课程表 Course,字符集 utf8
DROP TABLE IF EXISTS course;
CREATE TABLE course (
cno int not null primary key comment '课程号',
cname varchar(20) not null comment '课程名',
cpno int comment '先行课',
credit smallint comment '学分'
) charset utf8;

-- 创建选课表 SC,字符集 utf8 
DROP TABLE IF EXISTS SC;
CREATE TABLE SC (
sno int not null comment '学号',
cno int not null comment '课程号',
grade int comment '成绩',
primary key (sno,cno)
) charset utf8;


-- 学生表插入数据
INSERT INTO student VALUES 
(201215121,'李勇','男',20,'CS'),
(201215122,'刘晨','女',19,'CS'),
(201215123,'王敏','女',18,'MA'),
(201215124,'张立','男',19,'IS');
 
 -- 课程表插入数据
 INSERT INTO course VALUES 
(1, '数据库', 5, 4),
(2, '数学', NULL, 2),
(3, '信息系统', 1, 4),
(4, '操作系统', 6, 3),
(5, '数据结构', 7, 4),
(6, '数据处理', NULL, 2),
(7, 'PASCAL', 6, 4);

 -- 选课表插入数据
 INSERT INTO SC VALUES 
(201215121, 1, 92),
(201215121, 2, 85),
(201215121, 3, 88),
(201215122, 2, 90),
(201215122, 3, 80);
/* 表和数据输入完毕。开始操作。 */

(1) Add a unique constraint on Cname to the existing table Course in the database.

alter table course add unique (cname);

(2) For the existing table Course in the database, increase the Cpno reference Cno's foreign key constraint (self-table foreign key, also called table internal and foreign key, such a table is called self-associated table). For foreign key knowledge, refer to page 168 of the textbook.

alter table course add foreign key (cpno) references course(cno);

(3) Add foreign key constraints (2 commands) to the table SC that already exists in the database. The key names are sc_fk_sno and sc_fk_cno. ①Add the foreign key sc_fk_sno to the sno field: ②Add the foreign key sc_fk_cno to the cno field:

alter table sc add constraint sc_fk_sno foreign key (sno) references student (sno);

alter table sc add constraint sc_fk_cno foreign key (cno) references course (cno);

(4) Check the primary key constraint in the Student table, and then delete the primary key constraint. Check the error message. Add a unique constraint for the sno field of the student table, and then delete the primary key constraint. ① Add a unique constraint to the sno field of the student table. ② Delete the primary key constraint of the Student table.

alter table student add unique (sno);

alter table student drop primary key;

(5) Check the Cname unique constraint in the Course table, and then delete the unique constraint. Write code: delete the unique constraint of the Cname field.

alter table course drop index cname;

(6) Check the Sage default value constraint in the Student table, and then delete the default value constraint. Write code: delete Sage's default value constraint (no need to write a comment).

alter table student modify sage smallint;

(7) Check the non-empty constraint of the Sname attribute in the Student table, and then delete the non-empty constraint. Write code: delete the Sname non-empty constraint (no need to write a comment).

alter table student modify sname varchar(20);; alter table student modify sname varchar(20) null;

(8) Check the foreign key constraints in the Course table, and then delete the foreign key constraints. Write code: delete foreign key constraints.

alter table course drop foreign key course_ibfk_1;

(9) Change the age of the student 201215124 to 22 years old.

update student set sage = 22 where sno = 201215124;

(10) Increase the age of all students by one year.

update student set sage = sage + 1;

(11) Add 5 points to all students' scores for No. 1 course.

update sc set grade = grade + 5 where cno = 1;

(12) Modify the student ID of "201215121" to "201215221". Check the error message. Modify the foreign key attribute of the sno field of the SC table to cascade update (delete the foreign key and add it). Modify the student number again. ①Delete the foreign key of the sno field of the sc table. ②Add the foreign key sc_fk_sno of the sno field of the sc table, and set the cascade update (on update cascade was added after the original foreign key code was added, refer to page 168 of the textbook).

First empty:
alter table sc drop foreign key sc_fk_sno;

第二空:
alter table sc add constraint sc_fk_sno foreign key (sno) references student (sno) on update cascade;

Guess you like

Origin blog.csdn.net/ziyue13/article/details/112070354