数据库基础实验

数据库基础实验,第1张

(1)SELECT cno,cname FROM c WHERE teacher=程军;

(2)SELECT sno,sname FROM s WHERE age>21;

(3)SELECT sname FROM s WHERE(SELECT sno FROM sc WHERE (SELECT cno FROM c WHERE teacher=程军) );

(4)SELECT cno FROM sc WHERE not in (SELECT sno FROM s WHERE sname=流丽);

(5)SELECT sno FROM sc as X,sc as Y WHERE Xcno<>Ycno;

今天寝室要熄灯了。明天继续

姚津泓实验作业 网页链接可以看看

--实验一

create database test1

on

(name=test1,

filename='d:\3116004357姚津泓\test1mdf',

size=10,

maxsize=50,

filegrowth=5)

log on

(name=test1_log,

filename='d:\3116004357姚津泓\test1ldf',

size=5mb,

maxsize=25mb,

filegrowth=5mb)

create database test2

on

(name=test11,

filename='d:\3116004357姚津泓\test11mdf',

size=10,

maxsize=50,

filegrowth=5),

(name=test22,

filename='d:\3116004357姚津泓\test22ndf',

size=10,

maxsize=50,

filegrowth=5),

(name=test33,

filename='d:\3116004357姚津泓\test33ndf',

size=10,

maxsize=50,

filegrowth=5)

log on

(name=test11_log,

filename='d:\3116004357姚津泓\test11ldf',

size=5mb,

maxsize=25mb,

filegrowth=5mb),

(name=test22_log,

filename='d:\3116004357姚津泓\test22ldf',

size=5mb,

maxsize=25mb,

filegrowth=5mb)

create database test3

on

(name=dab1,

filename='d:\3116004357姚津泓\dab1mdf',

size=10,

maxsize=50,

filegrowth=5),

filegroup fg11 --第一个文件组

(name=dab2,

filename='d:\3116004357姚津泓\dab2ndf',

size=10,

maxsize=50,

filegrowth=5),

filegroup fg22 --第二个文件组

(name=dat1,

filename='d:\3116004357姚津泓\dat1ndf',

size=10,

maxsize=50,

filegrowth=5),

(name=dat2,

filename='d:\3116004357姚津泓\dat2ndf',

size=10,

maxsize=50,

filegrowth=5),

filegroup fg33--第三个文件组

(name=daz1,

filename='d:\3116004357姚津泓\daz1ndf',

size=10,

maxsize=50,

filegrowth=5),

(name=daz2,

filename='d:\3116004357姚津泓\daz2ndf',

size=10,

maxsize=50,

filegrowth=5)

alter database test1

add file

(name=te,

filename='d:\3116004357姚津泓\tendf',

size=10,

maxsize=50,

filegrowth=5

)

alter database test2

modify file

(name=test11,

filename='d:\3116004357姚津泓\test11mdf',

maxsize=55

)

drop database test1

--实验2--

create database 实验

create schema yaojinhong

create table yaojinhong仓库

(

仓库号char(6)primary key check (仓库号like'[A-Z][A-Z][0-9][0-9][0-9][0-9]'),

城市char(10) unique not null,

面积 int check(面积>=50)

)

create table yaojinhong职工

(

职工号char(8)primary key check (职工号like'[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9]'),

仓库号char(6)constraint ck foreign key references yaojinhong仓库(仓库号),

工资 money check (工资>=1000 and 工资<=10000)

)

create table yaojinhong供应商

(

供应商号 char(4)primary key check (供应商号like'[S][0-9][0-9][0-9]'),

供应商名 char(16),

地址char(30),

)

create table yaojinhong订购单

(职工号 char(8) not null foreign key references yaojinhong职工(职工号),

供应商号 char(4) null constraint supply foreign key references yaojinhong供应商(供应商号) on delete set null,

订购单号 char(6) primary key check(订购单号 like 'OR[0-9][0-9][0-9][0-9]'),

订购日期 datetime default getdate(),

金额 money default null

)

