-- 1.
create database studens;
use studens;
create table if not exists stu
(
学号 char(4) not null primary key,
姓名 char(8),
性别 char(2),
出生日期 date
);
-- 2.
create table if not exists sc
(
学号 char(4) not null,
课号 char(4) not null,
成绩 decimal(5, 2) check ( 0 <= 成绩 <= 100 ),
primary key (学号, 课号)
);
alter table sc
add constraint fk_sno foreign key (学号) references stu (学号);
-- 3.
create table course
(
课号 char(4) not null,
课名 char(20),
学分 int
);
alter table course
add constraint uq_name unique (课名);
# 4. 在course表的课号列建立主键约束。
alter table course
add primary key (课号);
# 5. 在sc表的课号列建立外键约束fk_cno,参照course表的课号列的取值,要求实现级联更新。
alter table sc
add constraint fk_cno foreign key (课号) references course (课号) on update cascade;
# 6. 在stu表的姓名列建立唯一约束名uq_sname。
alter table stu
add constraint uq_sname unique (姓名);
# 7. 在course表的学分列建立检查约束ck_xf,检查条件为学分>0。
alter table course
add constraint ck_xf check ( 学分 > 0 );
# 8. 删除sc表的外键约束fk_cno,fk_sno。
alter table sc
drop foreign key fk_cno;
alter table sc
drop foreign key fk_sno;
# 9. 删除stu表的主键约束。
alter table stu
drop primary key;
# 10.删除course表的唯一约束uq_cname。
alter table course
drop index uq_name;
-- 11触发器
create table text like stu;
create trigger test_trig
after insert
on stu
for each row insert into text(学号, 姓名, 性别, 出生日期)
values (NEW.学号, new.姓名, new.性别, new.出生日期);
insert into stu(学号,姓名,性别,出生日期)values ('111','ttt','男','2000-10-1');
select *
from text;
数据库实验四
于 2023-11-25 17:48:04 首次发布