BBS论坛数据库设计代码use mastergo----------------------------------------新建数据bbsDB的库---------------------------------------------------------判断数据库是否存在使用exists关键字,在用sysdatabases关键字查询--if exists(select * from sysdatabases where name = 'bbsDB')--如果数据库存在就删除此数据库用drop(删除)database(数据库对象)bbsDB (数据库名)--drop database bbsDB--删除后在添加一个数据库create(添加)database(数据库对象)bbsDB(数据库名)on--create database bbsDBon(name = 'bbsDB_MDF', --数据库主文件名--filename = 'F:\SQL\第四阶段\bbsDB_MDF.mdf', --数据库存储的路径--size = 10mb, --数据库初使大小--filegrowth = 10% --增长变化--)--设置日志文件--log on(name = 'bbsDB_LDF', --日志文件名--filename = 'F:\SQL\第四阶段\bbsDB_LDF.ldf', --日志文件存储路径--size = 1mb, --日志文件初使大小--maxsize = 20mb, --日志文件最大的可储存--filegrowth = 10% --增长变化--)go---------------------------------------以上信息为新建的数据库-------------------------------------------------use bbsDBgo---------------------------------------新建bbsUsers表(用户表)---------------------------------------------------判断bbsDB库是否有表bbsUser的存在exists关键字+sysobjects判断当前数据库表--if exists(select * from sysobjects where name='bbsUsers')--如果表存在就删除此表--drop table bbsUser--新建bbsUser表--create table bbsUsers(UID int identity(1,1) not null, --用户编号,自动增长列,不允许空--Uname varchar(50) not null, --用户名,不允许空--Upassword varchar(50) not null, --密码,不允许空--Uemail varchar(50), --邮箱--Usex char(2) not null, --性别,不允许空--Uclass int, --用户等级--Uremark varchar(50), --备注信息--UregDate datetime not null, --注册日期,不允许空--Ustate int, --状态,是否在线等--Upoint int, --用户的积分--)go--------------------------------------为用户表添加各种约束----------------------------------------------------------为bbsUser表添加约束,add constraint关键字PK_UID约束名,primary key (主键)设置UID字段为主键--alter table bbsUsersadd constraint PK_UID primary key(UID)--为Upassword添加约束,check(Upassword length>=6)字段长度大于等于6位,default设置默认值为888888--alter table bbsUsersadd constraint CK_Upassword check(len(Upassword) >= 6)alter table bbsUsersadd constraint DF_Upassword default('888888') for Upassword--为Uemail字段添加约束,必须包含@号--alter table bbsUsersadd constraint CK_Uemail check(Uemail like '%@%')--为User字段添加约束,设置默认值为男,并且值只能是男或女--alter table bbsUsersadd constraint DF_Usex default('男') for Usexalter table bbsUsersadd constraint CK_Usex check(Usex='男' or Usex='女')--为Uclass字段添加约束,默认值为1--alter table bbsUsersadd constraint DF_Uclass default(1) for Uclass--为UregDate字段添加约束,默认值为当前日期--alter table bbsUsersadd constraint DF_UregDate default(getDate()) for UregDate--为Ustate添加约束,默认值为0--alter table bbsUsersadd constraint DF_Ustate default(0) for Ustate--为Upoint字段添加约束,默认值为20--alter table bbsUsersadd constraint DF_Upoint default(20) for Upoint-----------------------------------以上信息为以建立的bbsUser(用户表)及各种约束-----------------------------------go-----------------------------------新建bbsSection表(版块表)-------------------------------------------------------判断数据库内是否有表bbsSection的存在--if exists(select * from sysobjects where name = 'bbsSection')--如果有此表执行删除--drop table bbsSection--新建bbsSection表,及个字段--create table bbsSection(SID int identity(1,1) not null, --版块编号,自动增长列--Sname varchar(50) not null, --版块名称,不允许空--SmasterID int not null, --版主ID--Sprofile varchar(50), --版面简介--SclickCount int, --点击率--StopicCount int --发贴数--)-----------------------------------为表bbsSection(版块表)添加约束-----------------------------------------------go--将SID字段设置为主键--alter table bbsSectionadd constraint PK_SID primary key(SID)--为SmasterID设置外键,他的主键是bbsUsers表的UID字段foreign key(外键) references 主键表(主键表字段)--alter table bbsSectionadd constraint FK_SmasterID foreign key(SmasterID) references bbsUsers (UID)--为SclickCount字段添加默认值为0--alter table bbsSectionadd constraint DF_SclickCount default(0) for SclickCount--为字段StopicCount设置默认值为0--alter table bbsSectionadd constraint DF_StopicCount default(0) for StopicCount---------------------------------以上为表bbsSection(版块表)建立及添加相应约束-----------------------------------------------------------------------------新建bbsTopic表(主贴表)---------------------------------------------------go--判断数据库中是否有bbsTopic表的存在--if exists(select * from sysobjects where name = 'bbsTopic')--删除此表--drop table bbsTopic--新建bbaTopic表及个字段--create table bbsTopic(TID int identity(1,1) not null, --帖子编号--TsID int not null, --版块编号--TuID int not null, --发贴人ID--TreplyCount int, --回复数量--Tface int, --发贴表情--Ttopic varchar(50) not null, --标题--Tcontents varchar(50) not null, --正文--Ttime datetime, --发贴时间--TclickCount int, --点击数--Tstate int not null, --状态--TlastReply datetime --最后回复时间--)----------------------------------------为bbsTopic(主贴表)表个字段添加约束-----------------------------------------go--将TID字段设置为主键--alter table bbsTopicadd constraint PK_TID primary key(TID)--将TsID设置为外键,引用bbsSection表的主键SID字段--alter table bbsTopicadd constraint FK_TsID foreign key(TsID) references bbsSection (SID) --将TuID字段设置为外键盘,引用bbsUsers表的主键UID字段-- alter table bbsTopicadd constraint FK_TuID foreign key(TuID) references bbsUsers (UID) --设置TreplyCount字段的默认值为0--alter table bbsTopicadd constraint DF_TreplyCount default(0) for TreplyCount--为Tcontents字段添加约束,他的值必须大于等于6位--alter table bbsTopicadd constraint CK_Tcontents check(len(Tcontents) >= 6)--为Ttime字段添加默认值,为当前日期--alter table bbsTopicadd constraint DF_Ttime default(getDate()) for Ttime--设置TclickCount字段默认值为0--alter table bbsTopicadd constraint DF_TclickCount default(0) for TclickCount--设置Tstate字段默认值为1--alter table bbsTopicadd constraint DF_Tstate default(1) for Tstate--为TlastReply字段添加约束,最后回复时间必须要晚于发贴时间--alter table bbsTopicadd constraint CK_TlastReply check(TlastReply > Ttime)-----------------------------------------以上为bbsTopic(主贴表)表建立及建立个字段------------------------------------------------------------------------------新建表bbsReply(回帖表)及个字段--------------------------------------go--判断数据库中是否有bbsReply(回帖表)的存在--if exists(select * from sysobjects where name = 'bbsReply')--删除bbsReply表--drop table bbsReply--创建bbsReply(回帖)表及个字段--create table bbsReply(RID int identity(1,1) not null, --帖子编号--RtID int not null, --主贴ID--RsID int not null, --版块ID--RuID int not null, --回帖人ID--Rface int, --回帖表情--Rcontents varchar(50) not null, --正文--Rtime datetime, --回帖时间--RclickCount int --点击数--)-----------------------------------------为bbsReply(回帖)表个字段添加约束----------------------------------------go--设置RID字段为主键--alter table bbsReplyadd constraint PK_RID primary key(RID)--设置RtID字段为外键引用bbsTopic表的主键TID字段--alter table bbsReplyadd constraint FK_RtID foreign key(RtID) references bbsTopic(TID)--设置RsID为外键引用bbsSection表的主键SID--alter table bbsReplyadd constraint FK_RsID foreign key(RsID) references bbsSection(SID)--设置RuID为外键引用bbsUsers表的主键UID字段--alter table bbsReplyadd constraint FK_RuID foreign key(RuID) references bbsUsers(UID)--设置Rcontents字段必须大于6位--alter table bbsReplyadd constraint DF_Rcontents check(len(Rcontents) >= 6)--设置Rtime字段默认值为当前日期--alter table bbsReplyadd constraint DF_Rtime default(getDate()) for Rtime------------------------------------------------以上为创建bbsReply(回帖)表的内容---------------------------------------------------------------------------------------向表格中插入数据-----------------------------------------go--向bbsUsers(用户表插入数据)--insert intobbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,Ustate,Upoint) values('天天','123456','zhong@','女',2,'我们的家乡在希望的田野上',3,100)insert intobbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,Ustate,Upoint) values('可卡因','HYXS007','ss@','女',1,'我要去公安局自首',1,200) insert intobbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,Ustate,Upoint) values('心酸果冻','888888','yy@','男',2,'牵匹瘦马闯天涯',2,600)insert intobbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,Ustate,Upoint) values('冬篱儿','fangdong','bb@','女',3,'爱迷失在天堂',4,1200) insert intobbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,Ustate,Upoint) values('Supper','master','dd@','女',5,'BBS大斑竹',1,500)--向bbsSection(版块表插入数据)--insert into bbsSection(Sname,SmasterID,Sprofile,SclickCount,StopicCount) values('Java技术核心',3,'包含框架,开源',500,1)insert into bbsSection(Sname,SmasterID,Sprofile,SclickCount,StopicCount) values('.Net技术',5,'包含C#,ASP',800,1)insert into bbsSection(Sname,SmasterID,Sprofile,SclickCount,StopicCount) values('Linux/Unix社区',5,'包含系统维护运行',0,0)--向bbsTopic(主贴表)插入数据--insert intobbsTopic(TsID,TuID,TreplyCount,Tface,Ttopic,Tcontents,TclickCount,Tstate,T lastReply)values(1,3,3,1,'还是JAVA','JAVA文件如何读取',200,1,'2008-8-1')insert intobbsTopic(TsID,TuID,TreplyCount,Tface,Ttopic,Tcontents,TclickCount,Tstate,T lastReply)values(2,2,1,2,'.NET文件部署','项目包含、WINFROM',0,1,'2007-8-5')--向bbsReply(跟贴表)插入数据--insert into bbsReply(RtID,RsID,RuID,Rface,Rcontents,RclickCount)values(1,1,5,2,'JAVA中乱码问题怎么解决好?',100)insert into bbsReply(RtID,RsID,RuID,Rface,Rcontents,RclickCount)values(1,1,4,4,'你好你好我们我们',200)insert into bbsReply(RtID,RsID,RuID,Rface,Rcontents,RclickCount)values(1,1,4,4,'呵呵哈哈嘿嘿西西',200)insert into bbsReply(RtID,RsID,RuID,Rface,Rcontents,RclickCount)values(2,1,4,4,'JSP文件转换',200)insert into bbsReply(RtID,RsID,RuID,Rface,Rcontents,RclickCount)values(2,1,4,4,'啊大安定大安定',200)---------------------------------------------------以上为各表格插入测试数据------------------------------------------------------------------------------------------------查询数据库情况-------------------------------------------print 'SQL Server的版本是:'+@@versionprint 'SQL Server的服务器名称是:'+@@servername--------------------------------------------------------------------------------------------------------------------------------------------------------------------系统变量查询上一条语句错误--------------------------------------update bbsUsers set Upassword = '1234' where Uname = '天天'print '执行上条语句产生错误: '+convert(varchar(5),@@error) --输出上一条语句的错误信息----------------------------------------------------------------------------------------------------------------------------------------------------------------------查询用户天天的信息---------------------------------------------set nocount on --不打印影响的信息----查询个人信息--print '天天个人信息如下'select Uname,Uremark,Upoint from bbsUsers where Uname = '天天'--用变量存储ID查询信息--declare @id int --声明变量--select @id = UID from bbsUsers where Uname = '天天' --将bbsUsers表中的UID字段赋值给id变量--print '发贴情况如下'selectconvert(varchar(10),Ttime,111),TclickCount,Ttopic,Tcontents,TclickCount,Tco ntents from bbsTopic where TuID = @idprint '回贴情况如下'select convert(varchar(10),Rtime,111),RclickCount,Rcontents from bbsReplywhere RuID = @idif(@id > 30) --if判断--print '有权发贴' --条件为真输出--else --否则--print '无权发贴' --输出-----------------------------------------------------------------------------------------------------------------------------------------------------------查询心酸果冻用户发贴回帖显示级别---------------------------------------------声明变量用于存储用户ID--declare @uNameID int--声明变量用于存储用户发贴数量--declare @count int--声明变量用于存储用户回帖数量--declare @count1 int--将心酸果冻用户的ID号存到变量@uNameID中--select @uNameID = UID from bbsUsers where Uname = '心酸果冻'--用count统计函数将用户的发贴数量存到变量@count中--select @count = count(*) from bbsTopic where TuID = @uNameID--输出--print '心酸果冻发贴数量为:' + convert(varchar(10),@count)print ''print '具体内容如下:'--显示信息,判断用户发贴数量大于0时显示帖子的信息--if(@count > 0)select Ttime as 发贴时间,TclickCount as 点击率,Ttopic as 主题,Tcontents as 内容from bbsTopic where TuID = @uNameID--用count统计函数将用户的回贴数量存到变量@count1中--select @count1 = count(*) from bbsReply where RuID = @uNameID--输出--print '心酸果冻的回帖数量为: '+convert(varchar(10),@count1)--显示信息,用户回帖数量大于0时显示帖子信息--if(@count1 > 0)select Rtime as 回帖时间,RclickCount as 点击数量,Rcontents as 回帖内容from bbsReply where RuID = @uNameID--判断用户的等级--if((@count + @count1) < 10)print '心酸果冻贴数总计为: '+convert(varchar(10),@count + @count1)+' 功臣级别为: 新手上路'if(((@count + @count1) >= 10) and ((@count + @count1) < 20))print '心酸果冻贴数总计为: '+convert(varchar(10),@count + @count1)+' 功臣级别为: 侠客'if(((@count + @count1) >= 20) and ((@count + @count1) < 30))print '心酸果冻贴数总计为: '+convert(varchar(10),@count + @count1)+' 功臣级别为: 骑士'if(((@count + @count1) >= 30) and ((@count + @count1) < 40))print '心酸果冻贴数总计为: '+convert(varchar(10),@count + @count1)+' 功臣级别为: 精灵王'if(((@count + @count1) >= 40) and ((@count + @count1) < 50))print '心酸果冻贴数总计为: '+convert(varchar(10),@count + @count1)+' 功臣级别为: 光明使者'if((@count + @count1) >= 50)print '心酸果冻贴数总计为: '+convert(varchar(10),@count + @count1)+' 功臣级别为: 法老'----------------------------------------------------------------------------------------------------------------------------------------------------------回帖最多的为精华贴-----------------------------------------------------------声明整形变量用于存储主贴编号,和作者编号--declare @tidid intdeclare @zhuzheid intdeclare @huifu int--将主贴标号存到@tidid变量中--select top 1 @tidid = TID from bbsTopic order by TreplyCount desc--将用户编号存到@zhuzheid变量中--select @zhuzheid = TuID from bbsTopic where TID = @tidid--输出--print '第一精华贴的信息如下:'--查出回贴最多的主贴,(用户表与主贴表两表连查)--select Ttime as 发贴时间,TclickCount as 点击率,Uname as 作者,Ttopic as 主题,Tcontents as 内容from bbsTopicinner join bbsUserson bbsUsers.UID = bbsTopic.TuIDwhere bbsUsers.UID = @zhuzheid and bbsTopic.TID = @tidid--为了美观输出空行--print ''--利用@tidid(主贴编号)变量查出回复数量存到@huifu变量中--select @huifu = TreplyCount from bbsTopic where TID = @tidid--输出--print '回帖数量'+convert(varchar(10),@huifu)+',如下所示:'--查询回帖信息--select Rtime as 回帖时间,RclickCount as 点击率,回帖表情= case--case多分之语句判断回帖表情--when Rface = 1 then '~(00)~猪头'when Rface = 2 then '*:0)小丑'when Rface = 3 then '[:|]机器人'when Rface = 4 then '{~0~0~}老人家'when Rface = 5 then '(:<)吹水大王'end,Rcontents as 回帖内容from bbsReply where RtID = @tidid-----------------------------------------------------------------------------------------------------------------/*实现:1、论坛人气,点击率>1000为“人气熊旺旺”,否则为“一般般”2、年度品牌版块:主贴量最多的版块3、年度倒胃版块:主贴量最少的版块4、年度回帖人气最旺奖:回帖的点击率排名前2名5、年度最差斑竹:版块点击率低于500或主贴等于0-------------------------------------------------*/--1、论坛人气,点击率>1000为“人气熊旺旺”,否则为“一般般”--print '---->>>>各位大虾注意了,本论坛即将发布年度奖项<<<<----'if(select sum(SclickCount) from bbsSection) > 1000print '论坛人气评估: 人气熊旺旺大家辛苦了'elseprint '论坛人气评估: 一般般,大家加油了'--2、年度品牌版块:主贴量最多的版块--print '年度最佳版块'select Sname as 版块名称,StopicCount as 主贴数量,Sprofile as 简介from bbsSectionwhere StopicCount = (select max(StopicCount) from bbsSection)--3、年度倒胃版块:主贴量最少的版块--print '年度倒胃版块'select Sname as 版块名称,StopicCount as 主贴数量,Sprofile as 简介from bbsSectionwhere StopicCount = (select min(StopicCount) from bbsSection)--4、年度回帖人气最旺奖:回帖的点击率排名前2名--print '年度回帖人气最旺的前两位'select Uname as 大名,Uclass as 星级from bbsUsers where uid in(select top 2 TuID from bbsTopic order by TclickCount desc)--5、年度最差斑竹:版块点击率低于500或主贴等于0--if exists(select * from bbsSection where StopicCount = 0 or SclickCount <= 500)beginprint '请以下斑竹加油哦'select Sname as 版块名称,StopicCount as 主贴数量,SclickCount as 点击率from bbsSectionwhere StopicCount = 0 or SclickCount <= 500end-----------------------------------------------------------------------------------------------------------------*-------------------------------------------用户发主贴---------------------------------------------------------*/--声明变量存放用户ID和版块ID--declare @id intdeclare @bankuai int--将发贴人心酸果冻的ID存到变量@id中--select @id = UID from bbsUsers where Uname = '心酸果冻'--将版块编号放入@bankuai变量中--select @bankuai = SID from bbsSection where Sname like '%.Net%'--将发贴内容插入到主贴表--insert into bbsTopic(TsID,TuID,Tface,Ttopic,Tcontents)values(@bankuai,@id,3,'什么是.Net啊?','我靠!微软的.Net广告超过半个北京城啊....')--更新主贴表,.Net技术版块主贴数加1--update bbsSection set StopicCount = StopicCount + 1 where SID = @bankuai --更新用户积分,如果是新主题则积分加100否则加50分--if not exists(select * from bbsTopic where Ttopic like '什么是.Net啊?' and TuID <> @id)update bbsUsers set Upoint = Upoint + 100 where UID = @idelseupdate bbsUsers set Upoint = Upoint + 50 where UID = @id--更新积分后更新级别--update bbsUsers set Uclass = casewhen Upoint < 500 then 1when Upoint between 500 and 1000 then 2when Upoint between 1001 and 2000 then 3when Upoint between 2001 and 4000 then 4when Upoint between 4001 and 5000 then 5else 6end--所有用户等级从新排名--select 妮称= Uname,星级= casewhen Uclass = 0 then ' 'when Uclass = 1 then '★'when Uclass = 2 then '★★'when Uclass = 3 then '★★★'when Uclass = 4 then '★★★★'when Uclass = 5 then '★★★★★'else '★★★★★★'end,积分= Upoint from bbsUsers------------------------------------------------------------------------------------------------------------------/*----------------------跟贴----------------------------------------------------------------*/--声明变量用于存放回帖人编号及主贴ID--declare @GTid intdeclare @ZTid intdeclare @BKid int--获得可卡因的编号--select @GTid = UID from bbsUsers where Uname = '可卡因'--获得主贴的编号--select @ZTid = TID from bbsTopic where Ttopic like '%什么是.Net%'--获得版块编号--select @BKid = SID from bbsSection where Sname like '%.Net%'--插入回帖内容--insert into bbsReply(RtID,RsID,RuID,Rface,Rcontents)values(@ZTid,@BKid,@GTid,2,'.NET是微软极力推崇的企业级信息网络共享平台.')--回帖后主贴回复数量加1,点击率加1--update bbsTopic set TreplyCount = TreplyCount + 1,TclickCount = TclickCount + 1 where TID = @ZTid--回帖后相应的版块点击率加1--update bbsSection set SclickCount = SclickCount + 1 where SID = @BKid--回帖后给用户加分,如果是第一个回帖加100分,否则加50分--if ((select top 1 RuID from bbsReply where RtID = @ZTid order by Rtime) = @GTid)update bbsUsers set Upoint = Upoint + 100 where UID = @GTidelseupdate bbsUsers set Upoint = Upoint + 50 where UID = @GTid--更新用户相应的信息--update bbsUsers set Uclass = casewhen Upoint < 500 then 1when Upoint between 500 and 1000 then 2when Upoint between 1001 and 2000 then 3when Upoint between 2001 and 4000 then 4when Upoint between 4001 and 5000 then 5else 6end--发布主贴和跟贴--select 主贴作者= '心酸果冻',主贴发布时间= convert(varchar(10),Ttime,111),主题= Ttopic,内容= Tcontentsfrom bbsTopic where TID = @@identityselect 回帖作者= '可卡因',回帖时间= convert(varchar(10),Rtime,111),回帖内容= Rcontentsfrom bbsReply where RID = @@identity--星级从新排名--select 妮称= Uname,星级= casewhen Uclass = 0 then ' 'when Uclass = 1 then '★'when Uclass = 2 then '★★'when Uclass = 3 then '★★★'when Uclass = 4 then '★★★★'when Uclass = 5 then '★★★★★'else '★★★★★★'end,积分= Upoint from bbsUsers-----------------------------------------------------------------------------------------------------------------/*------------------------------------------删除帖子-----------------------------------------------------------*/--声明变量用于存放回帖人编号及主贴ID--declare @GTid intdeclare @BZid intdeclare @ZTid intdeclare @BKid int--获得斑竹心酸果冻的编号--select @BZid = UID from bbsUsers where Uname = '心酸果冻'--获得主贴的编号--select @ZTid = TID from bbsTopic where Ttopic like '%什么是.Net%'--获得所有回帖人的编号--select @GTid = RuID from bbsReply where RtID = @ZTid--获得版块编号--select @BKid = SID from bbsSection where Sname like '%.Net%'--被删帖的斑竹心酸果冻减去积分100分--update bbsUsers set Upoint = Upoint - 100 where UID = @BZid--回贴的帖主均减积分50分--update bbsUsers set Upoint = Upoint - 50 where UID = @GTid--帖子所在版块的主贴量减1--update bbsSection set StopicCount = StopicCount - 1 where SID = @BKid --如果回帖数量大于10给予严重警告,否则给予警告--if((select TreplyCount from bbsTopic where TID = @ZTid) > 10)print '影响很坏,给予严重警告一次'elseprint '给予警告一次'--开始删除,先删除跟贴在删除主贴--delete from bbsReply where RtID = @ZTiddelete from bbsTopic where TID = @ZTid--重新排名--update bbsUsers set Uclass = casewhen Upoint < 500 then 1when Upoint between 500 and 1000 then 2when Upoint between 1001 and 2000 then 3when Upoint between 2001 and 4000 then 4when Upoint between 4001 and 5000 then 5else 6end--重新星级排名--select 妮称= Uname,星级= casewhen Uclass = 0 then ' 'when Uclass = 1 then '★'when Uclass = 2 then '★★'when Uclass = 3 then '★★★'when Uclass = 4 then '★★★★'when Uclass = 5 then '★★★★★'else '★★★★★★'end,积分= Upoint from bbsUsers-----------------------------------------------------------------------------------------------------------------select * from bbsUsersselect * from bbsSectionselect * from bbsTopicselect * from bbsReply。