数据库系统及应用 上机实验报告
实验1
一、实验目的:理解SQL Server数据库的存储结构,掌握SQL Server数据库的建立方法和维护方法。
二、实验内容:在SQL Server环境下建立数据库和维护数据库。
三、程序源代码:
--1
CREATE DATABASE test1
ON
(NAME=test1_dat,
FILENAME='f:\DB\data\test1dat.mdf',
SIZE= 10,
MAXSIZE= 50,
FILEGROWTH= 5 )
LOG ON
(NAME=order_log,
FILENAME='f:\DB\data\test1log.ldf',
SIZE= 5MB,
MAXSIZE= 25MB,
FILEGROWTH= 5MB)
--2
create database test2
on
primary
(name=test2_dat1,
filename='f:\DB\data\test2dat1.mdf'), (name=test2_dat2,
filename='f:\DB\data\test2dat2.ndf'), (name=test2_dat3,
filename='f:\DB\data\test2dat3.ndf') log on
(name=test2_log1,
filename='f:\DB\data\test2log1.ldf'), (name=test2_log2,
filename='f:\DB\data\test2log2.ldf') --3
create database test3
on
primary
(name=test3_dat1,
filename='f:\DB\data\test3dat1.mdf'), (name=test3_dat2,
filename='f:\DB\data\test3dat2.mdf'), filegroupg2
(name=test3_dat3,
filename='d:\DB\data\test3dat3.ndf'), (name=test3_dat4,
filename='d:\DB\data\test3dat4.ndf'), filegroupg3
(name=test3_dat5,
filename='e:\DB\data\test3dat5.ndf'), (name=test3_dat6,
filename='e:\DB\data\test3dat6.ndf') log on
(name=test3_log,
filename='f:\DB\data\test3log.ldf') --4
alter database test1
add file
(name=test1new_dat,
filename='f:\DB\data\test1newdat.ndf', size=5MB)
--5
alter database test1
modify file
(name=test1_dat,
size=15MB)
--6
dropdatabasetest3
四、实验数据、结果分析:
若没有指定size,则默认为1MB,没有指定Maxsize,文件可以增长到磁盘满为止,没有指定Filegrowth,则默认为10%。
五、总结:
CREATE DATABASE dataname 创建数据库
ALTER DATABASE database 修改数据库
DROP DATABASE dataname 删除数据库
实验2
1.创建客户表:
create table 客户
(
客户号 char(8) check(unicode(客户号) between 65 and 122) primary key,
/*check(客户号 like '[a-z]%')*/
客户名称 varchar(40) not null,
联系人 char(8),
地址 varchar(40),
邮政编码 char(6) check(len(邮政编码)=6 and isnumeric(邮政编码)=1),
/* check(邮政编码 like '[0-9][0-9][0-9][0-9][0-9][0-9]')*/
电话 char(12) check(isnumeric(电话)=1)
)
2.创建产品表:
create table 产品
(
产品号 char(8) primary key check(unicode(产品号) between 65 and 122
and unicode(substring(产品号,2,1)) between 65 and 122),
产品名称 varchar(40) unique,
规格说明 char(40),
单价 smallmoney check(单价>0)
)
3.创建订购单表:
create table 订购单
(
客户号 char(8) foreign key references 客户(客户号) not null,
订单号 char(8) primary key,
订购日期 datetime default getdate()
)
4.创建订单明细表:
create table 订单明细
(
订单号 char(8) foreign key references 订购单(订单号),
) 序号 tinyint, 产品号 char(8) foreign key references 产品(产品号) not null, 数量 smallint check(数量>0), primary key (订单号,序号)
实验3
一、实验目的:为实验2建立的表设计一组数据进行插入、删除、修改等操作,并体会数据完整性约束的作用,加深对数据完整性及其约束的理解。
二、实验内容:数据的插入、更新和删除。
三.、程序源代码:
insert into 客户 values('C001','A公司','小明','北京
','123456','143567568457')
insert into 客户 values('C002','B公司','小李','上海
','234567','146389045634')
insert into 客户(客户号,客户名称) values('C009','J商场')
insert into 产品 values('GD001','iphone','4s',5000)
insert into 产品 values('GD002','ipad','2g',5500)
insert into 产品(产品号,产品名称) values('GD010','Mac Pro')
insert into 订购单(客户号,订单号) values('C001','or01')
insert into 订购单(客户号,订单号) values('C001','or02')
insert into 订购单(订单号) values('or19')
insert into 订单明细 values('or01','1','GD001','4')
insert into 订单明细 values('or01','2','GD001','3')
insert into 订单明细 values('or01','3','GD002','6')
insert into 订单明细(订单号,序号,产品号) values('or01','28','GD002') --2
delete from 订购单 where 客户号='C001'
delete from 客户 where 客户号='C001' --受参照完整性约束
delete from 订单明细 where 订单号='or01'
delete from 订购单 where 订单号='or01' --受参照完整性约束
--3
update 订购单 set 订单号=null where 客户号='C001' --受实体完整性约束 update 订购单 set 客户号='C011' where 订单号='or04'--受参照完整性约束 update 订购单 set 客户号='C009' where 订单号='or07' --更新成功 update 订单明细 set 数量=0 where 序号='9' --受用户定义完整性约束 --4
update 订单明细 set 数量=数量+10
from 订购单
where 订购单.订单号=订单名细.订单号 and 客户号='C002'
--5
delete from 订单明细
from 订购单
where 订购单.订单号=订单明细.订单号 and 客户号='C002'
四、实验数据、结果分析:
--2
在删除客户号为“C002”的记录时无法删除,因为客户表被订购单表参照。 删除订购单号为“or01”的记录时无法删除,因为订单名细表参照订购单表。 --3
第一个更新根据实体完整性约束,订单号是主关键字,不能为空值。
第二个更新根据参照完整性约束,订购单表参照客户表,而客户表中没有客户号为“C011”的客户。
第四个更新根据用户定义完整性约束,数量必须为正整数。
--4
使客户号为C002的订购单的订购数量增加10.
--5
删掉客户号为C002的订单名细记录。
五、总结:
插入:INSERT INTO <表名>[(<列名>[,<列名>?])]
values(<表达式>[,<表达式>?])
更新:update <表名> set <列名>=<表达式>[,<列名>=<表达式>?]
[[from<表名>] where<逻辑表达式>]
删除:DELETE FROM <表名>
[[FROM <表名>]WHERE <逻辑表达式>]
完整性约束影响插入、更新和删除等操作
实验4
一、实验目的:熟练掌握SQL SELECT语句,能够运用该语句完成各种查询。
二、实验内容:用SQL SELECT语句完成各种数据查询。
三.、程序源代码:
--1
select * from 客户
--2
select 客户号 from 订购单
--3
select * from 产品 where 单价>=5000
--4
select * from 产品 where 单价>5000 and 产品名称='Macbook'
--5
select * from 产品 where 单价>6000 and 产品名称 in('Macbook','ipad')
--6
select c.客户名称,c.联系人,c.电话,o.订单号
from 客户 c,订购单 o
where o.订购日期 between '2011-10-30' and '2011-12-1'
and c.客户号=o.客户号
--7
select distinct 客户名称,联系人,电话
from 客户 c,产品 g,订购单 o,订单名细 d
where 产品名称='iphone'
and g.产品号=d.产品号
and d.订单号=o.订单号
and o.客户号=c.客户号
--8
select * from 订单名细
where 产品号 in(
select 产品号
from 产品
where 产品名称='Macbook')
--9
select * from 订购单
where 订单号 in(
select 订单号
from 订单名细
where 数量>10)
--10
select * from 产品 where 单价 =(select 单价 from 产品 where 规格说明='4s')
--11
select * from 产品 where 单价 between 1000 and 5000
--12
select * from 客户 where 客户名称 like '%集团'
--13
select * from 客户 where 客户名称 not like '%商场'
--14
select * from 产品 order by 单价
--15
select *
from 产品
order by 产品名称,单价
--16
select COUNT(产品号)
from 产品
--17
select SUM(数量)
from 订单名细
where 产品号=(select 产品号
from 产品
where 产品名称='ipad')
--18
select SUM(数量*单价) 总金额
from 产品,订单名细
where 产品.产品号=订单名细.产品号
and 产品名称='ipod nano'
--19
select COUNT(distinct 订单号)as 订购单个数,AVG(数量*单价)as 平均金额
from 产品,订单名细
where 产品.产品号=订单名细.产品号
--20
select 订单号,COUNT(订单号) 项目数,SUM(g.单价*o.数量) 总金额
from 产品 g,订单名细 o
where g.产品号=o.产品号
group by 订单号
--21
select i.订单号,MAX(数量*单价) 最高金额,MIN(数量*单价) 最低金额
from 订购单 o,产品 g,订单名细 i
where o.订单号=i.订单号
and g.产品号=i.产品号
and 产品名称='iphone'
group by i.订单号
--22
select 订单号,COUNT(*) 项目数,AVG(数量*单价) 平均金额
from 产品 g,订单名细 i
where g.产品号=i.产品号
group by 订单号
having COUNT(*)>=2
--23
select 客户名称,联系人,电话,订单号
from 客户 c,订购单 o
where c.客户号=o.客户号
and 订购日期 is null
--24
select 客户名称,联系人,电话,订单号,订购日期
from 客户,订购单
where 客户.客户号=订购单.客户号
and 订购日期>'2011-10-10'
--25
select *
from 产品 outa
where 单价=(select MAX(单价)
from 产品 innera
where outa.产品名称=innera.产品名称)
--26
select 客户号
from 客户
where not exists(
select *
from 订购单
where 客户.客户号=订购单.客户号)
--27
select *
from 客户
where exists(
select *
from 订购单
where 客户.客户号=订购单.客户号)
--28
select 产品名称
from 产品
where 单价= any(select 单价/2
from 产品)
--29
select 产品名称
from 产品
where 单价 >all(select max(单价) from 产品)
--30
--cross
select * from 客户 cross join 订购单
where 客户.客户号=订购单.客户号
--inner
select * from 客户 inner join 订购单
on 客户.客户号=订购单.客户号
--left
select 客户.*,订单号,订购日期
from 客户 left join 订购单
on 客户.客户号=订购单.客户号
--right
select 客户.*,订单号,订购日期
from 客户 right join 订购单
on 客户.客户号=订购单.客户号
--full
select 客户.*,订单号,订购日期
from 客户 full join 订购单
on 客户.客户号=订购单.客户号
实验5
一、实验目的:理解视图的概念,掌握视图的使用方法。
二、实验内容:定义视图,并在视图上完成查询、插入、更新和删除操作。
三.、程序源代码:
--1
--1)、基于单个表按投影操作定义视图
create view v_cus as
select 客户号,客户名称
from 客户
--使用
select * from v_cus
--2)、基于单个表按选择操作定义视图
create view v_order as
select *
from 订购单
where 客户号='C001'
--使用
select * from v_order
--3)、基于单个表按选择和投影操作定义视图
create view v_cuss as
select 客户名称,联系人,电话
from 客户
where 客户号='C003'
--使用
select * from v_cuss
--4)、基于多个表根据连接操作定义视图
create view v_join as
select 客户.*,订单号,订购日期
from 客户 join 订购单
on 客户.客户号=订购单.客户号
--使用
select * from v_join
--5)、基于多个表根据嵌套查询定义视图
create view v_item as
select * from 订单名细
where 产品号 in(
select 产品号
from 产品
where 产品名称='Macbook')
--使用
select * from v_item
--6)、查定义含有虚字段的视图
create view v_items(订单号,序号,产品号,数量,总金额) as
select i.*,i.数量*g.单价
from 订单名细 i,产品 g
where i.产品号=g.产品号
--使用
select * from v_items
--2
--在视图上查询
select * from v_items
--在视图上插入
insert into v_cus values('C012','Google')
--在视图上更新
update v_order set 订购日期='2011-11-29'
--在视图上删除
delete from v_cus
where 客户号='C012'
实验7
一、实验目的:理解存储过程的概念,掌握存储过程的使用方法,加深对客户/服务器机制的理解。
二、实验内容:存储过程的建立和执行。
三.、程序源代码:
--1、查询单价范围在X元到Y元内的产品信息
create proc sp_getgoods
@price1 money,@price2 money
as
select *
from 产品
where 单价>@price1 and 单价<@price2
if @@ERROR= 0
return 0
else
return -1
--执行
declare @status int
execute @status=sp_getgoods 1000,6000
print @status
--2、查询在某年某月某日之后签订的订购单的客户信息
create proc sp_or_cus
@dates datetime
as
select 客户名称,联系人,电话,订单号,订购日期
from 客户,订购单
where 客户.客户号=订购单.客户号
and 订购日期>@dates
if @@ERROR= 0
return 0
else
return -1
--执行
declare @status1 int
execute @status1=sp_or_cus '2011-10-27'
print @status1
--3、将某产品的订购日期统一修改为一个指定日期
create proc sp_upd_date
@gdname varchar(40),@odate datetime
as
update 订购单
set 订购日期=@odate
where 订单号 in(
select 订单号
from 订单名细
where 产品号 in(
select 产品号
from 产品
where 产品名称=@gdname))
if @@ERROR= 0
return 0
else
return -1
--执行
declare @status2 int
execute @status2=sp_upd_date 'iphone','2011-10-23'
print @status2
实验8
3﹕在Goods表建立删除触发器﹐实现Goods表和Orders表的级联删除。
create trigger goodsdelete
on goods
after delete
as
delete from orders
where goodsname in
(select name from deleted)
4﹕在Orders表建立一个更新触发器﹐监视Orders表的订单日期(OrderDate)列﹐使其不能手工修改.
create trigger orderdateupdate
on orders
after update
as
if update(orderdate)
begin
raiserror(' orderdate cannot be modified',10,1)
rollback transaction
end
数据库系统设计实验报告课程数据库系统原理与应用姓名沈凯丽专业测绘工程121班学号20xx18080109指导老师张茂震实验一数据库…
西安科技大学数据库原理及应用课程设计报告题目寄宿学校管理信息系统的设计与实现学院计算机科学与技术学院专业及班级软件工程1202及1…
实验一SQLServer基本使用与数据定义一实验目的1掌握企业管理器及查询的定义方法使用方法2熟悉数据库建模及ER图的画法3掌握S…
学生成绩管理系统数据库课题雇员信息管理系统姓名学号同组姓名专业班级指导教师设计时间20xx年6月4号20xx年6月10日一需求分析…
实验一建立数据库班级:姓名:学号:分数:一、实验目的1、理解SQLServer数据库的存储结构;2、掌握SQLServer数据库的…
实习报告一、实习时间和单位时间:20xx年x月x日地点:许昌西继迅达实习单位简介:西继迅达(许昌)电梯有限公司现拥有四个子公司和一…
实验一(1)无条件单表查询selectsnameNAME,'yearofbirth:'BIRTH,20xx-sageBIRTHDA…
课程代码1010000450数据库Database学分3总学时48实验学时16面向专业信息与计算科学数学与应用数学一实验教学目标数…
数据库实验第三次题目1实验内容1检索上海产的零件的工程名称2检索供应工程J1零件P1的供应商号SNO3检索供应工程J1零件为红色的…
个人实验总结报告经过一学期的组织行为学课程学习可谓收获颇丰数次上机测试使得我对自己的优势与劣势有了更加科学而深刻的了解现将实验报告…