| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 760 人关注过本帖
标题:求助建库的问题
只看楼主 加入收藏
antonio_jiao
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-6
收藏
 问题点数:0 回复次数:0 
求助建库的问题
use master
if exists (select * from sysdatabases where name='bbsDB')
drop database bbsDB
go
exec xp_cmdshell 'mkdir d:\project'
create database bbsDB
on
(
name='bbsDB_db',
filename='d:\project\bbsDB_db.mdf',
size=10,
filegrowth=10%
)
log on
(
name='bbsDB_log',
filename='d:\project\bbsDB_log.ldf',
size=1,
filegrowth=10%,
maxsize=20
)

if exists(select * from sysobjects where name='bbsUsers')
alter table bbsSection
drop constraint FK_UID
alter table bbsUsers
drop constraint FK_UID
alter table bbsReply
drop constraint FK_UID

drop table bbsUsers
Go
create table bbsUsers
(
UID INT identity(1,1) NOT NULL,
Uname VARCHAR(15) NOT NULL,
Upassword varchar(10) not null,
Uemail varchar(20),
Usex Bit not null,
UClass int,
Uremark varchar(20),
UregDate datetime not null,
Ustate Int null,
Upoint int null
)
go
alter table bbsUsers Add constraint PK_UID
PRIMARY KEY(UID)
alter table bbsUsers Add constraint DF_Upassword
default (888888) for Upassword
alter table bbsUsers Add constraint DF_Usex
default (1) for Usex
alter table bbsUsers add constraint DF_Uclass
default (1) for Uclass
alter table bbsUsers add constraint DF_UregDate
default (getDate()) for UregDate
alter table bbsUsers add constraint DF_Ustate
default(0) for Ustate
alter table bbsUsers add constraint DF_Upoint
default (20) for Upoint
alter table bbsUsers add constraint CK_Uemail
check (Uemail like '%@%')
alter table bbsUsers add constraint CK_Upassword
check (len(Upassword)>=6)
go
if exists(select * from sysobjects where name='bbsSection')
alter table bbsSection
drop constraint fk_SID

alter table bbsReply
drop constraint fk_UID



drop table bbsSection
go
create table bbsSection
(
SID INT identity(1,1) not null,
Sname varchar(32) not null,
SmasterID int not null,
Sprofile varchar(20),
SclickCount int,
StopicCount int
)
go

alter table bbsSection add constraint PK_SID
Primary key(SID)
alter table bbsUsers add constraint FK_UID
foreign key(UID) references bbsSection(SmasterID)
alter table bbsSection add constraint D_STopicCount
default(0) for SclickCount
alter table bbsSection add constraint DStopicCount
default(0) for StopicCount
go

if exists(select * from sysobjects where name='bbsTopic')
alter table bbsTopic
drop constraint FK_TID


drop table bbsTopic
go
create table bbsTopic
(
TID int identity(1,1)not null,
TsID int not null,
TuID int not null,
TreplyCount int ,
Tface int,
Ttopic varchar(20) not null,
Tcontents varchar(30) not null,
Ttime datetime ,
TclickCount int,
Tstate int not null,
TlastReply datetime
)
go
alter table bbsTopic Add constraint PK_TID
PRIMARY KEY(TID)
alter table bbsSection
add constraint fk_SID
foreign key(SID) references bbsTopic(TsID)
alter table bbsUsers
add constraint FK_UID
foreign key(UID) references bbsTopic(TuID)
alter table bbsTopic
add constraint Df_TreplyCount
default(0) for TreplyCount
alter table bbsTopic
add constraint ck_Tcontents
check(len(Tcontents)>6)
alter table bbsTopic
add constraint Df_Ttime
default(getDate()) for Ttime
alter table bbsTopic
add constraint Df_TclickCount
Default(0) for TclickCount
alter table bbsTopic
add constraint Df_Tstate
default(0) for Tstate
go

if exists(select * from sysobjects where name='bbsReply')


drop table bbsReply
go
create table bbsReply
(
RID int identity(1,1) not null,
RtID int not null,
RsID int not null,
RuID int not null,
Rface int,
Rcontents varchar(30) not null,
Rtime datetime,
RclickCount int,
)
go
alter table bbsReply add constraint PK_RID
primary key(RID)
alter table bbsTopic add constraint FK_TID
foreign key(TID) references bbsReply(RtID)
alter table bbsSection add constraint FK_SID
foreign key(SID) references bbsReply(RsID)
alter table bbsUsers add constraint FK_UID
foreign key(UID) references bbsReply(RuID)
alter table bbsReply add constraint D_Rcontents
check(len(Rcontents)>6)
alter table bbsReply add constraint D_Rtime
default(getdate()) for Ttime
go
如上代码,验证语法可以通过,但是一运行就报错
正在删除数据库文件 'd:\project\bbsDB_log.ldf'。
正在删除数据库文件 'd:\project\bbsDB_db.mdf'。
(所影响的行数为 2 行)
CREATE DATABASE 进程正在磁盘 'bbsDB_db' 上分配 10.00 MB 的空间。
CREATE DATABASE 进程正在磁盘 'bbsDB_log' 上分配 1.00 MB 的空间。
服务器: 消息 3728,级别 16,状态 1,行 22
'FK_UID' 不是约束。
服务器: 消息 3727,级别 16,状态 1,行 22
未能除去约束。请参阅前面的错误信息。
服务器: 消息 2714,级别 16,状态 6,行 1
数据库中已存在名为 'bbsUsers' 的对象。
服务器: 消息 1779,级别 16,状态 1,行 2
已在表 'bbsUsers' 上定义了主键。
服务器: 消息 1750,级别 16,状态 1,行 2
未能创建约束。请参阅前面的错误信息。
服务器: 消息 3728,级别 16,状态 1,行 3
'fk_SID' 不是约束。
服务器: 消息 3727,级别 16,状态 1,行 3
未能除去约束。请参阅前面的错误信息。
服务器: 消息 2714,级别 16,状态 6,行 1
数据库中已存在名为 'bbsSection' 的对象。
服务器: 消息 1779,级别 16,状态 1,行 3
已在表 'bbsSection' 上定义了主键。
服务器: 消息 1750,级别 16,状态 1,行 3
未能创建约束。请参阅前面的错误信息。
服务器: 消息 3728,级别 16,状态 1,行 6
'FK_TID' 不是约束。
服务器: 消息 3727,级别 16,状态 1,行 6
未能除去约束。请参阅前面的错误信息。
服务器: 消息 2714,级别 16,状态 6,行 1
数据库中已存在名为 'bbsTopic' 的对象。
服务器: 消息 1779,级别 16,状态 1,行 2
已在表 'bbsTopic' 上定义了主键。
服务器: 消息 1750,级别 16,状态 1,行 2
未能创建约束。请参阅前面的错误信息。
服务器: 消息 1776,级别 16,状态 1,行 4
在被引用表 'bbsReply' 中没有与外键 'FK_TID' 的引用列的列表匹配的主键或候选键。
服务器: 消息 1750,级别 16,状态 1,行 4
未能创建约束。请参阅前面的错误信息。


请帮我看看,大概外键的删除哪里错了,先谢过了哈

[此贴子已经被作者于2007-9-20 23:18:16编辑过]

搜索更多相关主题的帖子: database master create where 
2007-09-20 23:17
快速回复:求助建库的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018121 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved