第11章 视图

视图是一个虚拟表,是从数据库中一个或多个表中导出来的表。
虚拟视图不能存储;物化视图可存储。
视图的作用:
1.简单化
2.安全性
3.逻辑数据独立性

11.2 创建视图
11.2.2 在单表上创建视图
例:

mysql> CREATE DATABASE view_test;
Query OK, 1 row affected (0.00 sec)

mysql> USE view_test;
Database changed
mysql> CREATE TABLE t(quantity INT,price INT);
Query OK, 0 rows affected (3.12 sec)

mysql> INSERT INTO t VALUES (3,50);
Query OK, 1 row affected (1.53 sec)

创建视图:

mysql> CREATE VIEW view_t AS SELECT quantity,price,quantity*price FROM t;
Query OK, 0 rows affected (0.13 sec)

mysql> SELECT * FROM view_t;
+----------+-------+----------------+
| quantity | price | quantity*price |
+----------+-------+----------------+
|        3 |    50 |            150 |
+----------+-------+----------------+
1 row in set (0.07 sec)

例:在t表格上创建view_t2的视图,代码如下:

mysql> CREATE VIEW view_t2(qty,price,total) AS SELECT quantity,price,quantity*price FROM t;
Query OK, 0 rows affected (0.04 sec)

mysql> SELECT * FROM view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    3 |    50 |   150 |
+------+-------+-------+
1 row in set (0.00 sec)

11.2.3 在多个表上创建视图
例:在表student和表stu_info上创建视图stu_calss,代码如下:

mysql> CREATE TABLE student(
    -> s_id INT NOT NULL,
    -> name VARCHAR(15)
    -> );
Query OK, 0 rows affected (0.38 sec)

mysql> CREATE TABLE stu_info(
    -> s_id INT NOT NULL,
    -> class VARCHAR(10),
    -> address VARCHAR(10)
    -> );
Query OK, 0 rows affected (0.25 sec)

