1. 程式人生 > >第11章 檢視

第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;