如何在建表时就填约束?

来源:百度知道 编辑:UC知道 时间:2024/05/27 00:03:47
use bbsDB --建立板块表
if exists(select * from sysobjects where name='bbsSection')
drop table bbsSection
create table bbsSection -------能在建表时就创建约束吗?我觉得如果一个表如果约束不太多的话,比如就一个主键约束,想知道怎样能在定义数据类型是就添加约束
(
SID int identity(1,1) not null,
Sname varchar(32) not null,
SmasterID int not null,
Sprofile varchar(50),
SclickCount int,
StopicCount int
)
go
select * from bbsSection
go
/*--添加约束--*/
alter table bbsSection add constraint PK_SID primary key(SID)
alter table bbsSection add constraint DF_SclickCount default(0) for SclickCount
alter table bbsSection add constraint DF_StopicCount default(0) for StopicCount
alter table bbSSection add constraint FK_SmasterID foreign key (SmasterID)
references bbsUsers(UID)
go

create table bbsSection
(
SID int identity(1,1) not null constraint PK_SID primary key,
Sname varchar(32) not null,
SmasterID int not null constraint FK_SmasterID foreign key references bbsUsers(UID),
Sprofile varchar(50),
SclickCount int constraint DF_SclickCount default 0,
StopicCount int constraint DF_StopicCount default 0
)