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)

 

你可能感兴趣的:(数据库)