use master
print '创建数据库BBS'
--if exists(select database from sysdatabase)
if DB_id('BBS') > 0
drop database BBS
go
create database BBS
on primary
(
name = 'BBS_data',
filename = 'D:\BBS_data.mdf',
size = 4mb,
maxsize = 100mb,
filegrowth = 10%
)
log on
(
name = 'BBS_log',
filename = 'D:\BBS_log.ldf',
size = 3mb,
maxsize = 10mb
)
go
print '数据库创建完成!建表'
use BBS
go
if Object_id('TBL_USER') > 0
drop table TBL_USER
go
print '创建TBL_USER表'
create table TBL_USER
(
uId int identity(1,1) not null,
uName varchar(20) not null, --用户名
uPass varchar(20) not null, --密码
head varchar(100) not null, --头像
regTime datetime not null, --注册时间
gender smallint not null --1为女;2为男
)
go
if Object_ID('TBL_BOARD') > 0
drop table TBL_BOARD
go
print '创建TBL_BOARD表'
create table TBL_BOARD
(
boardId int identity(1, 1) not null,
boardName varchar(50) not null,
parentId int not null
)
go
print '创建TBL_TOPIC表'
if object_id('TBL_TOPIC') > 0
drop table TBL_TOPIC
go
create table TBL_TOPIC
(
topicId int identity(1,1) not null,
title varchar(50) not null,
contents varchar(1000) not null,
publishTime datetime not null,
modityTime datetime not null,
uId int not null,
boardId int not null
)
go
if object_id('TBL_REPLY') > 0
drop table TBL_REPLY
go
print '创建TBL_REPLE表'
create table TBL_REPLY
(
replyId int identity(1,1) not null,
title varchar(50) not null,
contents varchar(1000) not null,
publishTime datetime not null,
modityTime datetime not null,
uId int not null,
topicId int not null
)
go
print '添加约束'
--唯一约束
alter table TBL_USER add constraint UQ_uName unique (uName)
--主键约束
alter table TBL_USER add constraint PK_uId primary key (uId)
alter table TBL_BOARD add constraint PK_boardId primary key (boardId)
alter table TBL_TOPIC add constraint PK_topicId primary key (topicId)
alter table TBL_REPLY add constraint PK_replyId primary key (replyId)
--检查约束
alter table TBL_USER add constraint CK_gender check (gender between 1 and 2)
--外键约束
alter table TBL_TOPIC add constraint FK_uId foreign key(uId) references TBL_USER(uId)
alter table TBL_REPLY add constraint FK_uId1 foreign key(uId) references TBL_USER(uId)--七个不一样的名字
alter table TBL_REPLY add constraint FK_topicId foreign key(topicId) references TBL_TOPIC(topicId)
alter table TBL_TOPIC add constraint FK_boardId foreign key(boardId) references TBL_BOARD(boardId)
go
print '插入数据'
print 'TBL_USER表:'
insert into TBL_USER(uName, uPass, head, regTime, gender) values('威廉', 'welling', '海盗', getdate(), 1)
insert into TBL_USER(uName, uPass, head, regTime, gender) values('海莉丝', 'hailis', '海盗女王', getdate(), 2)
insert into TBL_USER(uName, uPass, head, regTime, gender) values('张三', 'xiaoSan', '小三', getdate(), 1)
insert into TBL_USER(uName, uPass, head, regTime, gender) values('李四', 'Lisin', '船长', getdate(), 1)
insert into TBL_USER(uName, uPass, head, regTime, gender) values('王五', 'wangwu', '商人', getdate(), 1)
print 'TBL_BOARD表:'
insert into TBL_BOARD(boardName, parentId) values('Java', 1)
insert into TBL_BOARD(boardName, parentId) values('C#', 2)
insert into TBL_BOARD(boardName, parentId) values('Microsoft', 3)
insert into TBL_BOARD(boardName, parentId) values('HTML', 4)
insert into TBL_BOARD(boardName, parentId) values('娱乐', 5)
print 'TBL_TOPIC表:'
insert into TBL_TOPIC(title, contents, publishTime, modityTime, uId, boardId) values('Java技术', 'java有没有抽象工厂模式啊', getdate(), getdate(), 1, 1)
insert into TBL_TOPIC(title, contents, publishTime, modityTime, uId, boardId) values('docNET技术交流', '这个工具能不能做游戏啊', getdate(), getdate(), 2, 2)
insert into TBL_TOPIC(title, contents, publishTime, modityTime, uId, boardId) values('微软', '盖茨比很有钱,因为微软很能赚钱', getdate(), getdate(), 3, 3)
insert into TBL_TOPIC(title, contents, publishTime, modityTime, uId, boardId) values('JSP', '动态网页与静态网页各有所长', getdate(), getdate(), 4, 4)
insert into TBL_TOPIC(title, contents, publishTime, modityTime, uId, boardId) values('游戏', '最近有一款名为“龙之舞”的游戏开始公测了', getdate(), getdate(), 5, 5)
print 'TBL_REPLY表:'
insert into TBL_REPLY(title, contents, publishTime, modityTime, uId, topicId) values('靠自己', '这个问题应该自己研究,不应该事事依赖别人啊', getdate(), getdate(), 1, 1)
insert into TBL_REPLY(title, contents, publishTime, modityTime, uId, topicId) values('解决', '当然可以了,你可以自己试一下,不会的话去北大青鸟去学啊', getdate(), getdate(), 2, 2)
insert into TBL_REPLY(title, contents, publishTime, modityTime, uId, topicId) values('money', '我要是比尔盖茨就好了,是他儿子也行啊', getdate(), getdate(), 3, 3)
insert into TBL_REPLY(title, contents, publishTime, modityTime, uId, topicId) values('动态', '就体现在‘动态’两个字上,可以连接数据库', getdate(), getdate(), 4, 4)
insert into TBL_REPLY(title, contents, publishTime, modityTime, uId, topicId) values('真的吗', '这个游戏是那个公司的啊,怎么样啊', getdate(), getdate(), 5, 5)
go
print '查看:'
select * from TBL_USER
select * from TBL_BOARD
select * from TBL_TOPIC
select * from TBL_REPLY