mysql> INSERT INTO student VALUES (1,'wanglin1'),(2,'gaoli'),(3,'zhanghai');
Query OK, 3 rows affected (0.13 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> INSERT INTO stu_info VALUES (1,'wuban','henan'),(2,'liuban','hebei'),(3,'qiban','shandong');
Query OK, 3 rows affected (0.13 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> CREATE VIEW stu_class(id,name,class) AS SELECT student.s_id,student.name,stu_info.class FROM student,stu_info WHERE student.s_id=stu_info.s_id;
Query OK, 0 rows affected (0.13 sec)

mysql> SELECT * FROM stu_class;
+----+----------+--------+
| id | name     | class  |
+----+----------+--------+
|  1 | wanglin1 | wuban  |
|  2 | gaoli    | liuban |
|  3 | zhanghai | qiban  |
+----+----------+--------+
3 rows in set (0.00 sec)

注:视图不像数据表一样实时更新->可延时。

11.3 查看视图

  • DESCRIBE 视图名
  • SHOW TABLE STATUS
    基本语句:SHOW TABLE STATUS LIKE '视图名;'
  • SHOW CREATE VIEW + 视图名
  • 在views表中查看视图详细信息
    查询语句:SELECT * FROM information_schema.views;

11.4 修改视图

mysql> DESC view_t;
+----------------+------------+------+-----+---------+-------+
| Field          | Type       | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| quantity       | int(11)    | YES  |     | NULL    |       |
| price          | int(11)    | YES  |     | NULL    |       |
| quantity*price | bigint(21) | YES  |     | NULL    |       |
+----------------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> CREATE OR REPLACE VIEW view_t AS SELECT * FROM t;
Query OK, 0 rows affected (0.05 sec)

mysql> DESC view_t;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| quantity | int(11) | YES  |     | NULL    |       |
| price    | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

可以看到,相比原来的view_t,新的视图view_t少了1个字段。

11.4.2 ALTER语句修改视图
例:使用ALTER语句修改视图view_t:


mysql> ALTER VIEW view_t AS SELECT quantity FROM t;
Query OK, 0 rows affected (0.08 sec)

mysql> DESC view_t;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| quantity | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

11.5 更新视图
更新视图有三种方法:

  • INSERT
    例:使用INSERT向t插入一条记录
mysql> INSERT INTO t VALUES(3,5);
Query OK, 1 row affected (0.15 sec)

mysql> SELECT * FROM t;
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
|        3 |     5 |
+----------+-------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    3 |    50 |   150 |
|    3 |     5 |    15 |
+------+-------+-------+
2 rows in set (0.00 sec)

  • UPDATE
    例:使用UPDATE语句更新视图view_t:
mysql> UPDATE view_t SET quantity=5;
Query OK, 2 rows affected (0.16 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> SELECT * FROM view_t;
+----------+
| quantity |
+----------+
|        5 |
|        5 |
+----------+
2 rows in set (0.00 sec)
  • DELETE(删除视图记录)
    例:使用DELETE语句删除视图view_t2中的一条记录:

mysql> DELETE FROM view_t2 WHERE PRICE=5;
Query OK, 1 row affected (0.14 sec)

mysql> SELECT * FROM view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    5 |    50 |   250 |
+------+-------+-------+
1 row in set (0.00 sec)

11.6 删除视图
使用DROP VIEW语句。
例:删除stu_class视图:


mysql> DROP VIEW IF EXISTS stu_class;
Query OK, 0 rows affected (0.17 sec)

mysql> SHOW CREATE VIEW stu_class;
ERROR 1146 (42S02): Table 'view_test.stu_class' doesn't exist

11.7 综合案例
01 创建学生表stu,插入3条记录:

mysql> CREATE TABLE stu(
    -> s_id INT PRIMARY KEY,
    -> s_name VARCHAR(20) NOT NULL,
    -> addr VARCHAR(20) NOT NULL,
    -> tel VARCHAR(50) NOT NULL
    -> );
Query OK, 0 rows affected (0.32 sec)

mysql> INSERT INTO stu
    -> VALUES(1,'xiaoming','henan','0371-12345678'),
    -> (2,'xiaoli','hebei','13889072345'),
    -> (3,'xiaotian','henan','0371-12345670');
Query OK, 3 rows affected (0.16 sec)
Records: 3  Duplicates: 0  Warnings: 0

02创建报名表sign,插入3条记录:


mysql> CREATE TABLE sign(
    -> s_id INT PRIMARY KEY,
    -> s_name VARCHAR(20) NOT NULL,
    -> s_sch VARCHAR(50) NOT NULL,
    -> s_sign_sch VARCHAR(50) NOT NULL
    -> );
Query OK, 0 rows affected (0.40 sec)

mysql> INSERT INTO sign
    -> VALUES(1,'xiaowang','Middle School1','Peking University'),
    -> (2,'xiaoli','Middle School2','Tsinghua University'),
    -> (3,'xiaotian','Middle School3','Tsinghua University');
Query OK, 3 rows affected (0.13 sec)
Records: 3  Duplicates: 0  Warnings: 0

03 创建成绩表stu_mark,插入3条记录:


mysql> CREATE TABLE stu_mark(
    -> s_id INT PRIMARY KEY,
    -> s_name VARCHAR(20) NOT NULL,
    -> mark int NOT NULL
    -> );
Query OK, 0 rows affected (0.34 sec)

mysql> INSERT INTO stu_mark
    -> VALUES(1,'xiaowang',80),
    -> (2,'xiaoli',71),
    -> (3,'xiaotian',70);
Query OK, 3 rows affected (0.13 sec)
Records: 3  Duplicates: 0  Warnings: 0

04 创建考上Peking University的学生视图:


mysql> 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';
Query OK, 0 rows affected (0.04 sec)

mysql> SELECT * FROM beida;
+----+----------+------+-------------------+
| id | name     | mark | sch               |
+----+----------+------+-------------------+
|  1 | xiaowang |   80 | Peking University |
+----+----------+------+-------------------+
1 row in set (0.03 sec)

05 创建考上Tsinghua University的学生视图:

mysql> 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='Tsing University';
Query OK, 0 rows affected (0.13 sec)

mysql> SELECT * FROM qinghua;

你可能感兴趣的:(第11章 视图)