关于SQL表的增删改

增加列(字段)

alter table 被添加的表 add column 新列名 数据类型 (完整性约束条件)
如:在student表中添加sex字段 ```

 alter table student 
 add column sex char(6) not null

增加表级完整性约束

alter table 被添加的表
add 表级完整性约束条件
如:令SC表的sno与cno做主码```
alter table SC

add constraint emph_pk primary key(sno,cno)

增加列级完整性约束

设置列不为空

Alter table 表名 modify 列名 not null 如:将student中的id设为不能为空

Alter table students modify id not null

删除基本表

drop table 表名 【restrict|cascade】
默认值为restric:若删除的表与其他表有关联则无法删除
cascade:删除的表及其所有与其关联的表都会被删掉

drop table sc cascade

删除列

alter table 表名
drop column 列名【restrict|cascade】
默认值为restric:若删除的列与其他表有关联则无法删除
cascade:删除的列及其所有与改列的其他对象,如视图
如:删除student的sex

alter table student
drop column sex

删除完整性约束

alter table 表名
drop constraint 完整性约束名【restrict|cascade】

改表名

EXEC sp_rename ‘old_tableName’, ‘new_tableName’
如:
下例将表 customers 重命名为 custs。

EXEC sp_rename 'customers', 'custs'

改列名

exec sp_rename ‘表名.列名’,‘新列名’,‘column’;
如:把书的note改为notes

exec sp_rename 'book.note','notes','column';

改列的数据类型

alter table 被改变的表
alter column 列名 数据类型
如:把student表的age改为int类型的

alter table student
alter column age int

该部分内容较多以后在单独写一篇博客

猜你喜欢

转载自blog.csdn.net/jiwei_1234____5/article/details/83551862