数据库实验示例带答案

2020-04-18 07:19

示例1

实验一

实验名称:SQL语句的应用(6课时) 一、实验目的

通过使用SQL SERVER企业管理器建立表。模式为人事表、客户表、销售表、销售明细表、产品表。理解数据库模式的概念,理解主键约束、外键约束、UNIQUE约束和CHECK约束。通过SQL SERVER企业管理器建立表间的约束。将得到的表生成脚本,保存。 二、实验环境

是MS SQL SERVER 2000的中文客户端。 三、实验示例 create table employee1(

emp_no char(5) not null,

constraint emp_nochk check(emp_no like '[E-F][0-9][0-9][0-9][0-9]'), emp_name char(10) not null, emp_sex char(1) not null,

constraint emp_sexchk check(emp_sex in ('m','f')), emp_phone char (13) not null,

constraint emp_phonechk check(emp_phone like '([0-9][0-9][0-9])[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'), emp_add varchar(60) not null, emp_salary char(5) not null,

constraint emp_salarychk check (emp_salary between 60000 and 80000) )

go

四、实验内容与步骤

1、建立五张表,每张表至少需要20条记录。

(1)/*员工人事表employee */ emp_no emp_name sex dept title date_hired birthday salary addr

(2)/*客户表customer */ cust_id cust_name addr

char(5) char(10) char(1) char(4) char(6) datetime datetime int char(50) Not null Not null Not null Not null Not null Not null Null Not null null primary key 员工编号 员工姓名 性别 所属部门 职称 到职日 生日 薪水 住址 char(5) char(20) char(40) Not null Not null, Not null, primary key 客户号 客户名称 客户住址

tel_no zip

char(10) char(6) Not null, null 客户电话 邮政编码 (3)/*销售主表sales */ order_no cust_id sale_id tot_amt order_date ship_date invoice_no

(4)/*销货明细表sale_item */ order_no prod_id qty unit_price order_date

(5)/*产品名称表product */ pro_id prod_name char(5) char(20) Not null Not null primary key 产品编号 产品名称 int char(5) int numeric(7,2) datetime Not null, Not null, Not null Not null null primary key 订单编号 产品编号 销售数量 单价 订单日期 int char(5) char(5) numeric(9,2) datetime datetime char(10) Not null Not null, Not null, Not null, Not null, Not null, Not null primary key 订单编号 客户号 业务员编号 订单金额 订货日期 出货日期 发票号码 2、建立表的同时创建表的约束。 (1) 为每张表建立主键约束。 (2) 通过拖放操作加入外键。

(3) 在表employee加入CHECK约束:输入的员工编号必须以E开头的5位数编号,性别只能为

M/F。

(4) 为销售主表sales中的发票编号字段建立UNIQUE约束。

3、通过快捷菜单得到脚本。 五、实验报告

实验二

实验名称:使用SELECT、UPDATE/INSERT/DELETE语句。(2课时) 一、实验目的

理解SELECT语句的操作和基本使用方法,熟练使用UPDATE/INSERT/DELETE语句进行表操作。

二、实验环境

三、实验示例

1、查找出职称为经理和职称为职员的女员工的姓名、职称、性别。 select emp_name,title,sex from employee

where title='经理'or title='职员'and sex='F'

2、选取销售数量最多的前5条订单订单号、数量。

select top 5 with ties order_no,sup_id,qty from sale_item order by qty desc

3、计算出sale_item表中每一笔销售数据的销售金额,并按照销售金额的大小排序。 select prod_id,qty,unit_price,(qty*unit_price)tot_amt from sale_item order by tot_amt desc 四、实验内容与步骤

1、 查找所有经理的姓名、职称、薪水。

select emp_name,title,salary from employee where title='经理'; 2、 查找出姓“王”并且姓名的最后一个字为“功”的员工。

select * from employee where emp_name like '王_功' 3、 添加一条记录至employee表(用INSERT…..INTO)。

insert into employee values('E1014','a14','m','办公','经理','2012-01-20','1999-12-23',60000,'北京'); 4、 将每个员工的薪水上调3%。

update employee set salary =salary*(1+0.03);

5、 查找住在上海或北京的女员工,并显示其姓名、所属部门、职称、住址。

select emp_name,dept,title,addr from employee where sex='f' and

(addr='上海' or addr='北京');

6、 在表sales中挑出销售金额大于等于10000元订单。 select * from sales where tot_amt>=10000;

7、 选取订单金额最高的前10%的订单数据。

select top 10 percent * from sales order by tot_amt desc;

8、 查找出职称为经理或职称为职员的女员工的信息。

