加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQLserver--各种约束

发布时间:2020-12-12 14:24:53 所属栏目:MsSql教程 来源:网络整理
导读:--1手动添加[主键约束]PK_Employees_EmpId alter table Employees add constraint PK_Employees_EmpId primary key(EmpId) --2手动为EmpName增加非空约束 alter table Employees alter column EmpName varchar(50) not null --3手动为EmpName增加唯一键约束

--1手动添加[主键约束]PK_Employees_EmpId
alter table Employees add constraint PK_Employees_EmpId primary key(EmpId)
--2手动为EmpName增加非空约束
alter table Employees alter column EmpName varchar(50) not null

--3手动为EmpName增加唯一键约束
alter table Employees add constraint UQ_Employees_EmpName unique(EmpName)
--4删除唯一键约束
alter table Employees drop constraint UQ_Employees_EmpName
--5为性别增加默认约束,使默认值为"男"
alter table Employees add constraint DF_Employees_EmpGender default('男') for EmpGender
--6为年龄增加检测约束? 0-120含0和120
alter table Employees add constraint CK_Employees_EmpAge check(EmpAge>=0 and EmpAge<=120)
--7为性别增加检查约束 非 男 即 女
alter table Employees add constraint CK_Employees_EmpGendere check(EmpGender='男' or EmpGender='女')
--8--为员工表增加外键约束

--首先,设置部门表中的DepId为主键,并且外键不能为空
alter table Department add constraint PK_Department_DepId primary key(DepId)
alter table Employees add constraint FK_Employees_EmpDpmId foreign key(EmpDpmId) references Department(DepId) on delete cascade
--9一条语句删除多个约束
alter table Employees drop constraint FK_Employees_EmpDpmId,
CK_Employees_EmpAge,
UQ_Employees_EmpName

--10一条语句为表增加多个约束
alter table Employees add constraint UQ_Employees_EmpName unique(EmpName),constraint CKSSS check(EmpAge>=0and EmpAge<=100)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读