数据库系统实验3:创建数据库以及表,创建和使用视图

数据库系统实验3:创建数据库以及表,创建和使用视图

实验环境

windows10操作系统,使用MySQL 5.5 Command Line完成实验

实验内容

实验课教材(MySQL数据库技术与实验指导)第66页“实验内容与要求”1-2题

步骤及过程

创建数据库以及表

创建数据库,并使用数据库

create DATABASE DingBao;
use DingBao; 

创建报纸编码表paper,以pno为主键

CREATE TABLE IF NOT EXISTS paper(
pno VARCHAR(10) NOT NULL,
pna VARCHAR(20) NOT NULL,
ppr DOUBLE NOT NULL,
PRIMARY KEY (pno)
) ENGINE=InnoDB;

向表paper中插入数据

INSERT INTO paper
VALUES
(“000001”,”人民日报”,12.5),
(“000002”,”解放军报”,14.5),
(“000003”,”光明日报”,10.5),
(“000004”,”青年报”,11.5),
(“000005”,”扬子日报”,18.5); 

创建顾客编码表customer,以cno为主键

CREATE TABLE IF NOT EXISTS customer(
cno VARCHAR(10) NOT NULL,
cna VARCHAR(50) NOT NULL,
adr VARCHAR(100) NOT NULL,
PRIMARY KEY (cno)
) ENGINE=InnoDB;

向表customer中插入数据

INSERT INTO customer
VALUES
(“0001”,”李涛”,”无锡市解放东路123号”),
(“0002”,”钱金浩”, ”无锡市人民西路234号”),
(“0003”,”邓杰”, ”无锡市惠河路432号”),
(“0004”,”朱海红”, ”无锡市中山东路432号”),
(“0005”,”欧阳阳文”, ”无锡市中山东路532号”); 

创建报纸订阅表,以pno与cno为主键,num默认值为1

CREATE TABLE IF NOT EXISTS cp(
cno VARCHAR(10) NOT NULL,
pno VARCHAR(10) NOT NULL,
num INT DEFAULT 1,
PRIMARY KEY (cno, pno)
) ENGINE=InnoDB;

向表cp中插入数据

INSERT INTO cp
VALUES
(“0001”,”000001”,2),
(“0001”,”000002”,4),
(“0001”,”000005”,6),
(“0002”,”000001”,2),
(“0002”,”000003”,2),
(“0002”,”000005”,2),
(“0003”,”000003”,2),
(“0003”,”000004”,4),
(“0004”,”000001”,1),
(“0004”,”000003”,3),
(“0004”,”000005”,2),
(“0005”,”000003”,4),
(“0005”,”000002”,1),
(“0005”,”000004”,3),
(“0005”,”000005”,5),
(“0005”,”000001”,4);

创建和使用视图

创建视图C_P_N

create view C_P_N (cno, cna, pno, pna, num) as 
select customer.cno, cna, paper.pno, pna, num
from customer, paper, cp
where customer.cno=cp.cno and paper.pno=cp.pno;

修改视图,增加报纸单价信息

alter view C_P_N (cno, cna, pno, pna, num, ppr) as 
select customer.cno, cna, paper.pno, pna, num, ppr
from customer, paper, cp
where customer.cno=cp.cno and paper.pno=cp.pno;

通过视图查询”人民日报”被订阅的情况

select * from C_P_N where pna = “人民日报”;

并实现对数据的更新操作

  • 修改”邓杰”订阅”光明日报”的份数为20
update C_P_N set num=20 where cna=”邓杰” and pna=“光明日报”;
  • 修改”扬子日报”的名称为”暴走日报”
update C_P_N set pna=”暴走日报” where pna=”扬子日报”;

删除视图C_P_N

drop view C_P_N;
展开阅读全文

没有更多推荐了,返回首页