日期:2014-05-16  浏览次数:20704 次

建表时,怎么让一个列的取值必须在另一个列所有取值范围内?
rt,比如说我想建一个课程表,有列课程ID,课程名称,先行课。那么我要约束先行课必须在课程ID里取值。。。
------解决方案--------------------
加个check约束

------解决方案--------------------
create table tb
(id int not null,
name varchar(10) not null,
pid int null,
constraint PK_tb primary key nonclustered (id)
)
go
alter table tb
   add constraint FK_self foreign key (pid)
      references tb (id)
go
alter table tb
add constraint CHK_tb CHECK(pid<>id)