create table yaojinhong订购单明细

(订购单号 char(6) not null foreign key references yaojinhong订购单(订购单号),

序号 char(2),

产品名称 char(20),

单价 money default null check(单价>0),

数量 int not null check(数量>0),

primary key(订购单号,序号)

)

alter table yaojinhong订购单

add 完成日期 datetime default null

alter table yaojinhong订购单明细

add check(数量 between 0 and 1000)

alter table yaojinhong订购单明细

alter column 数量 int not null alter table yaojinhong供应商

alter column 供应商名 varchar(30)

实验3

insert into yaojinhong仓库 values('QW0001','广州',500)

insert into yaojinhong仓库 values('QW0002','上海',900)

insert into yaojinhong仓库 values('QW0003','北京',250)

insert into yaojinhong仓库 values('QW0004','纽约',1000)

insert into yaojinhong仓库 values('QW0005','深圳',650)

insert into yaojinhong职工 values('ZG000001','QW0005',3000)

insert into yaojinhong职工 values('ZG000002','QW0001',5600)

insert into yaojinhong职工 values('ZG000003','QW0004',6600)

insert into yaojinhong职工 values('ZG000004','QW0002',3800)

insert into yaojinhong职工 values('ZG000005','QW0005',3900)

insert into yaojinhong职工 values('ZG000006','QW0001',4600)

insert into yaojinhong职工 values('ZG000007','QW0003',4000)

insert into yaojinhong职工 values('ZG000008','QW0003',5000)

insert into yaojinhong职工 values('ZG000009','QW0002',4800)

insert into yaojinhong职工 values('ZG000010','QW0004',6000)

insert into yaojinhong供应商 values('S001','华广','深圳')

insert into yaojinhong供应商 values('S002','广工','广州')

insert into yaojinhong供应商 values('S003','美的','茂名')

insert into yaojinhong供应商 values('S004','乐视','深圳')

insert into yaojinhong供应商 values('S005','康佳','广州')

insert into yaojinhong订购单 values('ZG000007','S005','OR0001','2015-1-6',5000,'2015-2-5')

insert into yaojinhong订购单 values('ZG000002','S001','OR0002','2015-2-3',10000,'2015-3-4')

insert into yaojinhong订购单 values('ZG000003','S004','OR0003','2015-2-17',3000,'2015-5-16')

insert into yaojinhong订购单 values('ZG000006','S002','OR0004','2015-2-16',9000,'2015-4-18')

insert into yaojinhong订购单 values('ZG000010','S005','OR0005','2015-3-20',8000,'2015-6-27')

insert into yaojinhong订购单 values('ZG000001','S001','OR0006','2015-3-22',7500,'2015-7-29')

insert into yaojinhong订购单 values('ZG000006','S003','OR0021','2015-2-15',4600,'2015-10-13')

insert into yaojinhong订购单 values('ZG000004','S002','OR0007','2015-2-23',8500,'2015-8-29')

insert into yaojinhong订购单 values('ZG000003','S005','OR0008','2015-1-5',3600,'2015-11-11')

insert into yaojinhong订购单 values('ZG000001','S004','OR0009','2015-1-31',7600,'2015-12-14')

insert into yaojinhong订购单 values('ZG000007','S001','OR0010','2015-3-25',12000,'2015-7-30')

insert into yaojinhong订购单 values('ZG000008','S002','OR0011','2015-2-26',4600,'2015-9-27')

insert into yaojinhong订购单 values('ZG000010','S002','OR0012','2015-1-28',3400,'2015-11-21')

insert into yaojinhong订购单 values('ZG000009','S004','OR0013','2015-3-19',2300,'2015-10-24')

insert into yaojinhong订购单 values('ZG000007','S005','OR0014','2015-2-23',9000,'2015-9-21')

insert into yaojinhong订购单 values('ZG000008','S003','OR0015','2015-2-27',6400,'2015-12-23')

