SQL Server第一次上机操作实例:用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)

运行上述代码并刷新后便可见创建的数据库、表以及约束
如下:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
其数据库关系图如下图:
在这里插入图片描述

在建立数据库关系图时,开始就遇见了一些问题,不知道咋建立关系图,然后查了查资料就解决了,如果有类似情况的小伙伴请进入传送门,一起来解决这个问题吧😁

点击此处打开传送门:SQL Server如何建立数据库关系图:别急!!一串代码让你轻松永久解决此类问题

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值