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)
alter table 课程表 add constraint check1 check(先行课<=课程ID)
比如说,课程ID取值:C801,C802,C803,C804,C804。那么先行课取值就必须在(C801,C802,C803,C804,C804)内
(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)