下面是关于使用sql命令修改表的一些题,供大家复习参考
首先是表的结构
![](https://i-blog.csdnimg.cn/blog_migrate/9599f6d2a11dde60c65e6628ae0cc6a6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0a046d375b7afcd483383e0811f42622.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e56fcbab2cde7de100fe8ee8b2401c28.png)
题目如下
对于教师表的操作
- 设置教师表的’编号’为not null
--设置not null
alter table 教师表 alter column 编号 nvarchar(5) not null
- 设置教师表的主键为‘编号’
--设置主键
alter table 教师表 add primary key(编号)
- 修改教师表中年龄类型为为int
--修改字段属性
alter table 教师表 alter column 年龄 nvarchar(1)
- 在教师表增加一个字段手机号,nvarchar(11)
--添加一个字段
alter table 教师表 add 手机号 nvarchar(11)
- 删除教师表中的字段‘电话’
--删除一个字段
alter table 教师表 drop column 电话
- 设置教师中性别的默认值为‘男’
--添加默认值
alter table 教师表 add default '男' for 性别
- 设置教师中年龄的字段的取值范围为1-100
--为某列属性添加check约束
alter table 教师 add check (年龄>1 and 年龄<100)
下面是对学生表和课程表的操作(重复题目不再作答,大家可以尝试自己解答一下)
- 将student表中的st_sex列属性更改为NOT NULL
- 将student表中的st_from列默认值设置为“安徽省”
- 将slt_course表中的score列的检查约束设置为>=0且<=100
- 向slt_course表中添加标识列id,第1行默认值为1,相邻两个标识列间的增量为1
- 将course表中的cs_nm列设置为唯一性约束
alter table course add unique(cs_nm)
- 建立‘slt_course’的外键’st_id’,被参照表是’student’
alter table slt_course
add foreign key(st_id) references student(st_id);
第一次写博客,还有点小紧张。如有错误,麻烦大家提出来,我会及时更改!