insert into yaojinhong订购单 values('ZG000002','S001','OR0016','2015-1-30',7900,'2015-6-30')

insert into yaojinhong订购单 values('ZG000005','S004','OR0017','2015-1-27',20000,'2015-8-18')

insert into yaojinhong订购单 values('ZG000009','S004','OR0018','2015-3-5',13000,'2015-7-31')

insert into yaojinhong订购单 values('ZG000004','S002','OR0019','2015-3-14',7000,'2015-6-17')

insert into yaojinhong订购单 values('ZG000006','S005','OR0020','2015-2-21',4300,'2015-5-25')

insert into yaojinhong订购单明细 values('OR0020','01','沐浴露',32,240)

insert into yaojinhong订购单明细 values('OR0001','02','洗衣粉',30,125)

insert into yaojinhong订购单明细 values('OR0012','03','洗衣液',39,59)

insert into yaojinhong订购单明细 values('OR0014','04','香皂',14,99)

insert into yaojinhong订购单明细 values('OR0004','05','盐',2,169)

insert into yaojinhong订购单明细 values('OR0005','06','酱油',14,65)

insert into yaojinhong订购单明细 values('OR0011','07','化妆品',26,24)

insert into yaojinhong订购单明细 values('OR0019','08','洗面奶',39,68)

insert into yaojinhong订购单明细 values('OR0008','09','面膜',5,147)

insert into yaojinhong订购单明细 values('OR0003','10','花生油',36,258)

insert into yaojinhong订购单明细 values('OR0017','11','鼠标',69,72)

insert into yaojinhong订购单明细 values('OR0015','12','鼠标垫',18,64)

insert into yaojinhong订购单明细 values('OR0016','13','移动电源',58,69)

insert into yaojinhong订购单明细 values('OR0012','14','插座',37,169)

insert into yaojinhong订购单明细 values('OR0011','15','牛奶',72,111)

insert into yaojinhong订购单明细 values('OR0020','16','洗洁精',9,114)

insert into yaojinhong订购单明细 values('OR0019','17','洁厕液',6,300)

insert into yaojinhong订购单明细 values('OR0010','18','鸡蛋',14,124)

insert into yaojinhong订购单明细 values('OR0009','19','牛肉',38,300)

insert into yaojinhong订购单明细 values('OR0016','20','内裤',59,654)

insert into yaojinhong订购单明细 values('OR0018','21','鞋子',299,300)

insert into yaojinhong订购单明细 values('OR0015','22','手表',890,34)

insert into yaojinhong订购单明细 values('OR0005','23','钱包',129,124)

insert into yaojinhong订购单明细 values('OR0008','24','行李箱',344,64)

insert into yaojinhong订购单明细 values('OR0014','25','手袋',3,874)

insert into yaojinhong订购单明细 values('OR0018','26','篮球',360,59)

insert into yaojinhong订购单明细 values('OR0003','27','足球',260,36)

insert into yaojinhong订购单明细 values('OR0005','28','羽毛球拍',78,69)

insert into yaojinhong订购单明细 values('OR0007','29','乒乓球拍',89,145)

insert into yaojinhong订购单明细 values('OR0020','30','羽毛球',5,645)

insert into yaojinhong订购单明细 values('OR0013','31','乒乓球',3,542)

insert into yaojinhong订购单明细 values('OR0016','32','牙膏',23,200)

insert into yaojinhong订购单明细 values('OR0009','33','牙刷',6,456)

insert into yaojinhong订购单明细 values('OR0019','34','防晒霜',56,65)

insert into yaojinhong订购单明细 values('OR0017','35','水杯',39,1187)

insert into yaojinhong订购单明细 values('OR0010','36','拖把',15,187)

insert into yaojinhong订购单明细 values('OR0004','37','扫把',6,200)

insert into yaojinhong订购单明细 values('OR0005','38','垃圾桶',16,254)

insert into yaojinhong订购单明细 values('OR0006','39','书',46,688)

