SQL server 设计
目录:
1.各个表的建立
2.级联操作
3.触发器和存储过程
4权限管理
注:由于表比较多,sql语句很长,所以下面的说明中每样只举一个或两个例子,
1.各个表的建立
表1:用户
表2:维修员
表3:管理员
表4:单车
表5:运行记录
表6:位置
表7:评价反馈
–表1:用户表
drop table if exists 用户
create table 用户(
用户名 varchar(30) not null,
账号 varchar(10) primary key,
密码 varchar(10)not null,
余额 int not null,
总充值金额 int not null,
vip等级 int not null
)
–表2:维修员表
drop table if exists 维修员
create table 维修员(
维修员名 varchar(30) not null,
账号 varchar(10) primary key,
密码 varchar(10) not null,
)
–表3:管理员表
drop table if exists 管理员
create table 管理员(
管理员名 varchar(30)not null,
账号 varchar(10) primary key,
密码 varchar(10) not null
)
–表4:单车表
drop table if exists 单车
create table 单车
(
编号 varchar(10) primary key,
是否在运行 bit,
是否可用 bit
)
–表5:运行记录表
if exists (select * from dbo.sysobjects where ID = OBJECT_ID(N'运行记录') AND OBJECTPROPERTY(ID, 'IsUserTable') = 1)
drop table 运行记录
create table 运行记录(
记录编号 varchar(30) primary key,
用户账号 varchar(10) not null,
单车编号 varchar(10),
起始时间 datetime,
结束时间 datetime,
费用 int not null
)
–表6:城市地理表
drop table if exists 位置
create table 位置(
当前位置 varchar(30) not null,
在市内否 bit,
可否停车 bit
)
drop table if exists 评价反馈
create table 评价反馈(
用户账号 varchar(10) not null,
行驶记录 varchar(30)not null,
评价 int
)
2.级联操作
样例一
if exists(select * from sysobjects where name='a1')
alter table 运行记录 drop constraint a1;
alter table 运行记录 add constraint a1 foreign key(单车编号) references 单车(编号)
on update cascade;
update 单车 set 编号='00009' from 单车 where 编号='00001';
将编号为00001的单车编号换为00009,表“运行记录”中相应单车的标号也会变为00009:
3.触发器和存储过程
go
create trigger tri1
on 用户
for update
as
begin
declare @sql int
set @sql=100
update 用户 set vip等级=总充值金额/100 from 用户
where(总充值金额>@sql);
end
go
update 用户 set 总充值金额=500 from 用户 where 账号='a001';
将用户“a001”的总充值金额变为500,那么在触发器作用下其vip等级变为5:
drop function if exists f1
go
create function f1(@a datetime,@b datetime) returns int
as
begin
declare @num1 int,@num2 int
set @num1=(select datediff(hh,@a,@b))
set @num2=@num1*2
return @num2
end
go
drop function if exists f2
go
create function f2(@a2 int) returns table
as
return select * from 用户 where(余额<@a2)
go
select * from f2(20);
4.权限管理
go
create role persons;
go
grant select,update,delete,insert on 维修员 to persons with grant option;
grant select,update,delete,insert on 用户 to persons with grant option;
grant select,update,delete,insert on 管理员 to persons with grant option;
grant select,update,delete,insert on 单车 to persons with grant option;
grant select,update,delete,insert on 位置 to persons with grant option;
创建一个role,将其命名为persons,然后将各种权限授权给persons
drop user if exists person1;
create login person1 with password='123456',default_database=bicycle;
create user person1 for login person1 with default_schema=dbo;
alter role persons add member person1
创建一个用户person1,密码为123456,默认的数据库是bicycle,并将其放入persons中,这样persons有的权限person1都有了。
然后我们以用户person1的身份重新登录
可以看到我们是以person1的身份登陆的,默认链接的数据库是bicycle;
我们随便创建一个表,由于在sql中我们没有赋予person1创建表的权限,所以提示我们无权创建表。
我们重新写入sql语句,因为我们授予了person1 有select、update、delete等权限,所以我们能够完成以下语句。
相关资源:
用Delphi写一个单车管理系统
单车管理系统——1.文档设计及sql代码说明
单车管理系统——2.delphi页面说明
单车管理系统——3.delphi主要代码说明
完整的文件代码及设计文档
更多资源:
北京理工大学—计算机专业课程资源:https://blog.csdn.net/weixin_50836014/article/details/125687455
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)