zoukankan      html  css  js  c++  java
  • [转] 简易论坛后台数据库设计及实现



    --① 创建数据库.sql
    --调用master数据库
    use master
    go
    --判断数据库bbsDB是否存在,如果存在,则删除
    if exists(select * from sysdatabases where name ='bbsDB')
    drop database bbsDB
    --exec xp_cmdshell 'mkdir f:project'     --创建一个'f:project'
    go
    --创建bbsDB数据库
    create database bbsDB      --主数据文件
    on
    (
    name='bbsDB_data',             --主数据文件的逻辑名
    filename='f:\bbsDB\bbsDB_data.mdf',         --主数据文件的物理名
    size=10MB,                  --主数据文件初始大小
    filegrowth=20%                 --主数据文件的增长率
    )
    LOG on                      --日志文件
    (
    name='bbsDB_log',             --日志文件逻辑名
    filename='f:\bbsDB\bbsDB_data.ldf',         --日志文件物理名
    size=1MB,                  --日志文件的初始大小
    filegrowth=10%                 --日志文件的增长率
    )
    --★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--
    --② 创建表bbsReply(回帖表).sql
    --运行bbsDB数据库
    use bbsDB
    go
    --检查表bbsReply是否存在,如果存在就删除
    if exists(select * from sysobjects where name='bbsReply')
    drop table bbsReply
    --创建表bbsReply
    create table bbsReply
    (
    RID int not null identity(1,1),
    RTID int not null,
    RSID int not null,
    RUID int not null,
    RFace int,
    RContents varchar(200) not null,
    RTime datetime not null,
    RClickCount int not null,
    )
    --为表增加外键(RTID),引用bbsTopic表的主键盘TID
    alter table bbsReply add constraint FK_RTID foreign key (RTID) references bbsTopic(TID)
    --为表增加外键(RSID),引用bbsSection表的主键SID
    alter table bbsReply add constraint FK_RSID foreign key(RSID) references bbsSection(SID)
    --为表增加外键(RUID),引用bbsUsers表的主键UID
    alter table bbsReply add constraint FK_RUID foreign key(RUID) references bbsUsers(UID)
    --增加默认值
    alter table bbsReply add constraint DK_RTime default(getdate()) for RTime
    --增加默认值
    alter table bbsReply add constraint DK_RClickCount default(0) for RClickCount
    --查看表数据
    select * from bbsReply
    --插入测试数据
    insert into bbsReply (RTID,RSID,RUID,RFace,RContents,RTime,RClickCount)
    values (1,1,5,2,'jsp乱码问题该怎么解决最好,因为我发现这个问题好象在好多地方都看见了',getdate(),100)
    insert into bbsReply (RTID,RSID,RUID,RFace,RContents,RTime,RClickCount)
    values (1,1,4,4,'转换jsp..',getdate(),200)
    insert into bbsReply (RTID,RSID,RUID,RFace,RContents,RTime,RClickCount)
    values (2,2,2,3,'.net很精彩,就像ppmm啊!',getdate(),200)
    --★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--
    --③ 创建表bbsSection(版块表).sql
    --使用数据库bbsDB
    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 primary key,  --版块编号
    Sname varchar(30) not null,               --版块名称
    SmasterID int not null,                   --版主ID,外键;引用用户bbsUsers的UID
    Sprofile varchar(50),                   --版面简介
    SclickCount int,                    --点击率
    StopicCount int                        --发帖数
    )
    --为bbsSection创建外键,引用bbsUsers的UID
    alter table bbsSection
    add constraint FK_Section_UID
    foreign key (SmasterID) references bbsUsers(UID)
    --为bbsSection增加默认值
    alter table bbsSection add constraint DF_SclickCount default(0) for SclickCount
    alter table bbsSection add constraint DF_StopicCount default(0) for StopicCount
    --插入数据
    insert into bbsSection(Sname,SmasterID,Sprofile,SclickCount,StopicCount)
    values('Java技术',3,'包含框架,开源,非技术区,J2SE',500,1)
    insert into bbsSection(Sname,SmasterID,Sprofile,SclickCount,StopicCount)
    values('.Net技术',5,'包含C#,ASP,.NET Framework,Web Services',800,1)
    insert into bbsSection(Sname,SmasterID,Sprofile,SclickCount,StopicCount)
    values('Linux/Unix社区',5,'包含系统维护与使用区,程序开发区别',0,0)
    select * from bbsSection
    --★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--
    --④ 创建表bbsTopic(主帖表).sql
    --使用bbsDB
    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 primary key,  --帖子编号
    TSID int not null,                    --版块编号;外键,引用bbsSection表的主键SID
    TUID int not null,                    --发帖人ID;外键,引用bbsUsers表的主键UID
    TReplyCount int not null,               --回复数量
    TFace int,                         --发帖表情
    TTopic varchar(50) not null,              --标题
    TContents varchar(200) not null,          --发帖内容
    TTime datetime not null,               --发帖时间
    TClickCount int not null,               --点击数
    TState int not null,                   --状态,例如是否被锁,是否为精华帖子
    TLastReply datetime,                   --最后回复时间
    )
    --把TSID设置为外键,引用bbsSection表的SID主键
    alter table bbsTopic add constraint FK_Topic_SID foreign key(TSID) references bbsSection(SID)
    --把TUID设置为外键,引用bbsUsers表的UID主键
    alter table bbsTopic add constraint FK_Topic_UID foreign key(TUID) references bbsUsers(UID)
    --创建默认值
    alter table bbsTopic add constraint DF_TReplyCount default(0) for TReplyCount
    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(1) for TState

    --插入测试数据
    insert into bbsTopic (TSID,TUID,TReplyCount,TFace,TTopic,TContents,TTime,TClickCount,TState,TLastReply)
    values(1,3,2,1,'还是JSP中...','jsp文件中读取...',2005-08-01,200,1,2005-08-01)
    insert into bbsTopic (TSID,TUID,TReplyCount,TFace,TTopic,TContents,TTime,TClickCount,TState,TLastReply)
    values(2,2,0,2,'部署.net...','项目包括WinSe...',getdate(),200,1,getdate())
    select * from bbsTopic
    --★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--
    --⑤ 创建表bbsUsers(用户信息表).sql
    --使用bbsDB数据库
    use bbsDB
    go
    --判断是否存在表bbsUsers,如果存在就删除
    if exists(select * from sysobjects where name='bbsUsers')
    drop table bbsUsers
    go
    --创建表bbsUsers
    create table bbsUsers
    (
    UID int identity(1,1) not null,           --自动编号,标识列
    Uname varchar(15) not null,            --昵称
    Upassword varchar(10),                --密码
    Uemail varchar(50),                 --邮件
    Usex bit not null,                 --性别
    Uclass int,                      --级别(几星级)
    Uremark varchar(50),                --备注
    UregDate datetime not null,            --注册日期
    Ustate int null,                 --状态(是否禁言)
    Upoint int null,                 --积分(点数)
    )
    go
    select * from bbsUsers                --查看表
    go
    --设主键
    alter table bbsUsers add constraint PK_UID primary key(UID)          --把UID设为主键
    --默认设置
    alter table bbsUsers add constraint DF_Upassword  default (888888) for Upassword    --初试密码默认为6个8
    alter table bbsUsers add constraint DF_Usexm default (1) for Usex        --性别默认为1(男)
    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_Uclass default (1) for Uclass       --默认级别为1(星级)
    alter table bbsUsers add constraint DF_Upoint default (20) for Upoint       --默认积分为20点
    --检查约束
    alter table bbsUsers add constraint CK_Uemail check(Uemail like '%@%')       --邮件地址必须含有'@'字符
    alter table bbsUsers add constraint CK_Upassword check(len(Upassword)>=6)      --密码至少6位
    go
    --插入测试数据
    insert into bbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
    values('可卡因','HYXS007','SS@hotmail.com',1,'1','我要去公安局自首',getdate(),0,200)
    insert into bbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
    values('心酸果冻','888888','lyzTTT@hotmail.com',0,'2','牵匹瘦马闯天下',getdate(),0,200)
    insert into bbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
    values('冬篱儿','fangdong','bb@sohu.com',1,'3','爱迷失在天堂',getdate(),0,600)
    insert into bbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
    values('Supper','master','dd@p.com',1,'6','BBS大斑竹',getdate(),0,5000)
    insert into bbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
    values('可卡因','HYXS007','SS@hotmail.com',1,'1','我要去公安局自首',getdate(),0,200)
    insert into bbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
    values('心酸果冻','888888','lyzTTT@hotmail.com',0,'2','牵匹瘦马闯天下',getdate(),0,200)
    insert into bbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
    values('冬篱儿','fangdong','bb@sohu.com',1,'3','爱迷失在天堂',getdate(),0,600)
    insert into bbsUsers(Uname,Upassword,Uemail,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
    values('Supper','master','dd@p.com',1,'6','BBS大斑竹',getdate(),0,5000)
    --查询bbsUsers
    select * from bbsUsers
    --★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--
    --⑥ 对数据库的操作.sql
    --使用bbsDB
    use bbsDB
    go
    --查询bbsDB内的表
    select * from bbsUsers   --用户表
    select * from bbsTopic         --主帖表
    select * from bbsReply         --回帖表
    select * from bbsSection     --版块表
    /*1.使用系统变量,查询数据库系统情况*/
    --查询SQL Server的版本号、服务器的名称、错误号等
    print 'SQL Server的版本:'+ @@version
    --查询服务器名称
    print '服务器的名称:'+ @@servername
    --修改会发生错误,因为Upassword限制必须大于6位,用@@error接受
    update bbsUsers set Upassword='1234' where Uname='可卡因'
    print convert(varchar(5),@@error)
    /*2.网上有人举报可卡因涉嫌发表不合法言论,版主希望查看核实可卡因的发贴情况和权限*/
    set nocount on               --不显示T-SQL语句影响的行数的信息
    print ' '
    print '个人资料如下:'                   --查看可卡因个人资料
    select 昵称=Uname,登记=Uclass,个人说明=Uremark,积分=Upoint
    from bbsUsers where Uname='可卡因'
    declare @userID int           --定义变量,用于存放用户编号值
    select @userID=UID from bbsUsers where Uname='可卡因'     --变量赋值
    print '可卡因发帖如下:'                   --查看可卡因发帖情况
    select 发帖时间=convert(varchar(10),Ttime,111),点击率=TClickCount,
    主题=TTopic,内容=TContents,状态=TState from bbsTopic where TUID=@userID
    print '可卡因回帖如下:'                   --查看可卡因回帖情况
    select 回帖时间=convert(varchar(10),Rtime,111),点击率=RClickCount,
    内容=Rcontents  from bbsReply where RuID=@userID
    print '可卡因权限:'                    --查看可卡因权限,如果积分大于30则可以发帖
    declare @point int
    select @point=Upoint from bbsUsers where Uname='可卡因'
    if (@point>30)
        print '有权发贴'
    else
        print '无权发帖'
    go
    /*3.循环反复提份(每次提分50),直到积分平均分达到2000以上*/
    select * from bbsUsers
    declare @avg int,@score
    set @score=0
    while(1=1)
        begin
            update bbsUsers set Upoint=Upoint+50 where Ustate<>4 --除了被封杀的帖子
            set @score=@score+50                        --记录提分值
            select @avg=avg(Upoint) from bbsUsers              --获取提分后的平均分
            if(@avg>2000)
                break
        end
    print '提升分值:'+convert(varchar(8),@score)
    /*4.给用户评星级:0~500评为1星级,500~1000评为2星级,1000~2000评为3星级,2000~4000评为4星级,
    4000~5000评为5星级,5000以上评为6星级*/
    --更新用户对应的等级
    update bbsUsers
        set Uclass=case
            when Upoint <500 then 1
            when Upoint between 500 and 1000 then 2
            when Upoint between 1001 and 2000 then 3
            when Upoint between 2001 and 4000 then 4
            when Upoint between 4001 and 5000 then 5
            else 6
        end
    print '加分后的用户级别情况'
    select 昵称=Uname,星级=case
                when 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
    go
    /*5.查询心酸果冻的发帖数和回帖数,如果发帖数>0,显示发帖数和具体的帖子信息,否则显示发帖数为:0帖;同理,回帖也如此.
    最后显示帖子总计数量(发帖数+回帖数),并根据帖子总量显示功臣级别*/
    select * from bbsTopic
    select * from bbsUsers
    select * from bbsReply
    declare @uid int         --记录心酸果冻的的UID
    declare @total int         --记录总帖子数
    set @total=0
    select @uid=UID from bbsUsers where Uname='心酸果冻'
    print '心酸果冻发帖如下:'
    select @total=@total+count(*) from bbsTopic where TUID=@uid
    print '帖子如下:'
    select convert(varchar(10),TTime,111),点击率=TClickCount,
    内容=TContents from bbsTopic where TUID=@uid
    print '心酸果冻回帖如下:'
    select @total=@total+count(*) from bbsReply where RUID=@uid
    print '帖子如下:'
    select convert(varchar(10),RTime,111),点击率=RClickCount,
    内容=RContents from bbsReply where RUID=@uid
    print '心酸果冻帖数总计:'+convert(varchar(8),@total)
    select 功臣级别=case
            when @total <10 then '新手上路'
            when @total between 10 and 20 then '侠客'
            when @total between 21 and 30 then '骑士'
            when @total between 31 and 40 then '精灵王'
            when @total between 41 and 50 then '光明使者'
            else '法老'
        end
    go
    /*6.选出精华帖(回帖量最多的帖子为精华帖子)*/
    set nocount on
    select * from bbsReply
    select * from bbsTopic
    select * from bbsUsers
    select * from bbsSection
    print '第一精华帖的信息如下'
    declare @max int              --存储最多回帖数
    declare @rtid int              --存储精华帖编号
    declare @ttopic varchar(50)         --存储精华帖的标题
    declare @name varchar(20)         --存储精华帖作者姓名
    declare @uid int              --存储精华帖作者编号
    select top 1 @max=count(*),@rtid=rtid from bbsReply group by RTID order by RTID
    select @ttopic=TTopic,@uid=TUID from bbsTopic where TID=@rtid
    select @name=Uname from bbsUsers where UID=@uid
    select 发帖时间=convert(varchar(10),TTime,111),点击率=TClickCount,作者=@name,主题=@ttopic,内容=Tcontents
    from bbsTopic where TID=@rtid
    print '回帖数:'+convert(varchar(10),@max)
    print '如下所示'
    select 回帖时间=convert(varchar(10),RTime,111),点击率=RclickCount,回帖表情=case
                    when RFace=1 then '^(oo)^猪头'
                    when RFace=2 then '*:o)小丑'
                    when RFace=3 then '[:|]机器人'
                    when RFace=4 then '{^o~o^}老人家'
                    else         '(:<)吹水大王'
                end
        ,回帖内容=RContents from bbsReply where RTID=@rtid
    /*7.论坛年度评估*/
    set nocount on
    print '--->>>各位大虾注意了,本论坛现在发布年度无记名评奖<<<---'
    --人气年度评估(论坛总点击率>1000,为"人气熊旺旺";否则就为"一般般")
    --select * from bbsSection
    print '论坛人气年度评估'
    if (select sum(SclickCount) from bbsSection)>1000
        print '人气熊旺旺'
    else
        print '一般般'
    --年度品牌版块:主帖量最多的版块
    --select * from bbsUsers
    --select * from bbsTopic
    --select * from bbsSection
    print '年度品牌版块评估:'
    select 版块名称=Sname,主帖数量=StopicCount,简介=Sprofile from bbsSection
    where StopicCount=(select max(StopicCount) from bbsSection)
    --年度倒胃版块:主帖最少的版块
    print '年度倒胃版块评估:'
    select 版块名称=Sname,主帖数量=StopicCount,简介=Sprofile from bbsSection
    where StopicCount=(select min(StopicCount) from bbsSection)
    --年度回帖人气最旺奖:回帖的点击率排名前两名
    print '年度回帖人气最IN的前两名获奖作者:'
    select * from bbsUsers
    select * from bbsTopic
    select 大名=Uname,星级=Uclass from bbsUsers
    where UID in(select  top 2 TUID from bbsTopic order by TClickCount desc)
    --评选最差版主:如果存在发帖子量为或者点击率低于500的版块,则评选最差版主
    if exists(select * from bbsSection where StopicCount=0 or SclickCount<=500)
        begin
            print    '请下列版块斑竹加油哦!'
            select 版块名称=Sname,主帖数量=StopicCount,点击率=SclickCount from bbsSection
            where StopicCount=0 or SclickCount<=500
        end
    /*8*/
    --用户发主帖后,需要更新相应版块(.NET技术版块)的主帖数,主帖数+1
    --用户发主帖后,酌情加分:如果主帖是新帖(别人没提及过),加100分,否则加50分.
    --用户积分添加后,更新用户的相应等级
    --在论坛上发布用户的新帖.
    --所有用户的等级重新排名
    /*--发主帖:
    心酸果冻在.NET技术版块发帖:
    怯怯的问:什么是.NET啊?
    微软的.NET广告超过半个北京城啊...
    */
    select * from bbsUsers
    select * from bbsSection
    select * from bbsTopic
    declare @userID varchar(10),@sID int --定义变量存放用户编号和版块编号
    select @userID=UID from bbsUsers where Uname='心酸果冻'        --获取心酸果冻的用户编号
    select @sID=SID from bbsSection where Sname like '%.NET技术%'    --获取.NET版块的编号
    --将心酸果冻的发帖插入主帖表
    insert into bbsTopic(TSID,TUID,TFace,Ttopic,Tcontents)
        values(@sID,@userID,3,'什么是.NET啊?','我靠!微软的.NET广告超过半个北京城啊...')
    --更新版块表:.NET技术版块主帖数+1
    update bbsSection set StopicCount=StopicCount+1 where SID=@sID
    --更新用户积分:如果是新主题,则积分增加100,否则增加50
    if not exists(select * from bbsTopic where TTopic like '什么是.NET啊?' and TuID<>@userID)
        update bbsUsers set Upoint=Upoint+1 where UID=@userID
    else
        update bbsUsers set Upoint=Upoint+50 where UID=@userID
    --更新用户积分后,更新相应的级别
    update bbsUsers
        set Uclass=case
                when Upoint<500 then 1
                when Upoint between 500 and 1000 then 2
                when Upoint between 1001 and 2000 then 3
                when Upoint between 2001 and 4000 then 4
                when Upoint between 4001 and 5000 then 5
                else 6
            end
        where UID=@userID
    --对外发布心酸果冻的发帖(使用系统变量@@IDENTITY查出刚才插入的编号值)
    select 发帖作者='心酸果冬',发帖时间=convert(varchar(10),TTime,111),
    主题=TTopic,内容=TContents from bbsTopic where TID=@@IDENTITY
    --显示目前的最新排名
    select 昵称=Uname,星级=case
                when 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
    go
    /*9*/
    --在论坛上发布主帖和跟帖.
    --论坛用户星级重新排名.
    /*
    可卡因回复主帖:"什么是.NET啊?"
    笑呵呵的回复道:".NET是微软力推的企业级信息网络共享平台."
    */
    set nocount on
    select * from bbsUsers
    select * from bbsReply
    select * from bbsTopic
    select * from bbsSection
    declare @uid int
    select @uid=UID from bbsUsers where Uname='可卡因'
    print @uid
    declare @tid int
    declare @sid int
    select @tid=TID,@sid=TSID from bbsTopic where TTopic like '%还是JSP中%'
    print @tid
    print @sid
    insert into bbsReply (RTID,RSID,RUID,RFace,RContents)
    values(@tid,@sid,@uid,4,'.NET是微软力推的企业级信息网络共享平台.')
    --用户回帖后,需要更新对应主帖的信息:回复数量+1,点击率+1.
    update bbsTopic set TClickCount=TClickCount+1,TReplyCount=TReplyCount+1
    --用户回帖后,还需要更新对应版块的点击率,点击率+1.
    update bbsSection set StopicCount=StopicCount+1
    --用户回帖后,酌情加分:如果是该帖的第一回帖人(即第一个回帖的),加100分;否则加50分.
    --方法一
    /*declare @treplycount int
    select @treplycount=TReplyCount from bbsTopic where @tid=TID
    if (@treplycount=1)
        update bbsUsers set Upoint=Upoint+100
    else
        update bbsUsers set Upoint=Upoint+50*/
    --方法二
    declare @treplycount int
    select @treplycount=count(*) from bbsReply where RTID=@tid
    if(@treplycount=1)
        update bbsUsers set Upoint=Upoint+100
    else
        update bbsUsers set Upoint=Upoint+50
    --用户积分添加后,更新用户的相应等级.
    update bbsUsers set Uclass=case
                when Upoint<500 then 1
                when Upoint between 500 and 1000 then 2
                when Upoint between 1001 and 2000 then 3
                when Upoint between 2001 and 4000 then 4
                when Upoint between 4001 and 5000 then 5
                else 6
            end
        where UID=@uid
    --在论坛上发布主帖和跟帖
    --显示主帖
    select * from bbsTopic where TID=@tid
    --显示回帖
    select * from bbsReply where RID=@@IDENTITY
    --论坛用户星级重新排名
    select 昵称=Uname,星级=case
                when 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
    /*10为了维护论坛环境的"空气清新",斑竹会定期检查帖子
    或核实网友对某个帖子的投诉,然后
    删除不合法的发帖*/
    --删除帖子
    --主题:什么是.NET?
    --内容:我靠!微软的.NET广告超过了半个北京城啊.
    select * from bbsTopic
    select * from bbsUsers
    select * from bbsReply
    select * from bbsSection
    declare @tid int
    declare @uidt int
    select @tid=TID,@uidt=TUID from bbsTopic where TTopic like '%什么是.NET啊%'
    --帖主分数-100
    update bbsUsers set Upoint=Upoint-100
    --跟贴的帖主分数-50
    update bbsUsers set Upoint
    delete from bbsTopic where TID=@tid
    select * from bbsReply where RTID=1
    /*11*/
    /*12*/
    --调用系统存储过程查看用户表(bbsUsers)的相关信息
    use bbsDB
    go
    exec sp_helpconstraint bbsUsers        --查看表bbsUsers的约束
    exec sp_helpindex bbsUsers        --查看表bbsUsers的索引
    go
    use master
    go
    exec xp_cmdshell 'mkdir e: est',no_output    --在e:下创建文件夹test
    exec xp_cmdshell 'dir e:'            --查看文件夹
    /*13创建带参数的存储过程*/
    --编写存储过程proc_find1,实现查找某个用户的发贴情况
    use bbsDB
    go
    if exists(select * from sysobjects where name='proc_find1')
        drop procedure proc_find1
    go
    create procedure proc_find1
        @userName varchar(10)    --输入参数用户名
        as
            set nocount on
            --获取用户对应的用户编号UID
            declare @userID varchar(10)
            select @userID=UID from bbsUsers where Uname=@userName
            --如果在主帖表中存在该用户发表的主帖
            if exists(select * from bbsTopic where TuID=@userID)
                begin
                    print @userName+'发表的主帖如下:'
                    select 发贴时间=convert(varchar(10),TTime,111),点击率=TClickCount,
                        主题=TTopic,内容=Tcontents from bbsTopic where TUID=@userID
                end
            else
                print @userName+'没有发表过主帖'
            if exists(select * from bbsReply where RUID=@userID)
                begin
                    print @userName+'发表的回帖如下:'
                    select 发帖时间=convert(varchar(10),RTime,111),点击率=RClickCount,
                        回帖内容=Rcontents from bbsReply where RUID=@userID
                end
            else
                print @userName+'没有发表过回帖'
    go
    exec proc_find1 '心酸果冻'
    /*14创建带返回值的存储过程*/
    --编写存储过程proc_find2,查找某个用户的发贴情况,并返回发贴数和回帖数
    select * from bbsUsers
    if exists(select * from sysobjects where name='pro_find2')
        drop procedure pro_find2
    go
    create procedure pro_find2
        @userName varchar(10),        --输入参数:用户名
        @sumTopic int output,        --输出参数:主帖数
        @sumReply int output        --输出参数:回帖数
        as
            set nocount on
            declare @userID varchar(10)
            select @userID=UID from bbsUsers where Uname=@userName
            if exists(select * from bbsTopic where TUID=@userID)
                begin
                    --获取主帖数,保存在输出参数中
                    select @sumTopic=count(*) from bbsTopic where TUID=@userID
                    print @userName+'发表主帖如下:'
                    select 发贴时间=convert(varchar(10),TTime,111),点击率=TClickCount,
                        主题=TTopic,内容=TContents from bbsTopic where TUID=@userID
                end
            else
                begin
                    set @sumTopic=0        --设置发帖数为0
                    print @userName+'没有发表过主帖.'
                end
            if exists(select * from bbsReply where RUID=@userID)
                begin
                    --获取回帖数,保存在输出参数中
                    select @sumReply=count(*) from bbsReply where RUID=@userID
                    print @userName+'发表的回帖如下:'
                    select 回帖时间=convert(varchar(10),Rtime,111),点击率=RclickCount,
                        回帖内容=Rcontents from bbsReply where RUID=@userID
                end
            else
                begin
                    set @sumReply=0
                    print @userName+'没有发表过回帖.'
                end
    --测试存储过程pro_find2
    set nocount on
    declare @sum1 int,@sum2 int
    exec pro_find2 '心酸果冻',@sum1 output,@sum2 output
    print @sum1
    print @sum2
    --★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--★★★★★--
    --⑦ 设置用户及用户权限.sql
    --为SQL创建新的登录用户David,密码为223251
    EXEC sp_addlogin 'David','223251'
    --为David用户设置访问bbsDB的权限
    use bbsDB
    go
    EXEC sp_grantdbaccess 'David','David'
    --为David用户设置可以在bbsDB中创建表的权限
    GRANT create table to David
    --为David用户设置对bbsDB中的表的权限,David是版块斑竹
    GRANT update on bbsUsers to David
    GRANT select,delete on bbsTopic to David
    GRANT select,delete on bbsReply to David

  • 相关阅读:
    软件工程网络15结对编程作业(201521123111 陈伟泽)
    软件工程网络15个人阅读作业2(201521123111 陈伟泽)
    软件工程网络15个人阅读作业1(201521123111 陈伟泽)
    Java课程设计——猜数游戏(201521123111 陈伟泽)
    java课程设计--猜数字(团队博客)
    201521123111《Java程序设计》第14周学习总结
    201521123111《Java程序设计》第13周学习总结
    个人作业5
    ,软工第四次作业
    第三次作业
  • 原文地址:https://www.cnblogs.com/temptation/p/975391.html
Copyright © 2011-2022 走看看