赞
踩
大二上学期学习了数据库,主要讲的是Sql Server,下学期开设的数据库课程设计要求将数据库和前端结合起来自己设计一个系统,我选择的是汽车租聘管理系统。我们不允许使用MySQL,只能用Sql Server,但其实二者区别不大,系统本身也不是很复杂,大部分都是很基本的crud。
在前期老师要求我们提交相应的数据库后台设计,包括触发器和存储过程等等,个人感觉存储过程很繁琐而且在后续没什么作用,因为系统是基于SpringBoot的,JDBC直接在mapper中编写SQL语句来实现crud更便捷(编写好的存储过程也可以放在mapper中使用,但是好像要通过实现类完成)。
下面展示部分系统界面和SQL代码,前后端代码过多就不展示了。
随着国内经济的快速发展,汽车租赁行业也如同繁星一般呈现欣欣向荣之势。然而,传统的人工管 理方法已经无法适应日益庞大的业务信息,必须开发出全新的管理方法,以期达到提高管理效率的目 的。因此,我们提出了建立一套集人员管理,车辆的各项性能管理于一体的租赁管理系统的选题。
该系统利用计算机网络以及数据库等高科技手段,旨在打破传统汽车租赁管理系统所存在的问题, 完善现有系统的不足之处。首先,该系统将实现管理的计算机辅助,使得管理便捷化、高效化。其次, 该系统不仅能够单独使用,还能够配合现有的其他汽车租赁管理系统使用,来补全其功能。同时,该系 统还能够配合前台网站,实现客户在线看、选、租车,租赁方根据客户要求为客户提供服务的一体化进程。
汽车租凭的整个业务流程中,主要的参与者是客户和业务员以及经理
查询个人信息、查询汽车信息、查询当前租凭信息、查询历史租凭信息、查询充值信息、查询扣款 信息、修改个人信息、租凭汽车、归还汽车、充值
查询客户信息、查询个人信息、查询汽车信息、查询租凭登记信息、查询租凭归还信息、查询充值 信息、查询扣款信息、修改个人信息、租凭汽车、归还汽车、充值、扣款、增加汽车信息、删除汽车信 息、更改汽车信息
查询客户信息、查询个人信息、查询汽车信息、查询租凭登记信息、查询租凭归还信息、查询充值 信息、查询扣款信息、修改个人信息、租凭汽车、归还汽车、充值、扣款
能够对所有数据进行增删改查
图1 系统功能
图2 系统管理员模块
图3 用户模块
图4 系统管理员功能
图5 登陆功能
图6 查询功能
图7 交易功能
图8 汽车管理功能
图9 交易功能
图10 注册功能
图11 加工“用户管理”分解
图12 加工“信息管理”分解
图13 加工“查询”分解
图14 加工“修改”分解
图15 加工“汽车管理”分解
图16 加工“交易”分解
这部分涉及的图片和数据太多了 就不过多展示了
客户信息(客户编号 ,客户姓名,性别,身份证号,电话,住址)
客户登录信息(客户账号 ,客户密码,客户编号)
客户余额信息(客户账号 ,余额)
业务员信息(业务员编号 ,业务员姓名,性别,电话,住址)
业务员登录信息(业务员账号 ,业务员密码,业务员编号)
经理信息(经理编号 ,经理姓名,经理账号,经理密码,性别,电话,住址)
经理登录信息(经理账号 ,经理密码,经理编号)
系统管理员信息(管理员编号 ,管理员姓名,管理员账号,管理员密码,性别,电话,住址) 系统管理员登录信息(管理员账号 ,管理员密码,经理编号)
汽车信息(车辆编号 ,车牌号,车辆型号,颜色,每日费用,超期每日费用,租凭状态)
租凭登记信息(租凭单号 ,客户编号,车辆编号,每日费用,超期每日费用,租凭天数,租凭日期,应 还日期)
租凭归还信息(租凭单号 ,客户编号,车辆编号,租凭日期,应还日期,归还日期,租期内费用,超期 费用,
车辆损坏费用,总费用)
充值信息(充值单号 ,客户编号,充值方式,充值金额,充值时间)
扣款信息(租凭单号 ,客户编号,扣款金额,扣款时间)
所有属性都是原子的,因此不需要进行拆分。
在客户信息(客户编号 ,客户姓名,性别,身份证号,客户账号,客户密码,余额,电话,住址)中, 客户账号和客户密码不完全依赖于主键客户编号,因此需要将其拆分为一个新的关系模式客户登录信息 (客户账号 ,客户密码,客户编号),业务员信息、经理信息、系统管理员信息同理。
租凭登记信息(租凭单号 ,客户编号,车辆编号,每日费用,超期每日费用,租凭天数,租凭日期,应 还日期)中,每日费用和超期每日费用都依赖于车辆编号,而不是客户编号,因此需要将其拆分为一个 新的关系模式汽车信息(车辆编号 ,车牌号,车辆型号,颜色,每日费用,超期每日费用,租凭状态),并将车辆编号添加到租凭登记信息中作为外键。
在租凭归还信息(租凭单号,客户编号,车辆编号,租凭日期,应还日期,归还日期,租期内费用,超 期费用,车辆损坏费用,总费用)中,租期内费用和超期费用依赖于租凭天数和每日费用,而每日费用 已经存在于车辆信息中,因此需要将租期内费用和超期费用移动到租凭登记信息中。
在充值信息(充值单号,客户编号,充值方式,充值金额,充值时间)和扣款信息(租凭单号,客户编 号,扣款金额,扣款时间)中,客户编号和充值/扣款金额存在多值依赖,因为一个客户可以进行多次 充值或扣款,因此需要将其拆分为两个新的关系模式客户信息(客户编号,客户姓名,性别,身份证号,电话,住址)和账户信息(客户账号,余额),并将客户编号添加到充值信息和扣款信息中作为外键。
create database car_rental;
- create table clientInfo
- (
- clientID nvarchar(12) primary key not null,
- clientName nvarchar(12) not null,
- gender nvarchar(2) check(gender in('男 ','女')) not null,
- clientCard nvarchar(18) unique not null,
- phone nvarchar(11) not null,
- address nvarchar(100) null
- );
- create table clientlogin
- (
- clientAcct nvarchar(12) primary key not null,
- clientPwd nvarchar(24) not null,
- clientID nvarchar(12) unique not null
- );
- create table clientAcct
- (
- clientID nvarchar(12) primary key not null,
- balance decimal check(balance>=0) not null default 0
- );
- create table saleInfo
- (
- saleID nvarchar(12) primary key not null,
- saleName nvarchar(12) not null,
- gender nvarchar(2) check(gender in('男 ','女')) not null,
- phone nvarchar(11) not null,
- address nvarchar(100) null
- );
- create table salelogin
- (
- saleAcct nvarchar(12) primary key not null,
- salePwd nvarchar(24) not null,
- saleID nvarchar(12) unique not null
- );
- create table managerInfo
- (
- managerID nvarchar(12) primary key not null,
- managerName nvarchar(12) not null,
- gender nvarchar(2) check(gender in('男 ','女')) not null,
- phone nvarchar(11) not null,
- address nvarchar(100) null
- );
- create table managerlogin
- (
- managerAcct nvarchar(12) primary key not null,
- managerPwd nvarchar(24) not null,
- managerID nvarchar(12) unique not null
- );
- create table adminInfo
- (
- adminID nvarchar(12) primary key not null,
- adminName nvarchar(12) not null,
- gender nvarchar(2) check(gender in('男 ','女')) not null,
- phone nvarchar(11) not null,
- address nvarchar(100) null
- );
- create table adminlogin
- (
- adminAcct nvarchar(12) primary key not null,
- adminPwd nvarchar(24) not null,
- adminID nvarchar(12) unique not null
- );
- create table carInfo
- (
- carID nvarchar(11) primary key not null,
- license nvarchar(9) unique not null,
- photo varbinary(max),
- category nvarchar(24) not null,
- color nvarchar(12) not null,
- daily_expend decimal not null,
- overdue_daily_expend decimal not null,
- lease_state nvarchar(2) check(lease_state in ('是 ','否')) not null );
- create table leaseInfo
- (
- checkID nvarchar(11) primary key not null,
- clientID nvarchar(12) not null,
- carID nvarchar(11) not null,
- daily_expend decimal not null,
- overdue_daily_expend decimal not null,
- day int not null,
- leasetime datetime not null,
- time_to_return datetime not null,
- foreign key(clientID) references clientInfo(clientID),
- foreign key(carID) references carInfo(carID)
- );
- create table returnInfo
- (
- checkID nvarchar(11) primary key not null,
- clientID nvarchar(12) not null,
- carID nvarchar(11) not null,
- leasetime datetime not null,
- time_to_return datetime not null,
- returntime datetime not null,
- expend decimal not null,
- overdue_expend decimal not null,
- damage_expend decimal not null,
- total_expend decimal not null,
- foreign key(clientID) references clientInfo(clientID),
- foreign key(carID) references carInfo(carID)
- );
- create table rechargeInfo
- (
- rechargeID nvarchar(11) primary key not null,
- clientID nvarchar(12) not null,
- remanner nvarchar(6) not null,
- remoney decimal not null,
- retime datetime not null,
- foreign key(clientID) references clientAcct(clientID)
- );
- create table deductInfo
- (
- checkID nvarchar(11) primary key not null,
- clientID nvarchar(12) not null,
- demoney decimal not null,
- detime datetime not null,
- foreign key(checkID) references leaseInfo(checkID),
- foreign key(clientID) references clientAcct(clientID)
- );
- insert into clientInfo values('202301010001','王叶
- 小 ','男 ','320683200203139018','13584648402','八角亭小区');
- insert into clientInfo values('202301010002','张雨
- 晗','男 ','321673200309198932','15062707897','幸福小区');
- insert into clientInfo values('202301010003','施金
- 丹','男 ','311683200308189099','13142681776','区山寺小区');
- insert into clientInfo values('202301010004','秦大
- 帅 ','男 ','315693200106171656','13643374854','丽上明郡');
- insert into clientInfo values('202301010005','辰
- 溥','男 ','320782200308081899','14737389335','区金色城品');
- insert into clientInfo values('202301010006','王二
- 小 ','男 ','325683200301213221','13964355646','维也纳之都');
- insert into clientInfo values('202301010007','金木
- 研','男 ','321684200301099019','13951323983','碧桂园');
- insert into clientInfo values('202301010008','木子
- 汐 ','女','322683200206169899','18958999899','碧桂园');
触发器创建后只需在客户信息表中插入数据,会自动插入数据到客户登录表)
- insert into clientlogin values('202301010001','wyx12138wyx','202301010001');
- insert into clientlogin values('202301010002','zyh666nb','202301010002');
- insert into clientlogin values('202301010003','jdjd123321','202301010003');
- insert into clientlogin values('202301010004','qyy6060Qyy','202301010004');
- insert into clientlogin values('202301010005','cppccppc123','202301010005');
- insert into clientlogin values('202301010006','wwexx36221','202301010006');
- insert into clientlogin values('202301010007','jmy0616JMZ','202301010007');
- insert into clientlogin values('202301010008','mzxmzx666','202301010008');
(触发器创建后只需在客户信息表中插入数据,会自动插入数据到客户余额表)
- insert into clientAcct values('202301010001',0);
- insert into clientAcct values('202301010002',0);
- insert into clientAcct values('202301010003',0);
- insert into clientAcct values('202301010004',0);
- insert into clientAcct values('202301010005',0);
- insert into clientAcct values('202301010006',0);
- insert into clientAcct values('202301010007',0);
- insert into clientAcct values('202301010008',0);
- insert into saleInfo values('202301011001','熊大','男','13863684957','白熊山一号树洞');
- insert into saleInfo values('202301011002','熊二','男','13782769362','白熊山二号树洞');
- insert into saleInfo values('202301011003','胡图图','男','13424367689','翻斗大街翻斗花园二号楼1001室');
(触发器创建后只需在业务员信息表中插入数据,会自动插入数据到业务员登录表)
- insert into salelogin values('202301011001','xcm123456XCM','202301011001');
- insert into salelogin values('202301011002','xcm654321','202301011002');
- insert into salelogin values('202301011003','htt12315ht','202301011003');
insert into managerInfo values('202301012001','艾伦·耶格尔','男','13862483666','帕拉迪岛');
(触发器创建后只需在经理信息表中插入数据,会自动插入数据到 经理登录表)
insert into managerlogin values('202301012001','ttk2022final','202301012001');
insert into adminInfo values('202301013001','张三','男 ','13011083460','润和西路66号');
insert into adminlogin values('202301013001','fwktjsw333','202301013001');
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101401','京A00001',BulkColumn,'兰博基尼 ','橙',200,220,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\兰博基尼 (橙) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101402','京A00002',BulkColumn,'劳斯莱斯幻影','蓝',190,210,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\劳斯莱斯幻影 (蓝) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101403','京A00003',BulkColumn,'玛莎拉蒂 ','黑 ',180,200,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\玛莎拉蒂 (黑) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101404','京A00004',BulkColumn,'法拉利 ','黄',170,190,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\法拉利 (黄) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101405','京A00005',BulkColumn,'宝马X6','黑 ',150,170,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\宝马X6 (黑) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101406','京A00006',BulkColumn,'宝马X5','蓝',140,160,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\宝马X5 (蓝) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101407','京A00007',BulkColumn,'宝马X5','白 ',140,160,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\宝马X5 (白) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101408','京A00008',BulkColumn,'特斯拉Model S','蓝',135,155,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\特斯拉Model S (蓝) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101409','京A00009',BulkColumn,'特斯拉Model S','黑 ',135,155,'否 '
-
- from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\特斯拉Model S (黑) .png', SINGLE_BLOB) AS Image
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101410','京A00010',BulkColumn,'特斯拉Model S','白 ',135,155,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\特斯拉Model S (白) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101411','鲁K00011',BulkColumn,'奥迪A6','橙',120,140,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\奥迪A6 (蓝) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101412','鲁K00012',BulkColumn,'奥迪A6','灰',120,140,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\奥迪A6 (灰) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101413','鲁K00013',BulkColumn,'奥迪A6','黑 ',120,140,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\奥迪A6 (黑) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101414','鲁K00014',BulkColumn,'奔驰E300','银',100,120,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\奔驰E300 (银) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101415','鲁K00015',BulkColumn,'奔驰E300','黑 ',100,120,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\奔驰E300 (黑) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101416','鲁K00016',BulkColumn,'奔驰E300','白 ',100,120,'否 ' from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\奔驰E300 (白) .png', SINGLE_BLOB) AS Image
-
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101417','鲁K00017',BulkColumn,'大众朗逸','紫红',70,90,'否 '
- from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\大众朗逸 (紫红) .png', SINGLE_BLOB) AS Image
- insert into carInfo(carID, license, photo, category, color, daily_expend, overdue_daily_expend, lease_state)
- select '20230101418','鲁K00018',BulkColumn,'大众朗逸','蓝',70,90,'否 '
- from OPENROWSET(BULK 'C:\Users\HUAWEI\Desktop\CarPhoto\大众朗逸 (蓝) .png', SINGLE_BLOB) AS Image
- insert into leaseInfo values('20230303501','202301010007','20230101401','200','220','2','2023-03-03 08:00','2023-03-05 08:00')
- insert into leaseInfo values('20230303502','202301010001','20230101402','190','210','2','2023-03-03 08:00','2023-03-05 08:00')
- insert into leaseInfo values('20230303503','202301010002','20230101404','180','200','2','2023-03-03 08:00','2023-03-05 08:00')
- insert into leaseInfo values('20230303504','202301010003','20230101405','150','170','2','2023-03-03 08:00','2023-03-05 08:00')
- insert into leaseInfo values('20230303505','202301010004','20230101407','140','160','2','2023-03-03 08:00','2023-03-05 08:00')
- insert into leaseInfo values('20230303506','202301010005','20230101408','135','155','2','2023-03-03 08:00','2023-03-05 08:00')
- insert into leaseInfo values('20230303507','202301010006','20230101410','135','155','2','2023-03-03 08:00','2023-03-05 08:00')
- insert into leaseInfo values('20230303508','202301010008','20230101411','120','140','2','2023-03-03 08:00','2023-03-05 08:00')
- insert into returnInfo values('20230303501','202301010007','20230101401','2023-03-03 08:00','2023-03-05 08:00','2023-03-05 07:30','400','0','0','400')
- insert into returnInfo values('20230303502','202301010001','20230101402','2023-03-03 08:00','2023-03-05 08:00','2023-03-05 07:30','380','0','0','380')
- insert into returnInfo values('20230303503','202301010002','20230101404','2023-03-03 08:00','2023-03-05 08:00','2023-03-05 07:30','360','0','0','360')
- insert into returnInfo values('20230303504','202301010003','20230101405','2023-03-03 08:00','2023-03-05 08:00','2023-03-06 07:30','300','170','0','470')
- insert into returnInfo values('20230303505','202301010004','20230101407','2023-03-03 08:00','2023-03-05 08:00','2023-03-05 07:30','280','0','1000','1280')
- insert into returnInfo values('20230303506','202301010005','20230101408','2023-03-03 08:00','2023-03-05 08:00','2023-03-05 07:30','270','0','0','270')
- insert into returnInfo values('20230303507','202301010006','20230101410','2023-03-03 08:00','2023-03-05 08:00','2023-03-05 07:30','270','0','0','270')
- insert into returnInfo values('20230303508','202301010008','20230101411','2023-03-03 08:00','2023-03-05 08:00','2023-03-07 07:30','220','280','0','500')
- insert into rechargeInfo values('20230101601','202301010001','现金',9999,'2023- 01-01 08:14');
- insert into rechargeInfo values('20230101602','202301010002','微信支 付',9999,'2023-02-12 14:41');
- insert into rechargeInfo values('20230101603','202301010003','支付宝 ',9999,'2023- 02-21 09:11');
- insert into rechargeInfo values('20230101604','202301010004','现金',9999,'2023- 02-23 13:15');
- insert into rechargeInfo values('20230101605','202301010005','现金',9999,'2023- 02-25 15:23');
- insert into rechargeInfo values('20230101606','202301010006','支付宝 ',9999,'2023- 02-27 17:11');
- insert into rechargeInfo values('20230101607','202301010007','微信支 付',9999,'2023-03-01 09:10');
- insert into rechargeInfo values('20230101608','202301010008','微信支 付',9999,'2023-03-03 16:15');
insert into deductInfo values('20230303501','202301010007',400,'2023-03-05 07:30');
1. 访问控制:建立用户账户和密码,对用户进行身份认证和授权,限制用户可以访问的数据库和数据 表,以防止未授权的访问。
2. 数据加密:对数据库中的敏感数据进行加密,防止数据泄露和非授权访问。常用的加密技术包括对 称加密、非对称加密和哈希加密等。
3. 审计日志:记录数据库的操作日志,包括用户登录、查询、修改和删除等操作,以便跟踪和审计数 据库的使用情况。
4. 数据备份与恢复:定期备份数据库,以防止数据丢失或损坏。备份数据应存储在安全的位置,并采 取恰当的措施确保备份数据的机密性和完整性。
5. 安全更新管理:定期对数据库进行安全更新,以修复已知漏洞和弱点,确保数据库的安全性。
6. 物理安全:保护数据库服务器的物理安全,包括控制物理访问、加固服务器和防火墙等措施,以保 障数据库的安全性。
1. 主键约束:每个表应该有一个主键,以确保表中每一行都具有唯一标识符。在can_ rental数据库 中,每个表都有主键。
2. 外键约束:使用外键约束可以确保表之间的引用完整性。在can_ rental数据库中, leaseI nfo表和 returnI nfo表中的clientID和 carID列都是外键,它们分别参考于clientI nfo表和carI nfo表中的 clientID和carID列。同时, rechargeI nfo表和deductI nfo表中的clientID列也是外键,参考于 clientAcct表中的clientID列。这些外键约束可以确保只有存在于父表中的数据才能被插入到子表 中。
3. 非空约束:使用非空约束可以确保列中的值不为空。在can_ rental数据库中, clientI nfo表中的 clientID、clientName、gender、clientCard和phone列, clientlogin表中的clientAcct、 clientPwd和clientID列, clientAcct表中的clientID和balance列, saleI nfo表中的saleID、 saleName、gender、 phone列, salelogin表中的saleAcct、salePwd和saleID列, managerI nfo表 中的managerID、 managerName、gender、 phone列, managerlogin表中的managerAcct、 managerPwd和managerID列, adminI nfo 表中的 adminID、adminName、gender、 phone列, adminlogin表中的adminAcct、adminPwd和adminID列, carI nfo表中的carID、 license、 category 、color、daily_expend、overdue_daily_expend和lease_state列, leaseI nfo表中的 checkID、clientID、carID、daily_expend、overdue_daily_expend、day 、 leasetime和 time_to_ return列, returnI nfo表中的checkID、clientID、carID、 leasetime、time_to_ return、 returntime、expend、overdue_expend、damage_expend和total_expend列, rechargeI nfo表 中的rechargeID、clientID、 remanner、 remoney和retime列,以及deductI nfo表中的checkID、 clientID、demoney和detime列都被定义为非空列,这可以确保这些列中不会存在空值。
4. 唯一约束:使用唯一约束可以确保列中的值是唯一的。在can_ rental数据库中, clientI nfo表中的 clientCard ,clientlogin表中的clientID ,salelogin表中的saleID,managerlogin表中的 managerID,adminlogin表中的adminID,carI nfo表中的license。
5. 检查约束:使用检查约束可以确保列中的值符合特定的条件。在can_ rental数据库中, clientI nfo 表中的gender列, carI nfo表中的lease_state列,以及deductI nfo表中的demoney列都有检查约 束。这可以确保这些列中的值符合特定的要求。
6. 默认约束:使用默认约束可以为列中的值提供默认值, clientAcct表中的balance默认为0。
- -- 创建一个视图,显示客户的姓名和余额
- create view clientBalance
- as
- select clientName,balance
- from clientInfo join clientAcct
- on clientInfo.clientID = clientAcct.clientID
- -- 创建一个视图,显示客户的租车记录和对应的汽车信息
- create view clientLeaseRecord
- as
- select leaseInfo.checkID, leaseInfo.clientID, carInfo.license, carInfo.category, carInfo.color, leaseInfo.leasetime, leaseInfo.time_to_return
- from leaseInfo
- join carInfo on leaseInfo.carID = carInfo.carID
- left join returnInfo on leaseInfo.checkID = returnInfo.checkID
- -- 创建一个视图,显示租凭登记信息和对应的汽车信息
- create view leaseCarInfo
- as
- select leaseInfo.checkID, leaseInfo.clientID, leaseInfo.day, carInfo.license, carInfo.category, carInfo.color,carInfo.daily_expend,carInfo.overdue_daily_expend, leaseInfo.leasetime, leaseInfo.time_to_return
- from leaseInfo join carInfo
- on leaseInfo.carID = carInfo.carID
- -- 创建一个视图,显示租凭归还信息和对应的汽车信息
- create view returnCarInfo
- as
- select returnInfo.checkID, returnInfo.clientID, carInfo.license, carInfo.category, carInfo.color, carInfo.daily_expend,carInfo.overdue_daily_expend, returnInfo.leasetime, returnInfo.time_to_return, returnInfo.expend, returnInfo.overdue_expend, returnInfo.damage_expend, returnInfo.total_expend
- from returnInfo join carInfo
- on returnInfo.carID = carInfo.carID
- -- 为汽车信息表的颜色和类别列创建联合索引
- create index idx_car_color_category on carInfo(color, category);
- -- 为租凭登记信息表的客户ID列和汽车ID列创建联合索引
- create index idx_lease_client_car on leaseInfo(clientID, carID);
- -- 为租凭登记信息表的出租时间和应还时间列创建索引
- create index idx_lease_time on leaseInfo(leasetime, time_to_return);
- -- 为租凭归还信息表的出租时间和应还时间以及归还时间创建索引
- create index idx_return_time on returnInfo(leasetime, time_to_return, returntime);
- -- 实现在向clientInfo中插入记录时clientID按格式自增长,且同时向clientAcct和clientlogin中插入相应记录,其中clientlogin中的clientAcct等于clientID,clientPwd默认为123456
- create trigger clientInfo_autoID
- on clientInfo
- after insert
- as
- begin
- declare @maxID nvarchar(12), @newID nvarchar(12), @prefix nvarchar(8), @suffix nvarchar(3)
- set @prefix = convert(nvarchar(8), getdate(), 112)
- set @suffix = '001'
- set @maxID = (select max(clientID) from clientInfo where left(clientID, 8) = @prefix)
- IF @maxID is not null
- begin
- set @suffix = right('000' + cast(cast(right(@maxID, 3) as int) + 1 as nvarchar(3)), 3)
- end
- set @newID = @prefix + '0' + @suffix + '1'
-
- insert into clientlogin (clientAcct, clientPwd, clientID)
- select @newID, '123456', @newID;
-
- insert into clientAcct (clientID, balance)
- select @newID, 0;
-
- update clientInfo set clientID = @newID where clientID = (select clientID from inserted)
- end;
- -- 实现在向saleInfo中插入记录时saleID按格式自增长,且同时向salelogin中插入相应记录,其中salelogin中的saleAcct等于saleID,clientPwd默认为123456
- create trigger saleInfo_autoID
- on saleInfo
- after insert
- as
- begin
- declare @maxID nvarchar(12), @newID nvarchar(12), @prefix nvarchar(8), @suffix nvarchar(3)
- set @prefix = convert(nvarchar(8), getdate(), 112)
- set @suffix = '001'
- set @maxID = (select max(saleID) from saleInfo where left(saleID, 8) = @prefix)
- IF @maxID is not null
- begin
- set @suffix = right('000' + cast(cast(right(@maxID, 3) as int) + 1 as nvarchar(3)), 3)
- end
- set @newID = @prefix + '1' + @suffix
-
- insert into salelogin (saleAcct, salePwd, saleID)
- select @newID, '123456', @newID;
-
- update saleInfo set saleID = @newID where saleID = (select saleID from inserted)
- end;
- -- 实现在向managerInfo中插入记录时managerID按格式自增长,且同时向managerlogin中插入相应记录,其中managerlogin中的managerAcct等于managerID,managerPwd默认为123456
- create trigger managerInfo_autoID
- on managerInfo
- after insert
- as
- begin
- declare @maxID nvarchar(12), @newID nvarchar(12), @prefix nvarchar(8), @suffix nvarchar(3)
- set @prefix = convert(nvarchar(8), getdate(), 112)
- set @suffix = '001'
- set @maxID = (select max(managerID) from managerInfo where left(managerID, 8) = @prefix)
- IF @maxID is not null
- begin
- set @suffix = right('000' + cast(cast(right(@maxID, 3) as int) + 1 as nvarchar(3)), 3)
- end
- set @newID = @prefix + '2' + @suffix
-
- insert into managerlogin (managerAcct, managerPwd, managerID)
- select @newID, '123456', @newID;
-
- update managerInfo set managerID = @newID where managerID = (select managerID from inserted)
- end;
- -- 实现在向adminInfo中插入记录时adminID按格式自增长,且同时向adminlogin中插入相应记录,其中adminlogin中的adminAcct等于adminID,adminPwd默认为123456
- create trigger adminInfo_autoID
- on adminInfo
- after insert
- as
- begin
- declare @maxID nvarchar(12), @newID nvarchar(12), @prefix nvarchar(8), @suffix nvarchar(3)
- set @prefix = convert(nvarchar(8), getdate(), 112)
- set @suffix = '001'
- set @maxID = (select max(adminID) from adminInfo where left(adminID, 8) = @prefix)
- IF @maxID is not null
- begin
- set @suffix = right('000' + cast(cast(right(@maxID, 3) as int) + 1 as nvarchar(3)), 3)
- end
- set @newID = @prefix + '3' + @suffix
-
- insert into adminlogin (adminAcct, adminPwd, adminID)
- select @newID, '123456', @newID;
-
- update adminInfo set adminID = @newID where adminID = (select adminID from inserted)
- end;
- -- 实现在向rechargeInfo表中插入数据时同时更新clientAcct表中balance余额
- create trigger rechargeInfo_update_clientAcct
- on rechargeInfo
- after insert
- as
- begin
- update clientAcct
- set balance = balance + i.remoney
- from clientAcct c
- inner join inserted i on c.clientID = i.clientID;
- end
- -- 在returnInfo表插入操作之后自动将相关数据插入到deductInfo表中
- create trigger returnInfo_insert_deductInfo
- on returnInfo
- after insert
- as
- begin
- declare @clientID nvarchar(12), @demoney decimal(10,2), @detime datetime, @checkID nvarchar(11);
- select @clientID = inserted.clientID, @detime = inserted.returntime, @checkID = inserted.checkID,
- @demoney = inserted.expend + inserted.overdue_expend + inserted.damage_expend
- from inserted;
-
- insert into deductInfo (checkID, clientID, demoney, detime)
- values (@checkID, @clientID, @demoney, @detime);
- end
- -- 实现在向deductInfo表中插入数据时同时更新clientAcct表中balance余额,并且在余额不足时拒绝插入数据同时给出提示
- create trigger deductInfo_update_clientAcct
- on deductInfo
- after insert
- as
- begin
- declare @new_balance decimal(10, 2)
-
- update clientAcct
- set @new_balance = balance - i.demoney
- from clientAcct c
- inner join inserted i on c.clientID = i.clientID;
-
- if @new_balance < 0
- begin
- raiserror ('余额不足,请充值!', 16, 1);
- rollback transaction;
- end
- else
- begin
- update clientAcct
- set balance = @new_balance
- from clientAcct c
- inner join inserted i on c.clientID = i.clientID;
- end
- end
- -- 实现向leaseInfo中插入数据时自动修改carInfo中对应汽车的租凭状态为'是'
- create trigger update_carInfo_leasestate1
- on leaseInfo
- after insert
- as
- begin
- update carInfo
- set lease_state = '是'
- from carInfo
- inner join inserted on carInfo.carID = inserted.carID;
- end
- -- 实现向returnInfo中插入数据时自动修改carInfo中对应汽车的租凭状态为'否'
- create trigger update_carInfo_leasestate2
- on returnInfo
- after insert
- as
- begin
- update carInfo
- set lease_state = '否'
- from carInfo
- inner join inserted on carInfo.carID = inserted.carID;
- end
都是基本的crud,不多展示了
- -- 查询客户余额函数
- create function dbo.get_balance (@clientID nvarchar(12))
- returns decimal
- as
- begin
- declare @balance decimal
- select @balance = balance from clientAcct where clientID = @clientID
- return @balance
- end
- -- 计算租凭期内费用expend 接受租凭单号作为输入参数
- create function dbo.calculate_expend(@checkID nvarchar(11))
- returns decimal
- as
- begin
- declare @carID nvarchar(11)
- declare @day int
- declare @daily_expend decimal
- declare @expend decimal
-
- select @carID = carID , @day = day from leaseInfo where checkID = @checkID
- select @daily_expend = daily_expend from carInfo where carID = @carID
- set @expend = @daily_expend * @day
- return @expend
- end
- -- 计算超期费用overdue_expend 接受租凭单号、还车时间作为输入参数
- create function dbo.calculate_overdue_expend(@checkID nvarchar(11), @returntime datetime)
- returns decimal
- as
- begin
- declare @carID nvarchar(11)
- declare @time_to_return datetime
- declare @overdue_daily_expend decimal
- declare @overdue_expend decimal
-
- select @carID = carID from leaseInfo where checkID = @checkID
- select @overdue_daily_expend = overdue_daily_expend from carInfo where carID = @carID
- select @time_to_return = time_to_return from leaseInfo where checkID = @checkID
- if @returntime > @time_to_return
- set @overdue_expend = @overdue_daily_expend * DATEDIFF(day, @time_to_return, @returntime)
- else
- set @overdue_expend = 0
- return @overdue_expend
- end
- -- 计算租凭总费用total_expend函数 接受租凭单号、还车时间、损坏费用作为输入参数
- create function dbo.calculate_total_expend(@checkID nvarchar(11), @returntime datetime, @damage_expend decimal)
- returns decimal
- as
- begin
- declare @carID nvarchar(11)
- declare @leasetime datetime
- declare @time_to_return datetime
- declare @daily_expend decimal
- declare @overdue_daily_expend decimal
- declare @day int
- declare @expend decimal
- declare @overdue_expend decimal
- declare @total_expend decimal
-
- -- 获取租赁信息和汽车信息
- select @day = day from leaseInfo where checkID = @checkID
- select @carID = carID, @leasetime = leasetime, @time_to_return = time_to_return from leaseInfo where checkID = @checkID
- select @daily_expend = daily_expend, @overdue_daily_expend = overdue_daily_expend from carInfo where carID = @carID
-
- -- 计算租赁费用
- if @returntime > @time_to_return
- select @expend = @daily_expend * @day, @overdue_expend = @overdue_daily_expend * datediff(day, @time_to_return, @returntime)
- else
- select @expend = @daily_expend * @day, @overdue_expend = 0
-
- -- 计算总费用
- set @total_expend = @expend + @overdue_expend + @damage_expend
-
- return @total_expend
- end
- -- 检查汽车是否可租赁函数
- create function dbo.check_car_leasable (@carID nvarchar(11))
- returns nvarchar(20)
- as
- begin
- declare @lease_state nvarchar(2)
- declare @str nvarchar(20)
- select @lease_state = lease_state from carInfo where carID = @carID
- if @lease_state = '是'
- set @str = '该汽车已出租'
- else
- set @str = '该汽车未出租'
-
- return @str
- end
- -- 实现在向managerInfo中插入记录时managerID按格式自增长,且同时向managerlogin中插入相应记录,其中managerlogin中的managerAcct等于managerID,managerPwd默认为123456
- create trigger managerInfo_autoID
- on managerInfo
- after insert
- as
- begin
- declare @maxID nvarchar(12), @newID nvarchar(12), @prefix nvarchar(8), @suffix nvarchar(3)
- set @prefix = convert(nvarchar(8), getdate(), 112)
- set @suffix = '001'
- set @maxID = (select max(managerID) from managerInfo where left(managerID, 8) = @prefix)
- IF @maxID is not null
- begin
- set @suffix = right('000' + cast(cast(right(@maxID, 3) as int) + 1 as nvarchar(3)), 3)
- end
- set @newID = @prefix + '2' + @suffix
-
- insert into managerlogin (managerAcct, managerPwd, managerID)
- select @newID, '123456', @newID;
-
- update managerInfo set managerID = @newID where managerID = (select managerID from inserted)
- end;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。