insert into yaojinhong订购单明细 values('OR0016','40','卫生纸',13,500)

insert into yaojinhong订购单明细 values('OR0020','41','相机',3698,32)

insert into yaojinhong订购单明细 values('OR0013','42','手机',2000,200)

insert into yaojinhong订购单明细 values('OR0017','43','音响',500,143)

insert into yaojinhong订购单明细 values('OR0001','44','吸尘器',1688,40)

insert into yaojinhong订购单明细 values('OR0012','45','油烟机',3500,10)

insert into yaojinhong订购单明细 values('OR0010','46','台式电脑',4000,25)

insert into yaojinhong订购单明细 values('OR0013','47','空调',50,230)

insert into yaojinhong订购单明细 values('OR0005','48','电视机',1300,100)

insert into yaojinhong订购单明细 values('OR0016','49','洗衣机',3400,15)

insert into yaojinhong订购单明细 values('OR0011','50','笔记本',6000,20)

insert into yaojinhong仓库 values('QW0001','惠州',600)

原因:违反了PRIMARY KEY 约束'PK__仓库__530C599C7F60ED59'。不能在对象'yaojinhong仓库' 中插入重复键。语句已终止。

insert into yaojinhong职工 values('ZG000011','QW0009',3000)

原因:仓库号列参照仓库表的仓库号,此时在仓库表中找不到仓库号为QW0009的仓库。INSERT 语句与FOREIGN KEY 约束"FK__职工__仓库号__0AD2A005"冲突。该冲突发生于数据库"实验",表"yaojinhong仓库", column '仓库号'。

insert into yaojinhong仓库 values('QW0006','天津',30)

原因:在定义时,面积要大于等于50,这里面积为30,明显违反了用户定义完整性

delete yaojinhong仓库 where 城市='北京'

删除 *** 作失败。因为有职工参照了干记录

delete yaojinhong供应商 where 供应商号='S002'

因为删除参照完整性规则定义为set null

alter table yaojinhong职工

drop constraint ck

alter table yaojinhong职工

drop column 仓库号

alter table yaojinhong职工

add 仓库号 char(6)constraint ck foreign key references yaojinhong仓库

on delete cascade

on update cascade

update yaojinhong职工 set 仓库号='QW0006' where 职工号='ZG000002'

更新 *** 作失败,因为职工表所参考的仓库表中不存在仓库号为QW0006的仓库。

update yaojinhong仓库 set 仓库号=null where 仓库号='QW0001'

更新 *** 作失败,因为仓库号是关键字,不能为空。

update yaojinhong仓库 set 面积=30 where 仓库号='QW0001'

更新 *** 作失败,因为面积被定义约束大于或等于50,此处面积=30,显然违反了约束。

update yaojinhong职工 set 工资=(工资+(工资01))

update yaojinhong订购单 set 金额=(select SUM(单价数量) from yaojinhong订购单明细

where 订购单明细订购单号=订购单订购单号)

delete yaojinhong职工 where 仓库号 in (select 仓库号 from yaojinhong仓库 where 城市='北京')

delete yaojinhong订购单 where 供应商号 in (select 供应商号 from yaojinhong供应商 where 供应商名='广工')

实验4

1 select distinct 工资 from yaojinhong职工

select all 工资 from yaojinhong职工

2 select from yaojinhong仓库

3 select 职工号 from yaojinhong职工 where 工资>5000

4 select from yaojinhong仓库 where 仓库号 in(select 仓库号 from yaojinhong职工 where 工资>5000)

5 select 职工号 from yaojinhong职工 where 仓库号 in ('QW0005','QW0002') and 工资<5000

6 select from yaojinhong职工 where 工资 between 3000 and 5000

7 select from yaojinhong供应商 where 供应商名 like '%公司'

8 select from yaojinhong仓库 where 城市!='北京

9 select from yaojinhong订购单 where 供应商号 is null

10 select from yaojinhong订购单 where 供应商号 is not null

