用sql语句创建BBS数据库
训练技能点:
学会使用sql语句创建数据库、表、约束
使用到的软件:SQL Server2008
代码如下:
use master go if exists(select * from sysdatabases where name='BBS') --判断BBS库是否存在,若存在则先删除 drop database BBS create database BBS on primary ( name='BBS_data', filename='c:\data\BBS_data.mdf', size=10, maxsize=20, filegrowth=20% ) log on ( name='BBS_log', filename='c:\data\BBS_log.ldf', size=1, maxsize=10, filegrowth=10% ) go use BBS go if exists(select * from sysobjects where name='BBSUser') --判断BBSUser表是否存在,若存在则先删除 drop table BBSUser create table BBSUser --创建(论坛用户)表 ( UID int primary key identity not null, --用户编号,设为主键 UName varchar(20) not null, --用户昵称 UPassword varchar(10) null, --密码 UEmail varchar(40) null, --电子邮件 UBirthday varchar(30) not null, --生日 USex bit not null, --性别 UClass int not null, --用户等级 UStatement varchar(50) null, --用户备注 URegDate datetime not null, --注册日期 UState int null, --用户状态 UPoint int null --用户积分 ) go use BBS go if exists(select * from sysobjects where name='BBSTopic') drop table BBSTopic create table BBSTopic ( TID int primary key identity not null, --标识主键列 TNumber int not null, --帖子编号 TSID int not null, --所在版块 TUID int not null, --发帖人 TReplyCount int not null, --回复数 TEmotion int null, --回复表情 TTopic varchar(40) not null, --主题 TContents varchar(40) not null, --正文 TTime datetime not null, --回复时间 TClickCount int null, --点击数 TFlag int null, --状态 TLastClickT datetime --最后回复时间 ) go use BBS go if exists(select * from sysobjects where name='BBSReply') drop table BBSReply create table BBSReply ( RID int primary key identity not null, --标识主键列 RNumber int not null, --帖子编号 RTID int not null, --回复的主贴 RSID int not null, --所在板块编号 RUID int not null, --发帖人编号 REmoticon int null, --发贴表情 RTopic varchar(40) not null, --主题 RContents varchar(40) not null, --正文 RTime datetime --发帖时间 ) go use BBS go if exists(select * from sysobjects where name='BBSSection') drop table BBSSection create table BBSSection ( SID int primary key identity not null, --版块编号,主键 SName varchar(30) not null, --版块名称 SMasterID int not null, --本版格言 SStatement varchar(30) not null, --点击率 SClickCount int null, --帖子数量 STopicCount int null ) go --添加约束 --1、为BBSUser表添加约束 alter table BBSUser add constraint df_UPassword default 888888 for Upassword alter table BBSUser add constraint df_USex default 1 for Usex alter table BBSUser add constraint df_URegDate default getdate() for URegDate alter table BBSUser add constraint df_UState default 0 for UState alter table BBSUser add constraint df_UClass default 1 for UClass alter table BBSUser add constraint df_UPoint default 10 for UPoint alter table BBSUser add constraint ck_UPassword check(len(UPassword)>=6) --2、为BBSTopic表添加约束 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_TLastClickT default getdate() for TLastClickT alter table BBSTopic add constraint fk_TSID foreign key(TSID) references BBSSection(SID) alter table BBSTopic add constraint fk_TUID foreign key(TUID) references BBSUser(UID) --3、为BBSReply表添加约束 alter table BBSReply add constraint df_RTime default getdate() for RTime alter table BBSReply add constraint fk_RTID foreign key(RTID) references BBSTopic(TID) alter table BBSReply add constraint fk_RSID foreign key(RSID) references BBSSection(SID) alter table BBSReply add constraint fk_RUID foreign key(RUID) references BBSUser(UID) --4、为BBSSection表添加约束 alter table BBSSection add constraint df_SClickCount default 0 for SClickCount alter table BBSSection add constraint df_STopicCount default 0 for STopicCount alter table BBSSection add constraint fk_SMasterID foreign key (SMasterID) references BBSUser(UId)
运行上述代码并刷新后便可见创建的数据库、表以及约束
如下:




浙公网安备 33010602011771号