use master
go
/*--检查是否存在bbsDB数据库,查询master数据库中的系统表sysdatabases--*/
if exists(select * from sysdatabases where name='bbsDB')
drop database bbsDB
go
exec xp_cmdshell 'mkdir d:porject' --调用DOS命令创建文件夹
/*--建库bbsDB--*/
create database bbsDB
on primary
(
name='bbsDB_data',
filename='D:\porject\bbsDB_data.mdf',
size=10 mb,
filegrowth=20%
)
log on
(
name='bbsDB_log',
filename='D:\porject\bbsDB_log.ldf',
size=1mb,
maxsize=20mb,
filegrowth=10%
)
go
use bbsDB
/*--检查是否存在bbsUsers表--*/
if exists(select * from sysobjects where name='bbsUsers')
drop table bbsUsers
/*--bbsUsers表里的字段--*/
go
create table bbsUsers
(
UID int identity(1,1) not null,
Uname varchar(15) not null,
Upassword varchar(10),
Uemail varchar(20),
Usex Bit not null,
Uclass int ,
Ure varchar(20),
UregDate datetime not null,
Ustate int null,
Upoint int null
)
go
select * from bbsUsers
go
/*---添加约束---*/
/*--UID主键约束--*/
alter table bbsUsers 
Add constraint PK_UID
primary key (UID)
/*--Upassword默认约束--*/
alter table bbsUsers 
add constraint DF_
default (888888) for Upassword
/*--Usex默认约束--*/
alter table bbsUsers 
add constraint DF_Usex
default (1) for Usex
/*--UregDate默认当前系统时间--*/
alter table bbsUsers 
add constraint DF_UregDate
default (getdate()) for UregDate
/*--Ustate默认约束--*/
alter table bbsUsers 
add constraint DF_Ustate
default (0) for Ustate
/*--Upoint默认约束--*/
alter table bbsUsers 
add constraint DF_Upoint
default (20) for Upoint
/*--Uemail检查约束--*/
alter table bbsUsers  
add constraint CK_Uemail
check (Uemail like '%@%')
/*--Upassword检查约束--*/
alter table bbsUsers 
add constraint CK_Upassword
check (len(Upassword)>=6)
go
/*--插入数据进行测试--*/
insert into bbsUsers(Uname,Upassword,Uemail,Ure,Upoint)
values('可','HYXS007','[email protected]','我要',50)
select * from bbsUsers
go
use bbsDB
go
/*--检查是否存在bbsSection表--*/
if exists(select * from sysobjects where name='bbsSection')
drop table bbsSection
go
/*--添加bbsSection字段--*/
create table bbsSection
(
SID int identity(1,1) not null, 
Sname varchar(32) not null, --版块名称
SmasterID int not null,
Sprofile varchar(20),
StopicCount int 
)
go/*--SID主键约束--*/
alter table bbsSection 
Add constraint PK_SID
primary key (SID)
/*--bbsUsers表里的UID关联到bbsSection表里的SmasterID--*/
alter table bbsUsers 
add constraint FK_UID
foreign key (UID) references bbsSection(SmasterID)
/*--SclickCount默认约束--*/
alter table bbsSection 
add constraint DF_SclickCount
default (0) for  SclickCount
/*--StopicCount默认约束--*/
alter table bbsSection 
add constraint DF_StopicCount 
default (0) for StopicCount
go
/*--插入数据进行测试--*/
insert into bbsSection(Sname,SmasterID,Sprofile)
values('可',1,'我要')
select * from bbsSection
go
use bbsDB
go
/*--检查是否存在bbsTopic表--*/
if exists(select * from sysobjects where name='bbsTopic')
drop table bbsTopic
go
/*--bbsTopic表里的字段--*/
create table bbsTopic
(
TID int identity(1,1) not null, 
TsID int not null, 
TuID int,
TreplyCount int ,
Tface varchar(20) not null,
Tcontents varchar(30) not null,
Ttime datetime,
TclickCount int,
Tstate int not null,
TlastReply datetime
)
go
/*--TID主键约束--*/
alter table  bbsTopic
Add constraint PK_TID
primary key (TID)
/*--bbsSection表里的SID关联到bbsTopic表里的TsID--*/
alter table bbsSection
add constraint FK_SID 
foreign key (SID) references  bbsTopic(TsID)
/*--bbsUsers表里的UID关联到bbsTopic表里的TuID--*/
alter table bbsUsers 
add constraint FK_UID 
foreign key  (UID) references  bbsTopic(TuID)
/*--TreplyCount默认约束--*/
alter table bbsTopic 
add constraint DF_TreplyCount
default (0) for TreplyCount
/*--Tcontents检查约束--*/
alter table bbsTopic 
add constraint CK_Tcontents
check (len(Tcontents)>=6)
/*--Ttimet默认约束--*/
alter table bbsTopic 
add constraint DF_Ttime
default (getdate()) for Ttime
/*--TclickCount默认约束--*/
alter table bbsTopic 
add constraint DF_TclickCount
default (0) for TclickCount
/*--Tstate默认约束--*/
alter table bbsTopic 
add constraint DF_Tstate
default (1) for Tstate
/*--TlastReply检查约束--*/
alter table bbsTopic 
add constraint CK_TlastReply
check(TlastReply>Ttime)
go
/*--检查是否存在bbsPeply表--*/
if exists(select * from sysobjects where name='bbsPeply')
drop table bbsPeply
go
/*--bbsPeply表里的字段--*/
create table bbsPeply
(
RID int  not null, 
RtID int not null, 
RsID int not null,
Rface int ,
Rcontente varchar(30) not null,
Rtime datetime,
RclickCount int
)
go
/*--bbsTopic表里的TID关联到bbsPeply表里的RtID--*/
alter table bbsTopic 
add constraint FK_TID 
foreign key  (TID) references  bbsPeply(RtID)
/*--bbsSection表里的SID关联到bbsPeply表里的RsID--*/
alter table bbsSection 
add constraint FK_SID
foreign key  (SID) references  bbsPeply(RsID)
/*--bbsUsers表里的UID关联到bbsPeply表里的RuID--*/
alter table bbsSection 
add constraint FK_UID
foreign key  (UID) references  bbsPeply(RuID)
/*--Rface检查约束--*/
alter table bbsPeply 
add constraint CK_Rface
check (len(Rface)>=6)
/*--默认约束--*/
alter table bbsPeply
add constraint DF_Rtime
default (getdate()) for Rtime************************************************
错误提示
(所影响的行数为 2 行)CREATE DATABASE 进程正在磁盘 'bbsDB_data' 上分配 10.00 MB 的空间。
CREATE DATABASE 进程正在磁盘 'bbsDB_log' 上分配 1.00 MB 的空间。(所影响的行数为 0 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)服务器: 消息 1776,级别 16,状态 1,行 7
在被引用表 'bbsSection' 中没有与外键 'FK_UID' 的引用列的列表匹配的主键或候选键。
服务器: 消息 1750,级别 16,状态 1,行 7
未能创建约束。请参阅前面的错误信息。(所影响的行数为 1 行)
(所影响的行数为 1 行)服务器: 消息 1776,级别 16,状态 1,行 6
在被引用表 'bbsTopic' 中没有与外键 'FK_SID' 的引用列的列表匹配的主键或候选键。
服务器: 消息 1750,级别 16,状态 1,行 6
未能创建约束。请参阅前面的错误信息。
服务器: 消息 1776,级别 16,状态 1,行 2
在被引用表 'bbsPeply' 中没有与外键 'FK_TID' 的引用列的列表匹配的主键或候选键。
服务器: 消息 1750,级别 16,状态 1,行 2
未能创建约束。请参阅前面的错误信息。

解决方案 »

  1.   

    /*--插入数据进行测试--*/
    insert into bbsUsers(Uname,Upassword,Uemail,Ure,Upoint)
    values('可','HYXS007','[email protected]','我要',50)
    select * from bbsUsers
    go
    在被引用表   'bbsSection'   中没有与外键   'FK_UID'   的引用列的列表匹配的主键或候选键。从以上信息看,你插入的数据中,受到外键约束.
    考虑先在外键表插入数据,然后再插入主表的数据.