SQL语句修改表结构和添加约束

--1.删除一列

alter table TbStudent drop column stuPhone

--2.添加一列

alter table TbStudent add  stuPhone char(11)

--3.修改字段的数据类型(表中Gender列不能有数据)

alter table TbStudent alter column stuGender nchar(1)

--4.添加主键约束

alter table TbStudent add constraint PK_TbStudent_stuId primary key(stuId)

--5.添加唯一性约束

alter table TbStudent add constraint UK_TbStudent_stuName unique(stuName)

--6.添加check约束

alter table TbStudent add constraint CK_TbStudent_stuAge

check(stuAge>=18 and stuAge<=35)

--7.添加非空约束,实际上就是对列的数据类型修改

alter table TbStudent alter column stuPhone char(11) not null

--8.添加外键约束

alter table TbStudent add constraint FK_TbStudent_stuClassId

foreign key(stuClassId) references TbClass(clsId)

--9.外键的级联删除/更新

--语法: on delete [no action cascade]

--       on update [no action cascade]

alter table TbStudent add constraint FK_TbStudent_stuClassId

foreign key(stuClassId) references TbClass(clsId) on delete cascade

--10.删除约束

alter table TbStudent drop constraint Fk_TbStudent_stuClassId

--11.一条语句删除多条约束

 alter table TbStudent drop constraint Fk_TbStudent_stuClassId,CK_TbStudent_stuAge

 --12.添加一条语句,添加多个约束

  alter table TbStudent add

  constraint FK_TbStudent_stuClassId foreign key(stuClassId) references TbClass(clsId)

  constraint PK_TbStudent_stuId primary key(stuId)

 

全部评论

相关推荐

03-29 18:59
运城学院 Java
程序员小白条:咱们要对自己的简历和学历有清晰的认知,不要动不动就大厂了....都26届了,没实习还想着大厂,唉
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务