1 Star 0 Fork 48

邓德城/SQL Server作业仓库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SQLQuery9.sql 3.60 KB
一键复制 编辑 原始数据 按行查看 历史
五星好市民.林 提交于 2021-03-16 21:48 . 林程铭第5次作业
create database bbs--创建数据库
on
(--主数据库参数设置
name='bbs',
filename='C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\bbs.mdf',
size=5MB,
filegrowth=1MB,
maxsize=unlimited
),(
--次数据库文件参数设置
name='bbs',
filename='C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\bbs.ndf',
size=5MB,
filegrowth=1MB,
maxsize=unlimited
)
log on
(
--日志文件参数管理
name='bbs1_log',
filename='C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\bbs1_log.ldf',
size=5MB,
filegrowth=1MB,
maxsize=unlimited
)
--------------------------------
go
--创建表
create table bbsUsers--创建用户信息表
(
UID int identity primary key,--用户编号 UID int 主键 标识列
uName varchar(10),--用户名 uName varchar(10) 唯一约束 不能为空
uSex varchar(2),--性别 uSex varchar(2) 不能为空 只能是男或女
uAge int ,--年龄 uAge int 不能为空 范围15-60
uPoint int ,--积分 uPoint int 不能为空 范围 >= 0
);
go
----
alter table bbsUsers
add constraint PK_bbsUsers_UID primary key(UID)
alter table bbsUsers
add constraint UQ_bbsUsers_uName check(uName is not null)
alter table bbsUsers
add constraint UQ_bbsUsers_uName unique(uName)
alter table bbsUsers
add constraint CK_bbsUsers_uSex check(uSex is not null)
alter table bbsUsers
add constraint DF_bbsUsers_uSex check(uSex='男' or uSex='女')
alter table bbsUsers
add constraint CK_bbsUsers_uAge check(uAge is not null)
alter table bbsUsers
add constraint CK_bbsUsers_uAge check(uAge>15 and uAge<60)
alter table bbsUsers
add constraint CK_bbsUsers_uPoint check(uPoint is not null)
alter table bbsUsers
add constraint CK_bbsUsers_uPoint check(uPoint>=0)
create table bbsTopic--主贴表
(
tID int identity primary key,--主贴编号 tID int 主键 标识列
tUID int references bbsUsers(UID) ,--发帖人编号 tUID int 外键 引用用户信息表的用户编号
tSID int references bbsSection(sID) ,--版块编号 tSID int 外键 引用版块表的版块编号 (标明该贴子属于哪个版块)
tTItle varchar(100) not null,--贴子的标题
tMsg text not null,--帖子的内容
tTime datetime,--发帖时间
tCount int--回复数量
);
go
create table bbsReply--回帖表
(
rID int identity primary key,--回贴编号 rID int 主键 标识列,
rUID int references bbsUsers(UID) ,--回帖人编号 rUID int 外键 引用用户信息表的用户编号
rTID int references bbsTopic(tID),--对应主贴编号 rTID int 外键 引用主贴表的主贴编号 (标明该贴子属于哪个主贴)
rMsg text not null,--回帖的内容 rMsg text 不能为空
rTime datetime ,--回帖时间 rTime datetime
);
go
create table bbsSection --版块表
(
sID int identity primary key ,--版块编号 sID int 标识列 主键
sName varchar(10),--版块名称 sName varchar(10) 不能为空
sUid int ,--版主编号 sUid int 外键 引用用户信息表的用户编号
);
go
---
alter table bbsSection
add constraint PK_bbsSection_sID primary key(sID)
alter table bbsSection
add constraint CK_bbsSection_sName check(sName is not null)
select * from bbsSection,bbsUsers where bbsSection.sUid=bbsUsers.UID
---------------------
--1.现在有3个会员注册成功,请用一次插入多行数据的方法向bbsUsers表种插入3行记录,记录值如下:
-- 小雨点 女 20 0
-- 逍遥 男 18 4
-- 七年级生 男 19 2
--查询bbsUsers的数据
select * from bbsUsers
--插入多行数据
insert into bbsUsers(uName,uSex,uAge)
values( '小雨点','女',20) ,
( '逍遥','男',18 ) ,
('七年级生','男',19)
--2.将bbsUsers表中的用户名和积分两列备份到新表bbsPoint表中,
-- 提示查询部分列:select 列名1,列名2 from 表名
select uName,uPoint
into bbsPoint
from bbsUsers
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/deng-decheng/sql-server-job-warehouse.git
git@gitee.com:deng-decheng/sql-server-job-warehouse.git
deng-decheng
sql-server-job-warehouse
SQL Server作业仓库
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385