数据库实验及作业部分参考答案

合集下载

数据库实验答案

数据库实验答案

数据库实验答案1)找出 MGM studios 的地址。

select 地址from 制片公司where 公司名称='MGM studios'2)找出 Sandra Bullock 的出生日期。

select 生日from 电影明星where 姓名='Sandra Bullock'3)找出在2003年拍摄过电影的所有影星,或者拍摄过电影中含有"天空"的电影的所有明星。

select 演员姓名from 演员参演电影where 拍摄年份=2003 or 电影名称 like'%天空%'4)找出净资产至少1000万美元的所有行政长官。

select 姓名from 制片人与行政总裁where 资产>=100000005)找出所有的男影星或者是住址中含有 Malibu 的影星。

select 姓名from 电影明星where 地址 like 'Malibu' or 性别 like '男'1)电影"加勒比海盗4"中的男影星都有谁。

select 姓名from 电影明星where 性别 ='男' and 姓名 in(select 演员姓名from 演员参演电影where 电影名称='加勒比海盗4')2)谁是米高梅制片公司的总裁。

select 姓名from 制片人与行政总裁where 身份证号 =( select 行政总裁from 制片公司where 公司名称 ='MGM studios')4)那些电影比《乱世佳人》更长。

select 电影名称from 电影where 长度 >(select 长度from 电影where 电影名称 = '乱世佳人')5)那些行政长官比 T om 更富有。

select 姓名from 制片人与行政总裁where 资产>(select 资产from 制片人与行政总裁where 姓名='T om')1、找出价格低于6000元的PC计算机型号、速度、以及硬盘容量。

数据库实验题目及答案

数据库实验题目及答案

数据库实验
一、题目
药品(编号,名称,价格,厂商)
处方(药品编号,数量,医生编号)
医生(编号,姓名,科室,职称)
根据上面基本表的信息完成下列查询。

1 列出医生“张力”开的所有药品名称。

2 列出比“北方制药”所有药品都贵的药品信息。

3 列出内科所有处方的医生姓名、药品名称、金额
二、答案
题目一
连接查询
select 名称
from Yao ,Chu,Dr
where Yao.编号=Chu.药品编号and Chu.医生编号=Dr.编号and 姓名='张力'
嵌套查询
select 名称
from Yao
where 编号in(select 药品编号
from Chu
where 医生编号in(select 编号
from Dr
where 姓名= '李丽'))
题目二
select *
from Yao
where 价格>all(select 价格from Yao where 厂商='北方制药')
题目三
select 姓名,名称,价格
from Yao ,Chu,Dr
where Yao.编号=Chu.药品编号and Chu.医生编号=Dr.编号and 科室='内科'。

数据库实验答案

数据库实验答案
--3.查询选修了1号课程且不及格的学生的学号、姓名。(要求分别用连接查询和嵌套查询完成)
select sc.sno,sname,grade from sc,student where sc.sno=student.sno and cno='1' and grade<60
select sno,sname from student where sno in(select sno from sc where cno='1' and grade<60)
8.查询课程名为“C_”开头的课程名和学分。
9.某些学生入学后还没有确定所在的系,查询还没有确定系的学生的姓名。
10.查询成绩大于60分的学生的学号、课程号和成绩,并将查询结果按课程编号升序排列,同一课程的成绩按分数降序排列。
11.查询学校所开设的总的课程数。
12.计算2号课的学生成绩总和以及平均成绩,对应的列名分别为“总成绩”和“平均成绩”。
要求分别用集合查询和多重条件查询完成实验12createdatabasestdb1gousestdb1gocreatetabledepartmentdeptnochar10primarykeydnamevarchar50gocreateuniqueindexdeptnamedepartmentdnamegocreatetablestudentsnochar9primarykeysnamevarchar20nullssexchar2sageintdeptnochar10foreignkeydeptnoreferencesdepartmentdeptnocreatetablecoursecnochar4primarykeycnamevarchar40uniquecpnochar4ccreditsmallintgocreatetablescsnochar9cnochar4gradeintprimarykeysnocnoforeignkeysnoreferencesstudentsnoforeignkeycnoreferencescoursecno3insertdepartmentvalues?d001??cs?insertdepartmentvalues?d002??is?insertdepartmentvalues?d003??ma?insertstudentvalues?200215121??李勇??男?20?d001?insertstudentvalues?200215122??刘晨??女?20?d001?insertstudentvalues?200215123??王敏??女?20?d002?insertstudentvalues?200215125??张立??男?20?d003?insertstudentvalues?200215126??陈天华??男?20?d003?insertstudentvalues?200215128??宋阳??女?20?d002?insertcoursevalues?1??数据库??5?4insertcoursevalues?2??数学?null2insertcoursevalues?3??信息系统??1?4insertcoursevalues?4??操作系统??6?3insertcoursevalues?5??数据结构??7?4insertcoursevalues?6??数据处理

