USE test create table stu ( stu_id int primary key, stu_name nvarchar(50) not null, stu_sex nchar(2) , stu_address nvarchar(200) ) --添加检查约束(CHECK简称CK) alter table stu add constraint CK_SEX check(stu_sex='男' or stu_sex='女') go --添加默认值约束(DEFAULT简称DF) alter table stu add constraint DF_ADDRESS default('江西省') for stu_address go --添加主键约束 alter table stu add constraint PK_ID primary key(主键字段) go --添加外键约束 alter table 从表 add constraint FK_从表_主表 foreign key 从表(公共字段) references 主表(公共字段) go --删除所建约束 alter table stu drop constraint DF_ADDRESS go alter table stu drop constraint CK_SEX go if exists(select * from sysobjects where name='stu1') drop table stu1 create table stu1 ( stu_id int primary key, stu_name nvarchar(50) not null, stu_sex nchar(2) check(stu_sex='男' or stu_sex='女'), stu_address nvarchar(200) default('江西省') ) ========================= 中括号的用法[] --1.或的意思:查找姓张或姓李的人 select * from stu where name like '[张李]%' --2.区域:查找学号在1-3之间的人(1,2,3) select * from stu where id like '[1-3]' --3.学号不是1,2,3的人 select * from stu where id like '[^1-3]'