文档库 最新最全的文档下载
当前位置:文档库 › 数据库实验答案

数据库实验答案

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 资产>=10000000

5)找出所有的男影星或者是住址中含有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)那些行政长官比Tom 更富有。

select 姓名

from 制片人与行政总裁

where 资产>

(

select 资产

from 制片人与行政总裁

where 姓名='Tom'

)

1、找出价格低于6000元的PC计算机型号、速度、以及硬盘容量。

select 型号,速度,硬盘容量

from PC电脑

where 价钱< 6000

2、同上要求,另外将"速度" 改名为"兆赫",将"硬盘" 改名为"G字节"。

select 型号,速度兆赫,硬盘容量G字节

from PC电脑

where 价钱< 6000

3、找出打印机的制造商。

select 制造商

from 产品

where 产品类型= '打印机'

4、找出价格高于2000元的"便携式电脑"的型号、内存容量及屏幕大小。

select 型号,内存,屏幕大小

from 便携式电脑

where 价钱> 6000

5、从"打印机"关系中找出所有彩色打印机的元组。

select *

from 打印机

where 是否彩色= '是'

6、找出具有300GB以上的硬盘容量而价格低于2000元的所有个人计算机的型号、速度及硬盘。

select 型号,速度,硬盘容量

from PC电脑

where 硬盘容量>300 and 价钱<6000

1、找出配置了容量至少为300GB硬盘的便携式电脑的制造商及其速度。

select 制造商,速度

from 产品,便携式电脑

where 产品型号=便携式电脑.型号and 硬盘>300

2、找出由生产厂商"苹果"生产的所有产品的型号和价格。

select 型号,价钱

from PC电脑

where 型号in

(

select 产品型号

from 产品

where 制造商='苹果'

)

union

select 型号,价钱

from 便携式电脑

where 型号in

(

select 产品型号

from 产品

where 制造商='苹果'

)

union

select 型号,价钱

from 打印机

where 型号in

(

select 产品型号

from 产品

where 制造商='苹果'

)

3、找出所有出售"便携式电脑"(而不出售PC)的生产厂商。select 制造商

from 产品

where 产品类型LIKE '便携式电脑' and 制造商not in

(

select 制造商

from 产品

where 产品类型LIKE 'PC电脑' or 产品类型LIKE '打印机' )

4、找出在两种或两种以上PC上出现的硬盘容量。

select 硬盘容量

from PC电脑

group by 硬盘容量

having count(*)>=2

5、找出拥有相同速度和内存的PC的成对的型号。

select distinct Q.型号,P.型号

from PC电脑Q, PC电脑P

where Q.速度=P.速度and Q.内存=P.内存and Q.型号

1、找出速度至少为1500MHz的"PC电脑"制造商。("=any"和"in") select distinct 制造商

from 产品

where 产品类型='PC电脑' and 产品型号in

(

select 型号

from PC电脑

where 速度>=1500

)

2、找出价格最高的打印机。(注意">all" 和">any" 的区别) (">=all"和"=max()") select 型号

from 打印机

where 价钱>=all

(

select 价钱

from 打印机

)