select * from employee where title='经理' or title='职员' and sex='f'; 9、 删除sales表中作废的订单(其发票号码为I000000004),其订货明细数据也一并删除。 delete from sales where invoice_no='I000000004'; sales 表和sale_item 要建立关联,删除原则设为“层叠” 10、计算出一共销售了几种产品。

select count(distinct prod_id ) from sale_item ;

11、显示sale_item表中每种个别产品的订购销售金额总和,并且依据销售金额由大到小排列来显示出每一种产品的排行榜。 select prod_id,sum(qty*unit_price) as 销售金额from sale_item group by

prod_id order by sum(qty*unit_price) desc ;

12、计算每一产品每月的销售金额总和,并将结果按销售(月份,产品编号)排序。 select month(order_date) as 月份,prod_id,sum(qty*unit_price) as 销

售金额from sale_item group by prod_id ,month(order_date);

五、实验报告

实验三

实验名称:表连接JOIN(2课时)

一、实验目的

理解JOIN语句的操作和基本使用方法,掌握内连接、外连接、自身连接的概念和使用。 二、实验环境

三、实验示例

1、 检索product 表和sale_item表中数量大于2的相同产品的产品编号、产品名称、数量、

单价。

select a.prod_id,a.qty,a.unit_price,b.prod_name

from sale_item as a inner join product as b /*如果改成left join/right join 试分析结果*/ on (a.prod_id=b.pro_id) and a.qty>2 order by a.prod_id

2、查找出employee表中住址相同的员工的姓名、性别、职称、薪水、住址。 select a.emp_name,a.sex,a.title,a.salary,a.addr,b.emp_name,b.sex,b.title,b.salary,b.addr from employee as a inner join employee as b

on (a.emp_no!=b.emp_no) and (a.emp_name>b.emp_name) and (a.addr=b.addr) 四、实验内容与步骤

1、 查找出employee表中部门相同且住址相同的女员工的姓名、性别、职称、薪水、住址。 select

a.emp_name,a.sex,a.title,a.dept,a.salary,a.addr,b.emp_name,b.sex,b.title,b.dept,b.salary,b.addr

from employee as a join employee as b

on (a.emp_no!=b.emp_no) and (a.emp_name>b.emp_name) and (a.dept=b.dept) and (a.addr=b.addr) and a.sex = 'm' and b.sex = 'm';

2、 检索product 表和sale_item表中相同产品的产品编号、产品名称、数量、单价。 select a.prod_id,a.prod_name,b.qty,b.unit_price from product as a join

sale_item as b on a.prod_id=b.prod_id order by prod_id ;

3、 检索product 表和sale_item表中单价高于2400元的相同产品的产品编号、产品名称、

数量、单价。

select a.prod_id,a.prod_name,b.qty,b.unit_price from product as a join sale_item as b on a.prod_id=b.prod_id and b.unit_price>2400 ;

4、 分别使用左向外连接、右向外连接、完整外部连接检索product 表和sale_item表中单价

高于2400元的相同产品的产品编号、产品名称、数量、单价。并分析比较检索的结果。

select a.prod_id,a.prod_name,b.qty,b.unit_price from product as a left join sale_item as b on a.prod_id=b.prod_id and b.unit_price>2400 ;

inner join left join right join full join 五、实验报告

实验四

实验名称:SELECT的嵌套使用,实现复杂查询(2课时)

一、实验目的

掌握SELECT语句的嵌套使用,实现表的复杂查询,进一步理解SELECT语句的高级使用方法。

二、实验环境

三、实验示例

1、由employee表中查找出薪水最高的员工信息。 select *

from employee

where salary=

(select max(salary )

from employee ) 2、由sales表中查找出订单金额大于“E0107业务员在2012-1-2这天所接每一张订单的金额”的所有订单。

select * from sales where tot_amt >all (select tot_amt from sales

where sale_id='E0107' and order_date='2012-01-02') order by tot_amt

3、利用begin...end语句实现数据查询,查询出employee表中所有女职工的平均工资和 住址在"上海市"的所有女职工的平均工资

begin

select avg(salary) from employee where sex like 'f' union

select avg(salary) from employee

where sex like 'f' and addr like '上海' end

四、实验内容与步骤

1、 由sales表中查找出销售金额最高的订单。

select * from sales where tot_amt=

(select max(tot_amt ) from sales )

2、 由sales表中查找出订单金额大于“E0107业务员在2012-1-2这天所接任一张订单的金

额”的所有订单,并显示承接这些订单的业务员和该条订单的金额。 select sale_id,tot_amt from sales where tot_amt >all

(select tot_amt from sales


数据库实验示例带答案.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:西南交通大学2016大物作业05

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: