C实战|人员管理系统[3]:在数据库中创建数据表的各种约束

C实战|人员管理系统[3]:在数据库中创建数据表的各种约束哈喽 你好啊 我是雷工 在创建完表格后还需要添加一些约束条件 以下为添加各种约束的相关笔记

欢迎大家来到IT世界,在知识的湖畔探索吧!

哈喽,你好啊,我是雷工!

在创建完表格后还需要添加一些约束条件,以下为添加各种约束的相关笔记。

01 主键约束

要对一个列加主键约束的话,该列就必须要满足的条件就是非空;

主键约束为非空,不重复,

格式为:

alter table 表格名称 add constraint 约束名称 增加约束的类型 (列名)

这里要对表格Peoples的PeopleId列添加主键约束,

T-SQL代码如下:

--创建“主键”约束primary key if exists(select * from sysobjects where name='pk_PeopleId') alter table Peoples drop constraint pk_PeopleId alter table Peoples add constraint pk_PeopleId primary key (PeopleId)

欢迎大家来到IT世界,在知识的湖畔探索吧!

C实战|人员管理系统[3]:在数据库中创建数据表的各种约束

欢迎大家来到IT世界,在知识的湖畔探索吧!

02 check约束

check约束就是给一列数据进行了限制;

格式为:

alter table 表名称 add constraint 约束名称 增加约束的类型 (列名 (约束条件))

此处要求Peoples表中的年龄范围在20-40之间,要求身份证长度为18;

T-SQL代码如下:

欢迎大家来到IT世界,在知识的湖畔探索吧!--创建检查约束check if exists(select * from sysobjects where name='ck_Age') alter table Peoples drop constraint ck_Age alter table Peoples add constraint ck_Age check (Age between 20 and 40) --创建身份证的长度检查约束 if exists(select * from sysobjects where name='ck_PeopleIdNo') alter table Peoples drop constraint ck_PeopleIdNo alter table Peoples add constraint ck_PeopleIdNo check (len(PeopleIdNo)=18)
C实战|人员管理系统[3]:在数据库中创建数据表的各种约束

03 unique约束

unique约束就是给列的数据追加的不重复的约束类型;

格式为:

alter table 表名称 add constraint 约束名称 增加约束的类型 (列名 (约束条件))

此处要求身份证号和考勤卡号不能重复;

T-SQL代码如下:

--创建唯一约束unique if exists(select * from sysobjects where name='uq_PeopleIdNo') alter table Peoples drop constraint uq_PeopleIdNo alter table Peoples add constraint uq_PeopleIdNo unique (PeopleIdNo) if exists(select * from sysobjects where name='uq_CardNo') alter table Peoples drop constraint uq_CardNo alter table Peoples add constraint uq_CardNo unique (CardNo)
C实战|人员管理系统[3]:在数据库中创建数据表的各种约束

04 默认约束

默认约束就是让该列的数据默认为一定的数据;

格式为:

alter table 表名称 add constraint 约束名称 约束类型 (默认值)for 列名

此处要求地址列无输入时默认为:地址不详;绩效的更新时间和考勤表的打卡时间,默认为获取当前时间;

T-SQL代码如下:

欢迎大家来到IT世界,在知识的湖畔探索吧!--创建默认约束 if exists(select * from sysobjects where name='df_PeopleAddress') alter table Peoples drop constraint df_PeopleAddress alter table Peoples add constraint df_PeopleAddress default ('地址不详' ) for PeopleAddress if exists(select * from sysobjects where name='df_UpdateTime') alter table Performances drop constraint df_UpdateTime alter table Performances add constraint df_UpdateTime default (getdate() ) for UpdateTime if exists(select * from sysobjects where name='df_DTime') alter table Attendance drop constraint df_DTime alter table Attendance add constraint df_DTime default (getdate() ) for DTime
C实战|人员管理系统[3]:在数据库中创建数据表的各种约束

05 外键约束

外键约束是用于两个表之间数据完整性和一致性的一种机制,它定义在一个表的字段上,引用另一个表的主键字段,外键约束确保引用表中的数据与被引用表中的数据保持一致,防止破坏两个表之间的关联关系。

格式为:

alter table 表名称 add constraint 约束名称 约束类型 (列名)references 被引用的表名称(列名)

T-SQL代码如下:

--创建外键约束 if exists(select * from sysobjects where name='fk_GroupId') alter table Peoples drop constraint fk_GroupId alter table Peoples add constraint fk_GroupId foreign key (GroupId) references Groups(GroupId) if exists(select * from sysobjects where name='fk_PeopleId') alter table Performances drop constraint fk_StudentId alter table Performances add constraint fk_PeopleId foreign key(PeopleId) references Peoples(PeopleId)
C实战|人员管理系统[3]:在数据库中创建数据表的各种约束

06 后记

以上为该实例中个数据表所需的约束,有记录不当或有更好的实现方法的可以留言讨论;

有更多相关话题也可以在交流群内沟通。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/105185.html

(0)
上一篇 1天前
下一篇 1天前

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信