3、找出速度低于任何PC的便携式电脑。(注意"

select 型号

from 便携式电脑

where 速度< all

(

select 速度

from PC电脑

)

4、找出具有最高价格的机器(PC,便携式电脑或打印机)的型号。

select 型号

from PC电脑

where 价钱=

(

select MAX(价钱)

from PC电脑

)

union

select 型号

from 便携式电脑

where 价钱=

(

select MAX(价钱)

from 便携式电脑

)

union

select 型号

from 打印机

where 价钱=

(

select MAX(价钱)

from 打印机

)

5、找出具有最低价格的彩色"打印机"的制造商。("=any和in" "<=all和MIN()") select 制造商

from 产品

where 产品型号=any

(

select 型号

from 打印机

where 价钱<=all

(

select 价钱

from 打印机

where 是否彩色='是'

)

)

6、找出在具有最小内存容量的所有PC 中,具有最快处理器的PC 制造商。select 制造商

from 产品

where 产品型号=any

(

select 型号

from PC电脑

where 速度=

(

select MAX(速度)

from PC电脑

where 内存<=all

(

select 内存

from PC电脑

)

)

and 内存in

(

select min(内存)

from pc电脑

)

)

select 制造商

from 产品

where 产品型号in

(

select 型号

from PC电脑

where 速度=

(

select MAX(速度)

from PC电脑

where 内存=

(

select MIN(内存)

from PC电脑

)

)

and 内存in

(

select MIN(内存)

from pc电脑

)

)

select 制造商

from 产品,PC电脑

where 产品.产品型号=PC电脑.型号and

PC电脑.速度=

(

select MAX(速度)

from PC电脑

where 内存<=all

(

select 内存

from PC电脑

)

)

and 内存in

(

select min(内存)

from pc电脑

)

create view vproduct(制造商,产品型号,产品类型,价钱,速度) as

select 制造商,产品型号,产品类型,价钱,速度

from 产品,PC电脑

where 产品.产品型号=PC电脑.型号

union

select 制造商,产品型号,产品类型,价钱,速度

from 产品,便携式电脑

where 产品.产品型号=便携式电脑.型号

1、找出销售便携式电脑但不销售PC 的厂商。

select 制造商

from vproduct

where 产品类型LIKE '便携式电脑' and 制造商not in

( select 制造商

from 产品

where 产品类型LIKE 'PC电脑' or 产品类型LIKE '打印机'

)

2、找出至少生产两种不同的计算机(PC或便携式电脑)且极其速度至少为133的厂商。select distinct 制造商

from vproduct

where 速度>133 and 产品类型LIKE 'PC电脑' or 产品类型LIKE '便携式电脑' and 制造商in

(select min(制造商)

from vproduct

group by 制造商

having COUNT(产品型号)>=2)

3、找出生产最高速度的计算机(PC电脑或便携式电脑)厂商。

select distinct 制造商

from vproduct

where 速度>133 and 产品类型LIKE 'PC电脑' or 产品类型LIKE '便携式电脑' and 制造商in

(select min(制造商)

from vproduct

group by 制造商

having COUNT(产品型号)>=2)

create view 最高速度1

as

select distinct 制造商

from 产品,PC电脑

where 产品.产品型号=PC电脑.型号

and 速度=

(

select MAX(速度)

from PC电脑

)

union

select distinct 制造商

from 产品,便携式电脑

where 产品.产品型号=便携式电脑.型号

and 速度=

(

select MAX(速度)

from 便携式电脑

)

4、找出至少生产三种不同速度PC的厂商。

select distinct 制造商

from vproduct

where 产品类型LIKE 'PC电脑' and 制造商in

(select min(制造商)

from vproduct

group by 产品类型

having count(速度)>=3)

5.找出只卖三种不同型号的PC的厂商。

create view 三种

as

select 制造商

from 产品A

where A.产品型号=any

(

select 型号

from PC电脑

)

AND A.制造商not in

(

select B.制造商

from 产品B

where 产品类型='便携式电脑' or 产品类型='打印机'

)

GROUP BY 制造商

having count(*)=3

create view vproduct3(制造商,产品型号,产品类型)

as

select 制造商,产品型号,产品类型

from 产品

where 制造商not in

(select 制造商

from 产品

where 产品类型LIKE '便携式电脑' or 产品类型LIKE '打印机' )

select 制造商

from vproduct3

group by 制造商

having count(产品型号)>=2

select avg(速度)

from PC电脑

select avg(速度)

from 便携式电脑

where 价钱>2500

select avg(价钱)

from PC电脑

where 型号in

(select 产品型号

from 产品

where 制造商='A'

)

实验六

1)找出PC的平均速度。

select A VG(速度)

from vproduct3

where 产品类型='PC电脑'

2)找出价格超过2500美元的便携式电脑的平均速度。select A VG(速度)

from vproduct3

where 产品类型='便携式电脑' and 价钱>2500

3)找出厂商A生产PC的平均价格。

select A VG(价钱)

from vproduct3

where 制造商='A' and 产品类型='PC电脑'

4)找出厂商D生产的PC和便携式电脑的平均价格。select A VG(价钱)

from vproduct3

where 制造商='D'

(5)找出各种不同速率的pc的平均价格

create view five

as

select 价钱,速度

from PC电脑

select 速度,A VG(价钱) as 平均价钱

from five

group by 速度

直接查询

select 速度,A VG(价钱)

from PC电脑

group by 速度

6)找出各厂商生产的便携式电脑的显示器平均尺寸。create view vproduct4(制造商,产品型号,速度,价钱,屏幕大小) as

select 制造商,产品型号,速度,价钱,屏幕大小

from 产品,便携式电脑

where 产品.产品型号=便携式电脑.型号

select 制造商,A VG(屏幕大小)

from vproduct4

group by 制造商

7)找出生产三种不同型号的PC的厂商。

create view vproduct5(制造商,产品型号,产品类型,价钱)

as

select 制造商,产品型号,产品类型,价钱

from 产品,PC电脑

where 产品.产品型号=PC电脑.型号

select 制造商

from vproduct5

group by 制造商

having count(产品型号)=3

8)找出各厂商生产的PC的最高价格。

select 制造商,MAX(价钱)

from vproduct5

group by 制造商

9)找出速度超过150MHz的各种速度的PC的平均价格。

select A VG(速度)

