create database bbs;
use bbs
go
--用户表
if exists(select * from sysobjects where name = 'userInfo')
drop table userInfo
go
create table userInfo
(
uId int identity (1,1), --用户id【主键】
uName nvarchar(10) not null, --用户名【唯一约束】
uPassWord nvarchar(20) not null, --用户密码
uSex bit not null, --用户性别
uFace nvarchar(5) not null, --用户头像路径
uType int not null, --用户类型【默认为0普通会员】
--禁言用户:-1、普通会员:0、版主:1、管理员:2
uRegTime datetime not null --用户注册时间【默认为当前时间】
)
go
--版块表
if exists(select * from sysobjects where name ='sectionInfo' )
drop table sectionInfo
create table sectionInfo
(
sId int identity(1,1), --版块id【主键】
sName nvarchar(20) not null, --版块名
sMasterId int not null, --版主id【默认为0】
sTopicCount int not null, --帖子数量【默认为0】
sParentId int not null --父版块【默认为0】
)
go
--主帖表
if exists(select * from sysobjects where name ='topicInfo')
drop table topicInfo
create table topicInfo(
tId int identity(1,1), --主帖id【主键】
tSId int not null, --所在版块id【外键】
tUId int not null, --发帖用户id【外键】
tTopic nvarchar(20) not null, --帖子标题
tContents nvarchar(max) not null, --主帖内容
tReplyCount int not null, --回复数量【默认为0】
tClickCount int not null, --点击数量【默认为0】
tPublishTime datetime not null, --发帖时间【默认为当前时间】
tModifyTime datetime --修改时间
)
go
--跟帖表
if exists(select * from sysobjects where name = 'replyInfo')
drop table replyInfo
create table replyInfo(
rId int identity(1,1), --跟帖id【主键】
rTId int not null, --回复的主帖id【外键】
rSId int not null, --跟帖所在版块id【外键
rUId int not null, --跟帖人id【外键】
rTopic nvarchar(20), --跟帖主题
rContents nvarchar(max) not null, --跟帖内容
rPublishTime datetime not null,--跟帖时间【默认为当前时间】
rModifyTime datetime --修改时间
)
go
--论坛短信表
if exists(select * from sysobjects where name = 'messageInfo')
drop table messageInfo
create table messageInfo(
mId int identity(1,1), --短信id【主键】
mSendUnameId int not null, --短信的发送者id【外键】
mReceiveUnameId int not null, --短信的接收者id【外键】
mTopic nvarchar(20) not null, --短信主题
mNote nvarchar(100) not null, --短信内容
mPostTime datetime not null,--短信发送时间【默认为当前时间】
mReadSign bit not null, --【默认为false未读】
)
//表约束条件
use bbs
go
--用户表约束
alter table userInfo
add
constraint PK_uId primary key (uId),
constraint UQ_uName unique(uName),
constraint DF_uType default(0) for uType,
constraint DF_uRegTime default(getdate()) for uRegTime
go
--版块表约束
alter table sectionInfo
add
constraint PK_sId primary key(sId),
constraint DF_sMasterId default(0) for sMasterId,
constraint DF_sTopicCount default(0) for sTopicCount,
constraint DF_sParentId default(0) for sParentId
go
--发帖表
alter table topicInfo
add
constraint PK_tId primary key(tId),
constraint FK_tSId foreign key(tSId) references sectionInfo(sId),
constraint FK_tUId foreign key(tUId) references userInfo(uId),
constraint DF_tReplyCount default(0) for tReplyCount,
constraint DF_tClickCount default(0) for tClickCount,
constraint DF_tPublishTime default(getDate()) for tPublishTime
go
--跟帖表
alter table replyInfo
add
constraint PK_rId primary key(rId),
constraint FK_rTId foreign key(rTId) references topicInfo(tId),
constraint FK_rSId foreign key(rSId) references sectionInfo(sId),
constraint FK_rUId foreign key(rUId) references userInfo(uId),
constraint DF_rPublishTime default(getdate()) for rPublishTime
go
--短信表
alter table messageInfo
add
constraint PK_mId primary key(mId),
constraint FK_mSendUnameId foreign key(mSendUnameId) references userInfo(uId),
constraint FK_mReceiveUnameId foreign key(mReceiveUnameId) references userInfo(uId),
constraint DF_mPostTime default(getdate()) for mPostTime,
constraint DF_mReadSign default(0) for mReadSign
go
//初始化表数据
--插入一个管理员
set IDENTITY_INSERT userInfo on;
insert into userInfo (uId,uName,uPassWord,uSex,uFace,uType,uRegTime)
values(8,'cmmm','cmmm',1,'1.gif',2,'2010-4-23');
select * from userInfo;
--设置一个主板块
set IDENTITY_INSERT sectionInfo on;
insert into sectionInfo(sId,sName,sMasterId,sTopicCount,sParentId)
values(75,'web技术',0,1,0);
select * from sectionInfo;
--插入一个子板块
set IDENTITY_INSERT sectionInfo on;
insert into sectionInfo(sId,sName,sMasterId,sTopicCount,sParentId)
values(76,'web技术大全',0,0,75);
set IDENTITY_INSERT sectionInfo off;
--插入一个主题
set IDENTITY_INSERT userInfo off;
set IDENTITY_INSERT topicInfo on
insert into topicInfo(tId,tSId,tUId,tTopic,tContents,tReplyCount,tClickCount,tPublishTime,tModifyTime)
values(56,76,8,'tomcat技术','tomcat是开源的小型服务器,很好用哦',1,0,'2010-04-20 16:37:46.013','2010-04-22 16:00:00.000')
select * from topicInfo;
--插入一个回复
set IDENTITY_INSERT topicInfo off;
set IDENTITY_INSERT replyInfo on;
insert into replyInfo(rId,rTId,rSId,rUId,rTopic,rContents,rPublishTime,rModifyTime)
values(77,56,76,8,'真的耶','我用过,真的很好用,哈哈','2010-04-18 20:16:15.577','2010-04-20 12:16:15.567');
select * from replyInfo;
--插入消息
set IDENTITY_INSERT replyInfo off;
set IDENTITY_INSERT messageInfo on;
set IDENTITY_INSERT messageInfo off;
insert into messageInfo(mId,mSendUnameId,mReceiveUnameId,mTopic,mNote,mPostTime,mReadSign)
values(2,1,8,'管理员的邀请','现在正式聘请你成为本bbs的管理员','2010-04-20 16:37:46.013',0);
select * from messageInfo;