一、 实验环境
1. Windows2000或以上版本;
2. SQLServer 2005或以上版本。
二、 实验目的
1. 掌握数据库表与视图的基础知识;
2. 掌握创建、修改、使用、删除表与视图的不同方法。
三、 实验内容
完成实验指导书中p60-61页 “实验内容与要求”。
四、 实验步骤、源代码及实验结果
- 创建数据库及表
- 用你掌握的某种方法,创建订报管理子系统的数据库DingBao,在DingBao数据库中用交互式界面操作方法或CREATE TABLE创建如下三表的表结构(表名及字段名使用括号中华出的英文名),并完成三表所示内容的输人,根据需要可自行设计输人更多的表记录。
- 创建表结构时要求满足:①报纸编码表(PAPER)以报纸编号(pno)为主键,如表3-1所示;②顾客编码表(CUSTOMER)以顾客编号(cno)为主键,如表3-2 所示;③报纸订阅表(CP以报纸编号(pno)与顾客编号(cno)为主键,订阅份数(num)的缺省值为1,如表3-3所示。
①、报纸编码表
CREATE TABLE PAPER
(
pno char(9) PRIMARY KEY,
pna char(9),
ppr float
);
②、顾客编码表
CREATE TABLE CUSTOMER
(
cno char(9) PRIMARY KEY,
cna char(9) UNIQUE NOT NULL,
adr char(20)
);
③、报纸订阅表
CREATE TABLE CP
(cno CHAR(9),
pno CHAR(9),
num int DEFAULT 1,
PRIMARY KEY(cno,pno),
FOREIGN KEY(cno) REFERENCES CUSTOMER(cno),
FOREIGN KEY(pno) REFERENCES PAPER(pno)
);
- 插入数据
①、
insert into PAPER values('000001','人物日报','12.5');
insert into PAPER values('000002','解放日报','14.5');
insert into PAPER values('000003','光明日报','10.5');
insert into PAPER values('000004','青年报','11.5');
insert into PAPER values('000005','扬子网报','18.5');
②、
insert into CUSTOMER values('10000001','李涛','无锡市解放东路123号');
insert into CUSTOMER values('10000002','钱金浩','无锡市人民西路234号');
insert into CUSTOMER values('10000003','邓杰','无锡市惠河路270号');
insert into CUSTOMER values('10000004','朱海红','无锡市中山东路432号');
insert into CUSTOMER values('10000005','欧阳阳文','无锡市中山东路532号');
③、
insert into CP values('10000001','000001','2');
insert into CP values('10000001','000002','4');
insert into CP values('10000001','000005','6');
insert into CP values('10000002','000001','2');
insert into CP values('10000002','000003','2');
insert into CP values('10000002','000005','2');
insert into CP values('10000003','000003','2');
insert into CP values('10000003','000004','4');
insert into CP values('10000004','000001','1');
insert into CP values('10000004','000003','3');
insert into CP values('10000004','000005','2');
insert into CP values('10000005','000003','4');
insert into CP values('10000005','000002','1');
insert into CP values('10000005','000004','3');
insert into CP values('10000005','000005','5');
insert into CP values('10000005','000001','4');
2、创建与使用视图
①、
CREATE VIEW C_P_N
AS
SELECT CP.cno,cna,CP.pno,pna,num
FROM PAPER,CP,CUSTOMER
WHERE CP.cno=CUSTOMER.cno AND CP.pno=PAPER.pno;
②、ALTER VIEW C_P_N
AS
SELECT PAPER.pno,pna,CUSTOMER.cno,cna,num,ppr
FROM PAPER,CUSTOMER,CP;
③、
查询“人物日报”的订阅数量
SELECT *
FROM C_P_N
WHERE pna='人物日报';
将李涛顾客改为罗猪猪
UPDATE C_P_N
SET cna='罗猪猪'
WHERE cna='李涛';
发现表中数据也改变了
将‘人物日报’修改为‘人民日报’
UPDATE C_P_N
SET pna='人民日报'
WHERE pna='人物日报';
有重复的经过和老师的探讨修改了代码如下
①、
CREATE VIEW C_P_N
AS
SELECT CP.cno,cna,CP.pno,pna,num
FROM PAPER,CP,CUSTOMER
WHERE CP.cno=CUSTOMER.cno AND CP.pno=PAPER.pno;
②、ALTER VIEW C_P_N
AS
SELECT CP.cno,cna,CP.pno,pna,num,ppr
FROM PAPER,CUSTOMER,CP
WHERE CP.cno=CUSTOMER.cno AND CP.pno=PAPER.pno;
查询“人民日报”的订阅数量
SELECT *
FROM C_P_N
WHERE pna='人民日报';
④、DROP VIEW C_P_N;
更新多个数据表格:
ALTER VIEW C_P_N
AS
SELECT CP.cno,cna,CP.pno,pna
FROM PAPER,CUSTOMER,CP
WHERE CP.cno=CUSTOMER.cno AND CP.pno=PAPER.pno;
五、 实验体会
通过这次实验学会了主要建表时需要将几个表中重复的数据进行合并,并且对更新视图的语言记忆更加深刻了,中间因为表格中是人物日报,但是要我们查找的是人民日报而出现错误,将UPDATE语句中的‘人民日报’改为‘人物日报’之后就能查询到对应的数据
附:实验准备代码
CREATE TABLE PAPER
(
pno char(9) PRIMARY KEY,
pna char(9),
ppr float
);
CREATE TABLE CUSTOMER
(
cno char(9) PRIMARY KEY,
cna char(9) UNIQUE NOT NULL,
adr char(20)
);
CREATE TABLE CP
(cno CHAR(9),
pno CHAR(9),
num int DEFAULT 1,
PRIMARY KEY(cno,pno),
FOREIGN KEY(cno) REFERENCES CUSTOMER(cno),
FOREIGN KEY(pno) REFERENCES PAPER(pno)
);
insert into PAPER values('000001','人物日报','12.5');
insert into PAPER values('000002','解放日报','14.5');
insert into PAPER values('000003','光明日报','10.5');
insert into PAPER values('000004','青年报','11.5');
insert into PAPER values('000005','扬子网报','18.5');
insert into CUSTOMER values('10000001','李涛','无锡市解放东路123号');
insert into CUSTOMER values('10000002','钱金浩','无锡市人民西路234号');
insert into CUSTOMER values('10000003','邓杰','无锡市惠河路270号');
insert into CUSTOMER values('10000004','朱海红','无锡市中山东路432号');
insert into CUSTOMER values('10000005','欧阳阳文','无锡市中山东路532号');
insert into CP values('10000001','000001','2');
insert into CP values('10000001','000002','4');
insert into CP values('10000001','000005','6');
insert into CP values('10000002','000001','2');
insert into CP values('10000002','000003','2');
insert into CP values('10000002','000005','2');
insert into CP values('10000003','000003','2');
insert into CP values('10000003','000004','4');
insert into CP values('10000004','000001','1');
insert into CP values('10000004','000003','3');
insert into CP values('10000004','000005','2');
insert into CP values('10000005','000003','4');
insert into CP values('10000005','000002','1');
insert into CP values('10000005','000004','3');
insert into CP values('10000005','000005','5');
insert into CP values('10000005','000001','4');
CREATE VIEW C_P_N
AS
SELECT CP.cno,cna,CP.pno,pna,num
FROM PAPER,CP,CUSTOMER
WHERE CP.cno=CUSTOMER.cno AND CP.pno=PAPER.pno;
ALTER VIEW C_P_N
AS
SELECT CP.cno,cna,CP.pno,pna,num,ppr
FROM PAPER,CUSTOMER,CP
WHERE CP.cno=CUSTOMER.cno AND CP.pno=PAPER.pno;
SELECT *
FROM C_P_N
WHERE pna='人民日报';
UPDATE C_P_N
SET cna='罗猪猪'
WHERE cna='李涛';
UPDATE C_P_N
SET pna='人民日报'
WHERE pna='人物日报';
DROP VIEW C_P_N;
标签:insert,数据库,视图,表与,pno,cno,values,CP,into From: https://www.cnblogs.com/lyyds666jyqdm/p/16836698.html