use master
go
------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------创建数据库存放位置(文件夹)-----------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
--exec sp_configure ‘show advanced options‘, 1
--go
--reconfigure
--go
--exec sp_configure ‘xp_cmdshell‘, 1
--go
--reconfigure
--go
--exec xp_cmdshell ‘mkdir E:\‘
--go
------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------创建数据库----------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
if exists(select * from sysdatabases where name = ‘Room‘)
drop database Room
go
create database Room
on primary
(
name = ‘Room‘,
filename = ‘E:\北大青鸟 - 深圳信狮\S2\1_优化MySchool数据库设计\Chapter_08\Room\Room.mdf‘,
size = 5,
filegrowth = 15%
)
log on
(
name = ‘Room_log‘,
filename = ‘E:\北大青鸟 - 深圳信狮\S2\1_优化MySchool数据库设计\Chapter_08\Room\Room_log.ldf‘,
size = 1,
filegrowth = 15%
)
go
------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------创建表------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
use Room
go
if exists(select * from sys.tables where name = ‘RoomType‘)
drop table RoomType
go
if exists(select * from sys.tables where name = ‘RoomState‘)
drop table RoomState
go
if exists(select * from sys.tables where name = ‘Room‘)
drop table Room
go
if exists(select * from sys.tables where name = ‘ResideState‘)
drop table ResideState
go
if exists(select * from sys.tables where name = ‘GuestRecord‘)
drop table GuestRecord
go
create table RoomType --客房类型表
(
TypeID int identity(1,1) not null, --客房类型编号,主键,标识列从1开始,递增值为1
TypeName varchar(50) not null, --客房类型名称
TypePrice Decimal not null --客房价格,大于等于0
)
go
create table RoomState --客房状态表
(
RoomStateID int identity(1,1) not null, --客房状态编号,主键,标识列从1开始,递增值为1
RoomStateName varchar(50) not null --客房状态名称
)
go
create table Room --客房信息表
(
RoomID int identity(1,1) not null, --房间号,主键,标识列从1开始,递增值为1
BedNum int, --床位数,默认值为2
GuestNum int, --入住客人数,默认值为0,必须大于等于0
[Description] varchar(50) not null, --客房描述,必填
RoomStateID int, --客房状态编号,外键
RoomTypeID int --客房类型编号,外键
)
go
create table ResideState --结账状态表
(
ResideId int identity(1,1) not null, --结账状态编号,主键,标识列从1开始,递增值为1
ResideName varchar(50) not null --结账状态名称,必填
)
go
create table GuestRecord --客人信息表
(
GusetID int identity(1,1) not null, --客人入住流水号,主键,标识列从1开始,递增值为1
GusetName varchar(50) not null, --客人姓名
IdentityID varchar(18) not null, --身份证号,字符个数必须等于18
RoomID int, --房间号,外键
ResideID int, --结账状态编号,外键,默认值为“未结账”编号
ResideDate datetime, --入住日期
LeaveDate datetime, --结账日期,必须大于等于入住日期
Deposit decimal, --押金
TotalMoney decimal --总金额
)
go
------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------添加约束信息-------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
--添加主键
alter table RoomType
add constraint PK_RoomType_TypeID primary key(TypeID)
go
alter table RoomState
add constraint PK_RoomState_RoomStateID primary key(RoomStateID)
go
alter table Room
add constraint PK_Room_TypeID primary key(RoomID)
go
alter table ResideState
add constraint PK_ResideState_TypeID primary key(ResideId)
go
alter table GuestRecord
add constraint PK_GuestRecord_TypeID primary key(GusetID)
go
--添加外键约束
alter table Room
add constraint FK_Room_RoomStateID
foreign key(RoomStateID) references RoomState(RoomStateID)
go
alter table Room
add constraint FK_Room_RoomTypeID
foreign key(RoomTypeID) references RoomType(TypeID)
go
alter table GuestRecord
add constraint FK_GuestRecord_RoomID
foreign key(RoomID) references Room(RoomID)
go
alter table GuestRecord
add constraint FK_GuestRecord_ResideID
foreign key(ResideID) references ResideState(ResideId)
go
--添加检查约束
alter table RoomType
add constraint CK_RoomType_TypePrice check(TypePrice >= 0)
go
alter table Room
add constraint CK_Room_GuestNum check(GuestNum >= 0)
go
alter table GuestRecord
add constraint CK_GuestRecord_IdentityID check(len(IdentityID) = 18)
go
alter table GuestRecord
add constraint CK_GuestRecord_LeaveDate check(datediff(day,LeaveDate,ResideDate) <= 0)
go
--添加默认约束
alter table Room
add constraint DF_Room_BedNum default(2) for BedNum
go
alter table Room
add constraint DF_Room_GuestNum default(0) for GuestNum
go
alter table GuestRecord
add constraint DF_GuestRecord_ResideID default(0) for ResideID
go
------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------添加测试数据-------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
insert into RoomType values(‘标准间‘,180.00)
insert into RoomType values(‘单人套房‘,360.00)
insert into RoomType values(‘豪华标准间‘,260.00)
insert into RoomState values(‘清理中‘)
insert into RoomState values(‘未入住‘)
insert into RoomState values(‘已入住‘)
insert into Room values(default,default,‘双人标准间‘,1,1)
insert into Room values(default,default,‘双人标准间‘,2,1)
insert into Room values(default,default,‘双人标准间‘,1,2)
insert into Room values(default,default,‘双人标准间‘,3,1)
insert into Room values(default,default,‘单人间‘,1,1)
insert into Room values(default,default,‘单人间‘,2,1)
insert into Room values(default,default,‘单人间‘,1,2)
insert into Room values(default,default,‘单人间‘,3,1)
insert into Room values(default,default,‘单人间‘,1,3)
insert into Room values(default,default,‘单人间‘,2,3)
insert into Room values(default,default,‘单人间‘,3,2)
insert into Room values(default,default,‘单人套房‘,2,1)
insert into Room values(default,default,‘单人套房‘,1,3)
insert into Room values(default,default,‘单人套房‘,3,1)
insert into Room values(default,default,‘单人套房‘,1,2)
insert into Room values(default,default,‘单人套房‘,2,3)
insert into Room values(default,default,‘单人套房‘,1,3)
insert into Room values(default,default,‘单人套房‘,1,2)
insert into Room values(default,default,‘单人套房‘,3,2)
insert into Room values(default,default,‘单人套房‘,1,2)
insert into Room values(default,default,‘单人套房‘,3,1)
insert into Room values(default,default,‘豪华标准间‘,2,3)
insert into Room values(default,default,‘豪华标准间‘,1,3)
insert into Room values(default,default,‘豪华标准间‘,2,1)
insert into Room values(default,default,‘豪华标准间‘,1,2)
insert into Room values(default,default,‘豪华标准间‘,2,1)
insert into Room values(default,default,‘豪华标准间‘,3,1)
insert into ResideState values(‘未结账‘)
insert into ResideState values(‘已结账‘)
insert into GuestRecord values(‘郜飞‘,‘555555555555555555‘,1,1,GETDATE(),DATEADD(DD,1,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞1‘,‘444444444444444444‘,2,1,GETDATE(),DATEADD(DD,2,getdate()),100.00,380.00)
insert into GuestRecord values(‘郜飞2‘,‘333333333333333222‘,1,1,GETDATE(),DATEADD(DD,3,getdate()),100.00,380.00)
insert into GuestRecord values(‘郜飞3‘,‘333333333333333333‘,1,2,GETDATE(),DATEADD(DD,4,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞4‘,‘410787855123179810‘,2,1,GETDATE(),DATEADD(DD,5,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞5‘,‘123321321312321321‘,1,2,GETDATE(),DATEADD(DD,7,getdate()),100.00,380.00)
insert into GuestRecord values(‘郜飞6‘,‘410787855102579810‘,3,1,GETDATE(),DATEADD(DD,8,getdate()),100.00,380.00)
insert into GuestRecord values(‘郜飞7‘,‘410787855102579810‘,1,1,GETDATE(),DATEADD(DD,1,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞8‘,‘410787855102579810‘,2,1,GETDATE(),DATEADD(DD,3,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞9‘,‘410787855102579810‘,1,1,GETDATE(),DATEADD(DD,1,getdate()),100.00,380.00)
insert into GuestRecord values(‘郜飞10‘,‘410787855102579810‘,3,1,GETDATE(),DATEADD(DD,5,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞11‘,‘123787855102579810‘,1,2,GETDATE(),DATEADD(DD,4,getdate()),100.00,380.00)
insert into GuestRecord values(‘郜飞12‘,‘210787855102579810‘,1,1,GETDATE(),DATEADD(DD,6,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞13‘,‘440787855102579810‘,1,2,GETDATE(),DATEADD(DD,2,getdate()),100.00,380.00)
insert into GuestRecord values(‘郜飞14‘,‘410787855102579810‘,1,1,GETDATE(),DATEADD(DD,1,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞15‘,‘410787855102576789‘,1,1,GETDATE(),DATEADD(DD,1,getdate()),100.00,380.00)
insert into GuestRecord values(‘郜飞16‘,‘410787855102579810‘,1,1,GETDATE(),DATEADD(DD,1,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞17‘,‘456465464546546467‘,2,1,GETDATE(),DATEADD(DD,1,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞18‘,‘000000000000000000‘,1,2,GETDATE(),DATEADD(DD,1,getdate()),100.00,280.00)
insert into GuestRecord values(‘郜飞19‘,‘111111111111111111‘,1,1,GETDATE(),DATEADD(DD,1,getdate()),100.00,280.00)
go