日期:2014-05-17  浏览次数:20426 次

创建表之后,怎样对表的某个字段再创建结束
表已经创建完成了,但后来,想再对表中的某个字段加一些检查约束,
怎样创建呢(通过SQL语句)?

非常感谢!

------解决方案--------------------
alter table tb add check(id>0)
------解决方案--------------------
alter table 表名
add constraint CK_约束名 check(表达式)
------解决方案--------------------
如果是加条件约束:ALTER TABLE <表名>
                  ADD <条件约束>(列名)
比如在A表中的B列字段加上不能为空的条件:ALTER TABLE A
                                        ADD NOT NULL(B)
如果是完整性约束就如楼上所说。
------解决方案--------------------

--创建测试表dbo.dny
create table dbo.dny(a int,b int,c int,d int,e int)


--添加not null约束
alter table dbo.dny alter column a int not null


--添加唯一约束
alter table dbo.dny add constraint uq_b unique (b)


--添加主键约束
alter table dbo.dny add constraint pk_a primary key (a)


--添加检查约束
alter table dbo.dny add constraint ck_c check(c between 0 and 100)


--添加外键约束
create table dbo.emz(f int constraint pk_emz primary key (f))

alter table dbo.dny add constraint fk_d foreign key (d) references dbo.emz(f)