修改表的约束条件 用alter table <表名>
前言,数据库中有5个表,student,sc,course,tc,teacher;(sql server里面不区分大小写)
student
sc tc
course
teacher
- 定义课程表中先修课程参照课程表中的课程号完整
alter table course add foreign key (cpno) references course (cno);
解释:目标:课程表中的先修课 即先修课是外码 course foreign key (cpno)
被参照表 :课程表(自己参照自己)course
被参照列:课程号 course (cno)
2. 设置学生表中Ssex的取值只能是男或 女
alter table student add check (ssex='男'or ssex='女');
解析:确定取值范围用 check
目标表:学生表 student
3.设置学生选课表中的成绩取值为0~100分
alter table sc add check (grade between 0 and 100) ;
解析;目标表/列 sc/grade
between 0 and 100 确定取值区间
4.修改学生性别的默认值为女
alter table student add default ('女') for ssex with values;
解析;添加默认值用 default