MySQL《数据约束实验》

/* 在查询分析器中用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)对数据库中已经存在的表Course,增加对Cname的唯一约束。

alter table course add unique (cname);

(2)对数据库中已经存在的表Course,增加Cpno的参考Cno的外键约束(自表外键,也称表内外键,这样的表叫自关联表)。外键知识参照教材168页。

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

(3)对数据库中已经存在的表SC,增加外键约束(2条命令),键名分别为sc_fk_sno,sc_fk_cno。①为sno字段添加外键sc_fk_sno:②为cno字段添加外键sc_fk_cno:

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)检查Student表中主键约束,后删除主键约束。查看出错提示。为student表sno字段添加唯一约束,再删除主键约束。①为student表sno字段添加唯一约束。②删除Student表主键约束。

alter table student add unique (sno);

alter table student drop primary key;

(5)检查Course表中的Cname唯一约束,后删除唯一约束。写代码:删除Cname字段的唯一约束。

alter table course drop index cname;

(6) 检查Student表中的Sage默认值约束,后删除默认值约束。写代码:删除Sage的默认值约束(不用写comment)。

alter table student modify sage smallint;

(7)检查Student表中的Sname属性的非空约束,后删除非空约束。写代码:删除Sname非空约束(不用写comment)。

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

(8)检查Course表中的外键约束,后删除外键约束。写代码:删除外键约束。

alter table course drop foreign key course_ibfk_1;

(9)将学生201215124的年龄改为22岁。

update student set sage = 22 where sno = 201215124;

(10)将所有学生的年龄增加一岁。

update student set sage = sage + 1;

(11)将所有学生的1号课程成绩加5分。

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

(12)将“201215121”同学的学号修改为“201215221”。查看出错信息。修改SC表的sno字段的外键属性为级联更新(删除外键再添加)。再修改学号。①删除sc表sno字段外键。②添加sc表sno字段外键sc_fk_sno,设置级联更新(原添加外键代码后添加 on update cascade,参照教材168页)。

第一空:
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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值