关闭

MySQL:视图

175人阅读 评论(0) 收藏 举报
分类:
/*样例表*/
CREATE TABLE student
(
  s_id  INT,
  name  VARCHAR(40)
);

CREATE TABLE stu_info
(
  s_id   INT,
  glass  VARCHAR(40),
  addr   VARCHAR(90)
);

【例11.1】在t表格上创建一个名为view_t的视图,代码如下:
CREATE TABLE t (qty INT, price INT);         /*创建基本表t*/
INSERT INTO t VALUES(3, 50);                 /*插入记录*/
CREATE VIEW view_t AS SELECT quantity, price, quantity *price FROM t;    /*创建视图view_t*/
SELECT * FROM view_t;


【例11.2】在t表格上创建一个名为view_t2的视图,代码如下:
CREATE VIEW view_t2(qty, price, total ) AS SELECT quantity, price, quantity *price FROM t;
SELECT * FROM view_t2;


【例11.3】在表student和表stu_info上创建视图stu_glass,代码如下:
INSERT INTO student VALUES(1,'wanglin1'),(2,'gaoli'),(3,'zhanghai');                /*插入记录*/
INSERT INTO stu_info VALUES(1, 'wuban','henan'),(2,'liuban','hebei'),(3,'qiban','shandong');

CREATE VIEW stu_glass (id,name, glass) AS SELECT student.s_id,student.name ,stu_info.glass FROM student ,stu_info WHERE student.s_id=stu_info.s_id;

SELECT * FROM stu_glass;


【例11.4】通过DESCRIBE语句查看视图view_t的定义,代码如下:
DESCRIBE view_t;


【例11.5】下面将通过一个例子来学习使用SHOW TABLE STATUS命令查看视图信息,代码如下:
SHOW TABLE STATUS LIKE 'view_t' \G;
SHOW TABLE STATUS LIKE 't' \G;

【例11.6】SHOW CREATE VIEW查看视图的详细定义,代码如下:
SHOW CREATE VIEW view_t \G;


【例11.7】在views表中查看视图的详细定义,代码如下:
SELECT * FROM information_schema.views \G;


【例11.8】修改视图view_t,代码如下:
DESC view_t;
CREATE OR REPLACE VIEW view_t AS SELECT * FROM t;
DESC view_t;

【例11.9】使用ALTER语句修改视图view_t,代码如下:
DESC view_t;
ALTER VIEW view_t AS SELECT quantity FROM t;
DESC view_t;

【例11.10】使用UPDATE语句更新视图view_t,代码如下:
SELECT * FROM view_t;          /*查看更新之前的视图*/
SELECT * FROM t;               /*查看更新之前的表*/
UPDATE view_t SET quantity=5;  /*更新视图*/
SELECT * FROM t;               /*查看更新之后的表*/
SELECT * FROM view_t;          /*查看更新之后的视图*/
SELECT * FROM view_t2;

【例11.11】使用INSERT语句在基本表t中插入一条记录,代码如下:
INSERT INTO t VALUES (3,5);
SELECT * FROM t;
SELECT * FROM view_t2;

【例11.12】删除stu_glass视图,代码如下:
DROP VIEW IF EXISTS stu_glass;
SHOW CREATE VIEW stu_glass;




综合案例

步骤1:创建学生表stu,插入3条记录。
CREATE TABLE stu
(
s_id INT PRIMARY KEY,
s_name VARCHAR(20),
addr VARCHAR(50),
tel VARCHAR(50)
);
INSERT INTO stu
VALUES(1,'XiaoWang','Henan','0371-12345678'),
(2,'XiaoLi','Hebei','13889072345'),
(3,'XiaoTian','Henan','0371-12345670');


步骤2:创建报名表sign,插入3条记录。
CREATE TABLE sign
(
s_id INT PRIMARY KEY,
s_name VARCHAR(20),
s_sch VARCHAR(50),
s_sign_sch VARCHAR(50)
);
INSERT INTO sign
VALUES(1,'XiaoWang','Middle School1','Peking University'),
(2,'XiaoLi','Middle School2','Tsinghua University'),
(3,'XiaoTian','Middle School3','Tsinghua University');


步骤3:创建成绩表stu_mark,插入3条记录。
CREATE TABLE stu_mark (s_id INT PRIMARY KEY ,s_name VARCHAR(20) ,mark int );
INSERT INTO stu_mark VALUES(1,'XiaoWang',80),(2,'XiaoLi',71),(3,'XiaoTian',70);


步骤4:创建考上Peking University的学生的视图
CREATE VIEW beida (id,name,mark,sch)
AS SELECT stu_mark.s_id,stu_mark.s_name,stu_mark.mark, sign.s_sign_sch
FROM stu_mark ,sign
WHERE stu_mark.s_id=sign.s_id AND stu_mark.mark>=41 AND sign.s_sign_sch='Peking University';


步骤5:创建考上Tsinghua University的学生的视图
CREATE VIEW qinghua (id,name,mark,sch)
AS SELECT stu_mark.s_id, stu_mark.s_name, stu_mark.mark, sign.s_sign_sch
FROM stu_mark ,sign
WHERE stu_mark.s_id=sign.s_id  AND stu_mark.mark>=40 AND sign.s_sign_sch='Tsinghua University';



步骤6:XiaoTian的成绩在录入的时候录入错误多录了50分,对其录入成绩进行更正。
UPDATE stu_mark SET mark = mark-50 WHERE stu_mark.s_name ='XiaoTian';


步骤7:查看更新过后视图和表的情况。
SELECT * FROM stu_mark;
SELECT * FROM qinghua;
SELECT * FROM beida;

步骤8:查看视图的创建信息。
 SELECT * FROM information_schema.views\G


步骤9:删除创建的视图。
DROP VIEW beida;
DROP VIEW qinghua;

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:344565次
    • 积分:6167
    • 等级:
    • 排名:第4223名
    • 原创:195篇
    • 转载:485篇
    • 译文:0篇
    • 评论:185条
    最新评论