创建表(主键、唯一键、约束、默认值)

use TestDB
go
create table student
(
    sno int primary key,
    sname char(10) not null,
    gender char(2),
    age tinyint,
    smobile char(11),
    semail varchar(50)
)

--标识主键格式
constraint PK_sno primary key clustered(sno)
--查看主键信息
select * from sys.objects where type='PK'
--为已存在的表添加主键
alter table student add constraint PK_sno primary key clustered(sno)
--删除主键
alter table student drop constraint PK_sno
--唯一键格式
constraint UQ_NAME unique nonclustered(sname)

--查看唯一键信息
select * from sys.objects where type='UQ'
--为已存在的表添加唯一键
alter table student add constraint UQ_mobile unique nonclustered(smobile)
--删除唯一键
alter table student drop constraint UQ_mobile
--检查约束格式
constraint CK_sex check (sex in('男','女'))
--查看check约束信息
select * from sys.objects whrer type='C'
--为已存在的表添加Check
alter table sudent add constraint CK_sex check (sex in('男','女'))
--删除check约束
alter table student drop constraint CK_sex
--查看default信息
select * from sys.objects where type='D'
--为已存在的表添加default
alter table student add constraint DF_name default 'Alice' for sname
--删除default
alter table student drop constraint DF_name

go

发布了6 篇原创文章 · 获赞 0 · 访问量 67

猜你喜欢

转载自blog.csdn.net/qiuzhimin0/article/details/104279348