11 select from yaojinhong职工  order by 工资 ASC

12 select from yaojinhong职工  order by 仓库号 ASC,工资 DESC

13 select 职工号,城市 from yaojinhong职工  join yaojinhong仓库 on 职工仓库号=仓库仓库号 where 工资>6000

14 select 职工号,城市 from yaojinhong职工  join yaojinhong仓库 on 职工仓库号=仓库仓库号 where 面积>700

16 select from yaojinhong仓库  cross join yaojinhong职工

17 select from yaojinhong仓库  cross join yaojinhong职工

where 仓库仓库号=职工仓库号

18 select 供应商供应商号,供应商名,订购单号,订购日期 from yaojinhong供应商  join yaojinhong订购单

on 供应商供应商号=订购单供应商号

19 select 供应商供应商号,供应商名,订购单号,订购日期 from yaojinhong供应商 left join yaojinhong订购单

on 供应商供应商号=订购单供应商号

20 select 供应商供应商号,供应商名,订购单号,订购日期 from yaojinhong供应商 right join yaojinhong订购单 on 供应商供应商号=订购单供应商号

21 select 供应商供应商号,供应商名,订购单号,订购日期 from yaojinhong供应商 full join yaojinhong订购单 on 供应商供应商号=订购单供应商号

22 select 城市 from yaojinhong仓库 where 仓库号 in

(select 仓库号 from yaojinhong职工 where 工资=3300)

23 select from yaojinhong仓库 where 仓库号 not in

(select 仓库号 from yaojinhong职工 where 工资<5000)

24 select from yaojinhong职工 where 工资 in

(select  工资 from yaojinhong职工 where 职工号='ZG000001')

and 职工号!='ZG000001'

25 select 城市 from yaojinhong仓库 where 仓库号 in

(select 仓库号 from yaojinhong职工 where 职工号 in

(select 职工号 from yaojinhong订购单 where 供应商号 in

(select 供应商号 from yaojinhong供应商 where 地址='广州')))

26 select 供应商名 from yaojinhong供应商 join yaojinhong订购单 on 供应商供应商号=订购单供应商号 join yaojinhong职工 on 订购单职工号=职工职工号 join yaojinhong仓库 on 职工仓库号=仓库仓库号 where 地址='广州'and 城市='广州'

select 供应商名

from yaojinhong供应商 where 地址='广州' and 供应商号 in

(select 供应商号 from yaojinhong订购单 join yaojinhong职工 on 订购单职工号=职工职工号

join yaojinhong仓库 on 职工仓库号=仓库仓库号 where 城市='广州')

27 select 仓库号 from yaojinhong仓库 where 仓库号 in

(select 仓库号 from yaojinhong职工 where 工资>any

(select 工资 from yaojinhong职工 join yaojinhong仓库 on 职工仓库号=仓库仓库号

where 仓库仓库号='QW0001')and 仓库号!='QW0001')

28 select 仓库号 from yaojinhong仓库 where 仓库号 in

(select 仓库号 from yaojinhong职工 where 工资>all

(select 工资 from yaojinhong职工 join yaojinhong仓库 on 职工仓库号=仓库仓库号

where 仓库仓库号='QW0001'))

29 select from yaojinhong订购单 a where 金额=

(select MAX(金额) from yaojinhong订购单 b where a职工号=b职工号)

order by 职工号 ASC

30 select from yaojinhong仓库 where exists(select from yaojinhong职工 where 职工号 is null and 职工仓库号=仓库仓库号)

31 select from yaojinhong仓库 where not exists(select from yaojinhong职工 where 职工号 is null and 职工仓库号=仓库仓库号)

32 select COUNT(distinct 城市)城市数目 from yaojinhong仓库 where 城市 is not null

33 select SUM(工资)需要支付职工工资总数 from yaojinhong职工

34 select SUM(工资)北京和上海的仓库职工的工资总和 from yaojinhong职工 join yaojinhong仓库 on 职工仓库号=仓库仓库号