[训练]数据库实验1答案.docx

[训练]数据库实验1答案.docx

数据库上机实验答案下面的示例创建名为Sales的数据库。

因为没有使用关键字PRIMARY,第一个文件(Sales_dat)成为主文件。

因为Sales.dat文件的SIZE参数没有指定MB或KB,因此默认为MB,以兆字节为单位进行分配。

Sales_log文件以兆字节为单位进行分配,因为SIZE参数中显式声明了MB后缀。

USE masterGOCREATE DATABASE SalesON(NAME = Sales_dat,FILENAME 二'c:\program fi les\microsoft sqIserver\mssqI\data\saIedat. mdf',SIZE = 10,MAXSI ZE 二50,FILEGROWTH = 5 )LOG ON(NAME 二'Sales_log',FILENAME 二* c:\program fi les\microsoft sqIserver\mssqI\data\saIe丨og. Idf',SIZE = 5MB,MAXSI ZE 二25MB,FILEGROWTH 二5MB )GOB.指定多个数据文件和事务日志文件创建数据库下面的示例使用三个100 MB的数据文件和两个100 MB的事务日志文件创建了名为Archive的数据库。

主文件是列表中的第一个文件,并使用PR I MARY关键字显式指定。

事务日志文件在LOG ON关键字后指定"注意FILENAME选项中所用的文件扩展名:主要数据文件使用.nidf,次要数据文件使用.ndf,事务日志文件使用.IdfoUSE masterGOCREATE DATABASE ArchiveONPR I MARY ( NAME 二Archl,FILENAME 二'c:\program fi les\microsoft sqIserver\mssqI\data\archdat1. mdf',SIZE 二WOMB,MAXSI ZE 二200,FILEGROWTH = 20),(NAME 二Arch2,FILENAME 二1 c:\program fi les\microsoft sqIserver\mssqI\data'archdat2. ndf',SIZE = 100MB,MAXSI ZE = 200,FILEGROWTH 二20),(NAME 二Arch3,FILENAME 二'c:\program fi les\microsoft sqI server\mssqI\data\archdat3.ndf',SIZE 二WOMB,MAXSIZE 二200,FILEGROWTH 二20)LOG ON(NAME 二Archlogl,FILENAME 二’c:\program fi les\microsoft sqI server\mssqI\data\archIog1. Idf', SIZE 二100MB,MAXSI ZE 二200,FILEGROWTH 二20),(NAME 二Archlog2,FILENAME 二'c:\program fi les\microsoft sqI server\mssqI\data\archIog2.Idf',SIZE 二100MB,MAXSI ZE 二200,FILEGROWTH 二20)GO c.创建简单的数据库本例创建名为Products的数据库,并指定单个文件。

数据库实验报告及答案

数据库实验报告及答案

实验任务书(实验一、实验二)课程名称:数据库原理与技术实验报告要求:1.列出所有的SQL语句和源代码;2.程序要求有适当的注释;3.对数据完整性约束实施要求给出相应的测试用例。

4.实验报告提交电子档。

实验内容:一:创建表、更新表和实施数据完整性1.运行给定的SQL Script,建立数据库GlobalToyz。

2.了解表的结构,建立所有表的关系图。

3.利用系统定义的存储过程sp_helpdb查看数据库的相关信息,例如所有者、大小、创建日期等。

4.查看所有表中出现的约束(包括Primary key, Foreign key, check constraint, default, unique)5.把价格在$20以上的所有玩具的信息拷贝到称为PremiumToys的新表中。

SELECT*INTO PremiumToysFROM ToysWHERE Toys.mToyRate>20;6.对表Toys实施下面数据完整性规则:(1)玩具的现有数量应在0到200之间;(2)玩具适宜的最低年龄缺省为1。

ALTER TABLE ToysADD CONSTRAINT C1CHECK (siToyQoh BETWEEN 0 AND 200);ALTER TABLE ToysADD CONSTRAINT C2default(1)for siLowerAge;7.给id为‘000001’玩具的价格增加$1。

update Toys set mToyRate=mToyRate+1 where cToyId='000001';8. 列出表PickofMonth中的所有记录,并显示中文列标题。

SELECT cToyId[玩具编号],siMonth[生产月份],iYear[生产年份],iTotalSold [销售总量]FROM PickofMonth;二:查询数据库1.显示属于California和Illinoi州的顾客的名、姓和emailID。

数据库技术实验报告的答案

数据库技术实验报告的答案

数据库技术实验报告的答案实验目的:本次实验旨在加深对数据库管理系统(DBMS)的理解,掌握数据库设计、实现和查询的基本技能,并通过实际操作来熟悉SQL语言的运用。

实验环境:1. 操作系统:Windows 102. 数据库管理系统:MySQL 8.03. 开发工具:Visual Studio Code实验内容:1. 数据库的创建与设计2. 数据表的创建与数据类型选择3. 数据的插入、更新与删除4. SQL查询语句的编写与执行5. 数据库的备份与恢复实验步骤与结果:1. 数据库的创建与设计:- 使用MySQL命令行工具创建名为“StudentsDB”的数据库。

- 设计了三个表:Students(学生信息),Courses(课程信息),Enrollments(选课信息)。

2. 数据表的创建与数据类型选择:- 为Students表创建字段:StudentID(INT,主键),Name (VARCHAR(50)),Age(INT),Gender(CHAR(1))。

- 为Courses表创建字段:CourseID(INT,主键),CourseName (VARCHAR(100)),Credits(INT)。

- 为Enrollments表创建字段:EnrollmentID(INT,主键),StudentID(INT),CourseID(INT),Grade(CHAR(2)),并设置StudentID和CourseID为外键。

3. 数据的插入、更新与删除:- 向Students表插入了10条学生记录。

- 向Courses表插入了5门课程记录。

- 通过Enrollments表记录了学生的选课情况,包括成绩。

- 使用UPDATE语句更新了学生的选课成绩。

- 使用DELETE语句删除了某些学生的选课记录。

4. SQL查询语句的编写与执行:- 编写了多条SELECT语句,查询了学生信息、课程信息以及学生的选课情况。

- 使用了JOIN操作查询了学生与其选修课程的关联信息。

数据库实验及答案

数据库实验及答案

数据库实验及答案实验二SQL语言的基本操作实验目的和要求:掌握利用SQL语句完成各种查询操作的能力。

重点掌握用SELECT 语句进行各种查询;掌握INSERT语句的用法。

实验内容:用SQL语句完成一下的要求:1.查询信息系(IS)的所有学生信息select * from student where sdept=’is’2.查询选修了“数学”课的所有学生名单Select s.sno,snameFrom student s,course c,scWhere s.sno=sc.sno and /doc/6b13656175.html,o=/doc /6b13656175.html,o and cname=’数学’3.查询至少选修了一门其直接先行课为5号课程的学生的姓名。

Select snameFrom student s, sc, course cWhere s.sno=sc.sno and /doc/6b13656175.html,o=/doc /6b13656175.html,o and pcno=’5’4.查询全体学生的姓名和出生年份。

select sname,year(now())-sage as '出生年份' from student5.查询所有姓王的学生。

select *from student where sname like '王%'6.查询选修了3号课程的学生姓名及成绩,并按成绩降序排序。

Select sname,gradeFrom student s, scWhere s.sno=sc.sno and /doc/6b13656175.html,o=’3’Order by grade desc7.查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

Select *From studentOrder by sdept asc,age desc8.计算2号课程的平均成绩。

数据库实训操作题参考答案

数据库实训操作题参考答案

销售管理数据库的操作题销售管理数据库的数据查询1.查询员工"王娜"所在的部门。

select DepartmentNamefrom Departmentwhere DepartmentID=(select DepartmentID from Employee where EmployeeName='姚安娜')2.查询年龄最小的员工姓名、性别和工资。

select EmployeeName 姓名,Sex 性别,BirthDate 出生年月,Salary 工资from Employeewhere BirthDate=(select MAX(BirthDate)from Employee)--年龄最小就是出生年月最大,利用嵌套查询,查询最大出生年月3.查询已经接收销售订单的员工姓名和工资信息。

(用两种方法做:嵌套查询、相关子查询)--嵌套查询方法:Select EmployeeName 姓名,Salary 工资from Employeewhere EmployeeID in(select EmployeeID from Sell_Order)--相关子查询方法:Select Employee.*from Employeewhere exists(select*from Sell_Order where Sell_Order.EmployeeID=Employee.EmployeeID)4.查询订购“牛奶”的客户信息。

(用两种方法做:嵌套查询、连接查询)--嵌套查询:select CompanyName 公司名称,ContactName 联系人,Address 地址from Customerwhere CustomerID IN(select CustomerID from Sell_Order whereProductID=(select ProductID from Product where ProductName='牛奶'))--连接查询:select CompanyName 公司名称,ContactName 联系人,Address 地址from Customer join Sell_Order on Customer.CustomerID=Sell_Order.CustomerIDjoin Product on Product.ProductID=Sell_order.ProductIDwhere Product.ProductName='牛奶'5.查询所有员工姓名、性别、出生年月和所在部门信息。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验三关系查询与连接首先,将sales表中tot_amt的值计算出来。

update sales set tot_amt=0goselect order_no,tot=sum(qty*unit_price)into aa1from sale_itemgroup by order_nogoselect * from aa1goupdate sales set tot_amt=totfrom sales,aa1where aa1.order_no=sales.order_nogo1、查找所有经理的姓名、职称、薪水。

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

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

insert into employee(emp_no,emp_name,sex,dept,title,date_hired,salary)values('E0025','彭江','M','业务','职员',1900/01/21,43200)//4、将每个员工的薪水上调3%。

update employeeset salary=salary*1.03select *from employee3、查找住在上海或北京的女员工,并显示其姓名、所属部门、职称、住址select emp_name,dept,title,addrfrom employeewhere (addr='上海市' or addr= '北京市')and sex='F'4、在表sales中挑出销售金额大于等于10000元订单select *from saleswhere tot_amt>=10000.005、选取订单金额最高的前10%的订单数据。

select top 10 percent *from salesorder by tot_amt desc6、查找出职称为经理或职称为职员的女员工的信息select *from employeewhere (title='经理' or title='职员') and sex='F'7、删除sales表中作废的订单(其发票号码为I000000004)。

delete from sale_itemwhere order_no in(select order_nofrom saleswhere invoice_no='I000000004')delete from saleswhere invoice_no='I000000004'8、计算出一共销售了几种产品。

select count(distinct prod_id) as '共销售产品数'from sale_item9、显示sale_item表中每种个别产品的订购金额总和,并且依据销售金额由大到小排列来显示出每一种产品的排行榜。

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

select month(order_date) as 月份,prod_id,sum(qty*unit_price) as sum1from sale_itemgroup by prod_id,month(order_date)order by 月份asc ,prod_id asc11、由sales表中查找出销售金额最高的订单。

select *from saleswhere tot_amt=(select max(tot_amt)from sales)12、由sales表中查找出订单金额大于“E0013业务员在1996/10/15这天所接任一张订单的金额”的所有订单,并显示承接这些订单的业务员和该条订单的金额。

select *from saleswhere tot_amt>any(select tot_amtfrom saleswhere sale_id='E0013'and order_date='1996/10/15')order by tot_amt13-1、找出公司女业务员所接的订单。

select sale_id,tot_amtfrom saleswhere sale_id in(select emp_nofrom employeewhere sex='F')13-2:select sale_id,emp_name,tot_amtfrom sales,employeewhere sale_id=emp_no and sale_id in(select emp_nofrom employeewhere sex='F')14-1、找出公司中姓名相同的员工,并且依据员工编号排序显示这些员工信息。

select emp_no,emp_namefrom employeewhere emp_name in(select emp_namefrom employeegroup by emp_namehaving count(*)>1)order by emp_no14-2select a.emp_no,a.emp_namefrom employee a, employee bwhere a.emp_no>b.emp_no and a.emp_name=b.emp_nameorder by a.emp_no15-1、找出目前业绩未超过200000元的员工。

select *from employeewhere(select sum(tot_amt)from saleswhere emp_no=sale_id)<20000015-2:select emp_no,emp_namefrom employeewhere emp_no in(select sale_idfrom salesgroup by sale_idhaving sum(tot_amt)<200000)15-3:select distinct sale_id,a.emp_namefrom employee a,sales bwhere sale_id=emp_no and sale_id in(select b.sale_idfrom salesgroup by sale_idhaving sum(tot_amt)<200000)order by sale_id15-4select sale_idfrom salesgroup by sale_idhaving sum(tot_amt)<20000016-1: 计算公司内各个部门的工资支出总和。

*/select dept,emp_name,title,salaryfrom employeeorder by deptcompute sum(salary) by dept16-2select dept,sum(salary)from employeegroup by dept17-1: 计算每一产品销售数量总和与平均销售单价*/select prod_id,sum(qty),avg(unit_price)from sale_itemgroup by prod_id17-2select prod_id,qty,unit_pricefrom sale_itemorder by prod_idcompute sum(qty),avg(unit_price)by prod_id18、查找出employee表中部门相同且住址相同的女员工的姓名、性别、职称、薪水、住址。

select a.emp_name,a.sex,a.title,a.salary,a.addrfrom employee as a , employee as bwhere (a.emp_no!=b.emp_no) and (a.emp_name>b.emp_name) and (a.addr=b.addr)and (a.dept=b.dept) and a.sex='F'19、检索product 表和sale_item表中相同产品的产品编号、产品名称、数量、单价。

A:不求数量总和select distinct a.prod_id,prod_name,qty,unit_pricefrom product a,sale_item bwhere a.prod_id=b.prod_idorder by a.prod_idB:求数量总和1\计算sale_item表中同种产品的数量总和select prod_id,sumqty=sum(qty)from sale_itemgroup by prod_id2\利用上题的结果,查找prod_id,prod_name,sumqty的信息select a.prod_id,prod_name,sumqtyfrom product a,(select prod_id,sumqty=sum(qty)from sale_itemgroup by prod_id) bwhere a.prod_id=b.prod_id3\利用上题的结果,再显示unit_price信息select distinct a.prod_id,prod_name,sumqty,unit_pricefrom product a,(select prod_id,sumqty=sum(qty)from sale_itemgroup by prod_id) b,sale_item cwhere a.prod_id=b.prod_id and b.prod_id=c.prod_idorder by a.prod_id20、检索product 表和sale_item表中单价高于2400元的相同产品的产品编号、产品名称、数量、单价。

select a.prod_id,a.qty,a.unit_price,b.prod_namefrom sale_item as a, product as bwhere (a.prod_id=b.pro_id) and a.unit_price>2400//20、分别使用左向外连接、右向外连接、完整外部连接检索product 表和sale_item表中单价高于2400元的相同产品的产品编号、产品名称、数量、单价。

相关文档
最新文档