• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

数据库手动设置数据约束

mysql 搞代码 4年前 (2022-01-09) 28次浏览 已收录 0个评论

数据库手动设置数据约束 1手动添加[主键约束]PK_Employees_EmpId alter table Employees add constraint PK_Employees_EmpId primary key(EmpId) http://www.2cto.com 2手动为EmpName增加非空约束 alter table Employees alter column EmpName va本文来源gaodai$ma#com搞$$代**码)网8rchar(50) not null

数据库手动设置数据约束

1手动添加[主键约束]PK_Employees_EmpId

alter table Employees add constraint PK_Employees_EmpId primary key(EmpId)

http://www.2cto.com

2手动为EmpName增加非空约束

alter table Employees alter column EmpName varchar(50) not null

3手动为EmpName增加唯一键约束

alter table Employees add constraint UQ_Employees_EmpName unique(EmpName)

go

http://www.2cto.com

4删除唯一键约束

alter table Employees drop constraint UQ_Employees_EmpName

5为性别增加默认约束,使默认值为”男”

alter table Employees add constraint DF_Employees_EmpGender

default(1) 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_EmpGender

check(EmpGender='男' or EmpGender='女')

8–为员工表增加外键约束

–首先,设置部门表中的DepId为主键,并且外键不能为空

alter table Department add constraint Pk_Deparment_DepId primary key(DepId)

alter table Employee alter column DepId int not null

alter table Employee add constraint FK_Employee_EmpDepId

foreign key(DepId) references Department(DepId) on delete cascade

http://www.2cto.com

9一条语句删除多个约束

alter table Employees drop constraint FK_Employees_EmpDepId,

CK_Employees_EmpAge,

UQ_Employees_EmpName

10一条语句为表增加多个约束

alter table Employees add constraint

UQ_Employees_EmpName

unique(EmpName),

constraint

CKSSS

check(EmpAge>=0and EmpAge<=120)


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:数据库手动设置数据约束

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址