where 城市='北京' or  城市='上海'

35 select AVG(面积)平均面积 from yaojinhong仓库 where 仓库号 not in

(select 仓库号 from yaojinhong职工 where 工资<6000)

36 select max(金额)工资大于的职工所经手的订购单最高金额 from yaojinhong订购单 where 职工号 in

(select 职工号 from yaojinhong职工 where 工资>6000)

37 select 仓库号,AVG(工资)平均工资 from yaojinhong职工

group by 仓库号

38 select 仓库号,max(金额)最高金额,MIN(金额)最低金额,AVG(金额)平均金额 from yaojinhong职工 join yaojinhong订购单 on 职工职工号=订购单职工号 group by 仓库号

39 select 订购单订购单号,AVG(金额)平均金额 from yaojinhong订购单 join yaojinhong订购单明细  on 订购单订购单号=订购单明细订购单号

group by 订购单订购单号 having COUNT(订购单订购单号)>=5

40 select 仓库号,职工号,工资 from yaojinhong职工

order by 仓库号

compute avg(工资),sum(工资)by 仓库号

compute avg(工资),sum(工资)

41select 订购单明细订购单号,序号,产品名称,单价,数量,金额

from yaojinhong订购单明细 join yaojinhong订购单 on

订购单订购单号=订购单明细订购单号 order by 订购单订购单号

compute avg(金额),sum(金额)by 订购单订购单号

compute avg(金额),sum(金额)

42 select from yaojinhong订购单

compute avg(金额),sum(金额)

实验5

(1) 基于单个表按投影 *** 作定义视图。

create view yaojinhong仓库视图 as select from yaojinhong仓库

(2) 基于单个表按选择 *** 作定义视图。

create view yaojinhong仓库视图1 as select from yaojinhong仓库 where 城市='广州'

(3) 基于单个表按选择和投影 *** 作定义视图。

create view yaojinhong仓库视图2 as select 仓库号,面积 from yaojinhong仓库 where 城市='广州'

(4) 基于多个表根据连接 *** 作定义视图。

create view yaojinhong视图3 as select 职工号,城市,工资 from yaojinhong职工 join yaojinhong仓库 on 职工仓库号=仓库仓库号

(5) 基于多个表根据嵌套查询定义视图。

create view yaojinhong视图4 as select from yaojinhong仓库 where 仓库号 in

(select 仓库号 from yaojinhong职工 where 工资>4000)

(6) 定义含有虚字段的视图。

create view yaojinhong虚字段视图(仓库号,城市,面积) as select 仓库号,城市,面积2 from yaojinhong仓库

2、分别在定义的视图上设计一些查询(包括基于视图和基本表的连接或嵌套查询)。

select from yaojinhong仓库视图

select from yaojinhong仓库视图1 where 城市='广州'

select 仓库号,面积 from yaojinhong仓库视图2

select 职工号,城市,工资 from yaojinhong职工 join yaojinhong仓库视图 on 职工仓库号=仓库视图 仓库号

select from yaojinhong仓库视图 where 仓库号 in

(select 仓库号 from yaojinhong职工 where 工资>4000)

3、在不同的视图上分别设计一些插入、更新和删除 *** 作,分情况讨论哪些 *** 作可以成功完成,哪些 *** 作不能完成,并分析原因。

insert into yaojinhong仓库视图(仓库号,城市) values('QW0008','长沙')

update yaojinhong仓库视图 set 面积=650 where 仓库号='QW0008'

insert into yaojinhong仓库视图 values('QW0008','长沙',500)

update yaojinhong仓库视图 set 面积=700 where 仓库号='QW0001'

delete yaojinhong仓库视图 where 仓库号='QW0008'

delete yaojinhong仓库视图 where 仓库号='QW0008'

以上就是关于数据库基础实验全部的内容,包括:数据库基础实验、数据库程序设计1-5章实验、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9791107.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存