from vproduct3

where 产品类型='PC电脑' and 速度>150

10)找出所有生产打印机的厂商生产PC的硬盘平均容量。

create view now

as

select 制造商,产品型号

from 产品

where 制造商in

(select 制造商

from 产品

where 产品类型='打印机')

and 产品类型='PC电脑'

create view now1

as

select 制造商,产品型号,硬盘容量

from now,PC电脑

where now.产品型号=PC电脑.型号

select avg(硬盘容量)平均硬盘容量

from now1

实验七

1)

create procedure P1

@制造商char(10),@产品型号char(10),@产品类型char(10),@速度int,@内存int,@硬盘容

量int,@价钱money

as

begin transaction

declare @error1 int

insert into 产品values(@制造商,@产品型号,@产品类型)

set @error1=@error1+@@error

insert into PC电脑values(@产品型号,@速度,@内存,@硬盘容量,@价钱) set @error1=@error1+@@error

if(@error1<>0)

begin

print '插入失败,回滚事物'

rollback transaction

end

else

begin

print '成插入成功,提交事物'

commit transaction

end

exec P1 @制造商='C',@产品型号='11100',@产品类型='PC电脑',@速度=240,@内存=32,@硬盘容量='2.5',@价钱=2499

2)

create procedure P2

as

begin transaction

declare @Error2 int

insert into 产品(制造商,产品型号,产品类型)

select 制造商,产品型号+'1100','便携式电脑'

from 产品

where 产品.产品类型='PC电脑'

set @Error2=@Error2+@@error

insert into 便携式电脑(型号,速度,内存,硬盘,屏幕大小,价钱)

select 型号+'1100',速度,内存,硬盘容量,11,价钱+500

from PC电脑

set @Error2=@Error2+@@error

if(@Error2<>0)

begin

print'插入失败,回滚事物'

rollback transaction

end

else

begin

print'插入成功,提交事物'

commit transaction

end

3)

create trigger P3

on PC电脑

for delete

as

delete from 产品

where 产品型号in (select 型号from deleted)

delete from PC电脑

where 硬盘容量<50

create trigger P4

on 产品

for update

as

declare @型号1 char(10)

declare @制造商char(10)

select @型号1=产品型号from inserted

select @制造商=制造商

from inserted b

where b.产品型号=@型号1

if @制造商is null

begin

print '错误!'

rollback transaction

end

update 产品

set 制造商='A'

where 制造商='B'

(4)厂商A收购了B,将所有由B制造的产品改为A制造declare MyCursor cursor

DYNAMIC

for

select 制造商,产品型号,产品类型

from 产品

where 制造商= 'B'

for update

open MyCursor

fetch next from MyCursor

update 产品

set 制造商= 'A'

where current of MyCursor

while(@@FETCH_STATUS =0)

begin

fetch next from MyCursor

update 产品

set 制造商= 'A'

where current of MyCursor

END

deallocate MyCursor

create database Movies

create table 电影明星(

姓名varchar(20) not null primary key,

地址varchar(50),

性别char(2)check(性别='男' or 性别='女'),

生日datetime

);

create table 制作人与行政总裁(

姓名varchar(20),

地址varchar(50),

身份证号char(18) not null primary key,

资产int

);

create table 制片公司(

公司名称varchar(50) not null primary key,

地址varchar(50),

行政总裁char(18)not null references 制作人与行政总裁(身份证号),

);

create table 电影(

电影名称varchar(50) not null,

拍摄年份datetime not null,

长度int,

是否彩色char(2)check(是否彩色= '是' or 是否彩色= '否'),

所属电影公司varchar(50) references 制片公司(公司名称),

制片人char(18) references 制作人与行政总裁(身份证号),

constraint dy primary key(电影名称,拍摄年份)

);

create table 演员参演电影(

电影名称varchar(50) not null ,

拍摄年份datetime not null ,

演员姓名varchar(20) not null references 电影明星(姓名),

foreign key(电影名称,拍摄年份) references 电影(电影名称,拍摄年份), constraint yycydy primary key(电影名称,拍摄年份,演员姓名)

);

create database Products

create table 产品(

制造商varchar(50),

产品型号char(4) not null primary key,

产品类型varchar(20),

)

create table PC电脑(

型号char(4) not null primary key references 产品(产品型号),

速度float,

内存float,

硬盘容量float,

价钱int,

);

create table 便携式电脑(

型号char(4) not null primary key references 产品(产品型号),

速度int,

内存int,

硬盘int,

屏幕大小int,

价钱int,

)

create table 打印机(

型号char(4) not null primary key references 产品(产品型号),

是否彩色char(2)check(是否彩色= '是' or 是否彩色= '否'),

类型varchar(20) ,

价钱int,

)

相关文档