前言
为数据库课程设计做,划水了几天,实在无聊看了一下书(上课没咋听,唉)自己做了一下(刚开始准备划水来着,小组大佬有做),所以有些粗糙,就记录一下,还请大佬们指教
需求分析
需求大家应都非常理解了,不在多述
E-R图(充钱不够,只能画60个图标,部分实体属性不全)
E-R图转换为表结构
客户(证件号[主键],证件类型,姓名,性别,联系电话)
房间类型(类型编号[主键],单价/天,押金,是否可为钟点房,钟点房价格/h)
房间(房间号[主键],类型编号[外键],状态)
入住信息(入住编号[主键],房间号[外键],证件号[外键],入住时间,预入住时间,退房时间,钟点房标志位,罚款金额)
服务(入住编号,服务编号) 实现服务总金额 sum(消费金额) over()
可服务内容(服务编号[主键],服务内容,服务金额)
员工(编号[主键],姓名,职务,性别)
结算(入住编号[主键],员工编号[主键],折扣,折扣原因,收费总金额)
--视图
入住总览表:
入住编号,客户证件号,姓名,联系电话,房间号,入住时间,预入住时间,钟点房标志位
--房间空闲表
房间号,房间类型编号,单价/天,押金,是否可为钟点房,钟点房价格/h
--服务消费总表
入住编号,服务内容,服务金额
代码实现
--创建数据库
create database Hotel_management;
use Hotel_management;
--表的构建
--客户表customer
CREATE TABLE CUSTOMER
( Cid nvarchar(20) NOT NULL, --证件号[主键]
Ctype nvarchar(10) DEFAULT '身份证号', --证件类型
Cname nvarchar(10) NOT NULL, --姓名
Csex nvarchar(2) DEFAULT '男' CHECK (Csex = '男' OR Csex='女'),--性别
Cnumber nvarchar(15) NOT NULL, --联系电话
PRIMARY KEY (Cid),
);
--房间类型表roottype
CREATE TABLE ROOMTYPE
( RTid nvarchar(10) NOT NULL, --类型编号[主键]
RTprice float NOT NULL CHECK (RTprice>=0), --单价/天
RTdeposit float NOT NULL CHECK (RTdeposit>=0),--押金
RTjudge bit NOT NULL, --是否为钟点房
RTjdprice float CHECK (RTjdprice>=0), --钟点房价格
PRIMARY KEY (RTid),
);
--房间表root
CREATE TABLE ROOM
( Rid nvarchar(10) NOT NULL, --房间号[主键]
Rroomtype nvarchar(10) NOT NULL, --类型编号[外键ROOMTYPE(RTid)]
Rstate bit NOT NULL, --状态
PRIMARY KEY (Rid),
FOREIGN KEY (Rroomtype) REFERENCES ROOMTYPE(RTid),
);
--入住信息表live
CREATE TABLE LIVE
( Lid int IDENTITY(1,1), --入住编号[主键]自增
Lrootid nvarchar(10) NOT NULL, --房间号[外键ROOM(Rid)]
Lcustomerid nvarchar(20) NOT NULL,--证件号[外键CUSTOMER(Cid)]
Lstart date NOT NULL, --入住时间
Lexpect date NOT NULL, --预计入住时间
Lout date, --退房时间
Lfine float CHECK (Lfine>=0), --罚款金额
Lmark bit NOT NULL, --钟点房标志位
PRIMARY KEY (Lid),
FOREIGN KEY (Lrootid) REFERENCES ROOM(Rid),
FOREIGN KEY (Lcustomerid) REFERENCES CUSTOMER(Cid),
);
--服务表service
CREATE TABLE SERVICE
( SLid int, --入住编号
SCid int, --服务编号
);
--存在的服务CSERVICE
CREATE TABLE CSERVICE
( CSid int IDENTITY(1,1), --服务编号[主键]
CScontent nvarchar(10) NOT NULL, --服务内容
CSprice float CHECK (CSprice>0) NOT NULL, --服务金额
PRIMARY KEY (CSid),
);
--员工表employees
CREATE TABLE EMPLOYEES
( Eid int IDENTITY(1,1), --职工编号
Ename nvarchar(10) NOT NULL, --姓名
Esex nvarchar(2) DEFAULT '男' CHECK (Esex = '男' OR Esex='女'), --性别
Eposition nvarchar(10) NOT NULL, --职务
PRIMARY KEY (Eid),
);
--结算表settlement
CREATE TABLE SETTLEMENT
( Sliveid int NOT NULL, --入住编号
Semployeesid int NOT NULL, --员工编号
Sdiscount float CHECK (1>Sdiscount AND Sdiscount>0), --折扣
Sdiscountwhy nvarchar(20), --折扣原因
Stotalamount float CHECK (Stotalamount>0), --收费总金额
PRIMARY KEY (Sliveid,Semployeesid),
);
--视图的建立
--入住信息表
go
CREATE VIEW LIVE_INFO(入住编号,客户证件号,客户姓名,联系电话,房间号,入住时间,预入住时,钟点房标志位)
AS
SELECT LIVE.Lid,LIVE.Lcustomerid,CUSTOMER.Cname,CUSTOMER.Cnumber,LIVE.Lrootid,Lstart,Lexpect,Lmark
FROM LIVE,CUSTOMER
WHERE LIVE.Lcustomerid = CUSTOMER.Cid
WITH CHECK OPTION;
go
--房间空闲表
CREATE VIEW FREE_ROOM(房间号,房间类型编号,房间单价,房间押金,是否可为钟点房,钟点房价格)
AS
SELECT ROOM.Rid,ROOM.Rroomtype,ROOMTYPE.RTprice,RTdeposit,RTjudge,RTjdprice
FROM ROOM,ROOMTYPE
WHERE (ROOM.Rroomtype = ROOMTYPE.RTid AND ROOM.Rstate = 'False')
WITH CHECK OPTION;
go
--服务消费总金额表
CREATE VIEW CUSTOMER_SERVICE(入住编号,服务内容,服务金额)
AS
SELECT SERVICE.SCid,CSERVICE.CScontent,CSERVICE.CSprice
FROM SERVICE,CSERVICE
where SERVICE.SLid = CSERVICE.CSid
WITH CHECK OPTION;
go
添加测试数据
--添加数据
--INSERT INTO table_name (column1,column2,column3,...)
--VALUES (value1,value2,value3,...);
--客户表加数据
INSERT INTO CUSTOMER (Cid,Cname,Csex,Cnumber)
VALUES ('444555200201283625','lnng','男','17837712772');
INSERT INTO CUSTOMER (Cid,Cname,Csex,Cnumber)
VALUES ('414455200201283626','樊星','男','15937711673');
INSERT INTO CUSTOMER (Cid,Cname,Csex,Cnumber)
VALUES ('678876200201283627','李月','女','17037711784');
--房间类型表加数据
INSERT INTO ROOMTYPE (RTid,RTprice,RTdeposit,RTjudge,RTjdprice)
VALUES ('0001','200','200','1','50');
INSERT INTO ROOMTYPE (RTid,RTprice,RTdeposit,RTjudge,RTjdprice)
VALUES ('0002','400','400','0','0');
INSERT INTO ROOMTYPE (RTid,RTprice,RTdeposit,RTjudge,RTjdprice)
VALUES ('0003','600','600','0','0');
--房间表room加数据
INSERT INTO ROOM (Rid,Rroomtype,Rstate)
VALUES ('201','0001','1');
INSERT INTO ROOM (Rid,Rroomtype,Rstate)
VALUES ('301','0002','0');
INSERT INTO ROOM (Rid,Rroomtype,Rstate)
VALUES ('401','0003','0');
--入住信息表live加数据
INSERT INTO LIVE (Lrootid,Lcustomerid,Lstart,Lexpect,Lmark)
VALUES ('201','444555200201283625',getdate(),dateadd(d, 1, getdate()),0);
--存在的服务CSERVICE加数据
INSERT INTO CSERVICE (CScontent,CSprice)
VALUES ('可乐','3');
INSERT INTO CSERVICE (CScontent,CSprice)
VALUES ('午饭','20');
--服务表service加数据
INSERT INTO SERVICE (SLid,SCid)
VALUES ('1','1');
INSERT INTO SERVICE (SLid,SCid)
VALUES ('2','1');
--员工表employees加数据
INSERT INTO EMPLOYEES (Ename,Esex,Eposition)
VALUES ('王晨','男','经理');
INSERT INTO EMPLOYEES (Ename,Esex,Eposition)
VALUES ('王月','女','管家');
----结算表settlement
INSERT INTO SETTLEMENT (Sliveid,Semployeesid)
VALUES ('1','1');
效果图