实验六 约束与索引

第1关:添加约束一

实验完成要求

根据代码区的提示,将SQL语句书写在对应的代码区中。

注:请务必按要求为约束命名

1、为Student表的Sage列添加约束,使其取值小于30岁(约束名:stu_chk_sage)

2、为student表的Ssex列添加约束,使其只能取值‘m’或‘f’(约束名:stu_chk_ssex)

3、为Student表的ssex列添加缺省约束,缺省值为‘m’(约束名:stu_def_ssex)

alter table Student add constraint stu_chk_sage check(sage<30);
alter table Student add constraint stu_chk_ssex check(ssex='m'or ssex='f');
alter table Student add constraint stu_def_ssex default 'm' for ssex;

第2关:添加约束二

实验完成要求

根据代码区的提示,将SQL语句书写在对应的代码区中。

注:请务必按要求为约束命名

1、为SC表的sno列添加外码(约束名:stu_ref_sno)

2、为SC表的cno列添加外码(约束名:stu_ref_cno)

3、为SC表的grade列添加检查约束(1到100分)(约束名:stu_chk_grade)

alter table SC 
add constraint stu_ref_sno foreign key(sno)references Student(sno);
alter table SC 
add constraint  stu_ref_cno foreign key(cno)references Course(cno);
alter table SC 
add constraint stu_chk_grade check(grade between 1 and 100);

第3关:添加约束三

实验完成要求

根据代码区的提示,将SQL语句书写在对应的代码区中。

注:请务必按要求为约束命名

1、为Course表的cname列添加唯一约束(约束名:Course_un_cname)

2、为Course表的ccredit列添加检查约束,使其值为(1到10之间)(约束名:Course_chk_ccredit)

3、为Course表的cpno列添加外码约束(约束名:Course_ref_cpno)

select S
SELECT student.sname, avg(sc.grade) AS avg_grade
FROM student, sc
WHERE student.sno=sc.sno
GROUP BY student.sname
HAVING avg(sc.grade) >= all(
SELECT avg(grade) as avg
FROM sc
WHERE grade NOTNULL
GROUP BY sc.sno);
alter table Course 
add constraint Course_chk_ccredit check(ccredit between 1 and 10);
alter table Course 
add constraint Course_ref_cpno  foreign key(cpno) references Course(cno); 

第4关:创建索引

实验完成要求

根据代码区的提示,将SQL语句书写在对应的代码区中。

注:请务必按要求为索引命名

1、为Student表的sname列创建唯一索引(索引名:idx_student_sname)

2、为sc表的grade列创建降序索引(索引名:idx_sc_grade)

3、为course表的ccredit列创建升序索引(索引名:idx_course_cname)

create unique index  idx_student_sname  on Student(sname);
create index idx_sc_grade on SC(grade desc);
create index idx_course_cname on Course(ccredit asc);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值