数据库实验四

-- 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;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值