06Microsoft SQL Server 完整性约束

Microsoft SQL Server 完整性约束


 标识 IDENTITY自动编号

CREATE TABLE table_name(

    id int IDENTITY(1,1),

    NAME nvarchar(50) not null,

    sex char(2) default '',

    note ntext null

)

主键约束 PRIMARY KEY

 在表中定义一个主键来唯一确定表中一行数据的标识符,主键列的数据类型不限,但列必须唯一并且非空。一个表只允许一个主键,主键可以是单个字段或多个字段的组合。

create table tb(

    --单列主键的创建

    id int primary key

)

go

create table tb1(

    id int,

    name varchar(50),

    --多列组合的主键

    CONSTRAINT tb1_id_name_pk PRIMARY KEY(id,name)

)

Go

--为表添加主键

alter table table_name

add

constraint pk_name

primary key(column_name)

--删除主键

alter table table_name

drop

constraint pk_name

唯一性约束 UNIQUE防止非主键重复

CREATE TABLE Persons(

Id_P int NOT NULL UNIQUE,

LastName varchar(255) NOT NULL,

FirstName varchar(255),

Address varchar(255),

City varchar(255)

)

--多字段

CREATE TABLE Persons(

Id_P int NOT NULL,

LastName varchar(255) NOT NULL,

FirstName varchar(255),

Address varchar(255),

City varchar(255),

CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)

)

--添加

ALTER TABLE Persons

ADD UNIQUE (Id_P)

--添加

ALTER TABLE Persons

ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)

--删除

ALTER TABLE Persons

DROP CONSTRAINT uc_PersonID

默认值约束 DEFAULT

CREATE TABLE table_name(

    id int IDENTITY(1,1),

    NAME nvarchar(50) not null,

    sex char(2) default '',

    note ntext null

)

指定列的默认值

--创建一个默认值对象

create default sdept_char

as '信息中心'

go

--为表sdept列绑定默认值

sp_bindefault sdept_char,'student.sdept'

--测试绑定的默认值

insert student(sno,sname,ssex) values('001','类鳄梨','')

select * from student where sno='001'

--取消默认值绑定

sp_unbindefault 'student.sdept'

--测试取消是否成功

insert student(sno,sname,ssex) values('002','雷海鸣','')

select * from student

--删除默认值对像

drop default sdept_char

检查约束 CHECK

指定列的允许值,指定根据同一个表中其他列的值可在列中接受的数据值

--为表年龄字段添加约束

alter table student

add constraint ck_sage check(sage>=0 and sage<=100)

--测试约束

update student set sage=102 where sno=001

--删除表年龄字段约束

alter table student

drop constraint ck_sage

--测试

update student set sage=102 where sno=001
非空约束NOT NULL
指定是否允许为NULL字符

CREATE TABLE table_name(

    id int IDENTITY(1,1),

    NAME nvarchar(50) not null,

    sex char(2) default '',

    note ntext null

)

规则 RULE

--创建规则对象

create rule format

as

@联系电话 like '[0][1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

--修改表添加列

alter table student

add telephone char(12)

--为列绑定规则

sp_bindrule format,'student.telephone'

--测试绑定规则

update student set telephone='00001234567' where sno='001'

update student set telephone='01001234567' where sno='002'

select * from student

--解除绑定规则

sp_unbindrule 'student.telephone'

--测试解除

update student set telephone='00001234567' where sno='001'

select * from student

--删除规则对象

drop rule format

外键约束 FOREIGN KEY

指定必须存在值得列,定义值与同一个表或另一个表的主键值匹配的一列或多列组合

--创建表时添加外键

create table table_name(

    id int primary key,

    id1 int foreign key  )

--已有表添加约束

alter table table_name

add

constraint fk_name

foreign key(columne_name)

--删除外键

alter table table_name

drop

constraint fk_name

 

 

 

 

 

转载于:https://www.cnblogs.com/Aha-Best/p/10857489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值