MySQL数据库应用案例课堂
上QQ阅读APP看书,第一时间看更新

7.3 查看视图

查看视图是查看数据库中已有的视图的定义。查看视图必须要有SHOW VIEW的权限,MySQL数据库下的user表中保存着这个信息。查看视图的方法有DESCRIBE、SHOW TABLE STATUS和SHOW CREATE VIEW,本节将介绍查看视图的各种方法。

7.3.1 用DESCRIBE语句查看视图的基本信息

用DESCRIBE语句查看视图的具体语法如下:

  DESCRIBE 视图名;

【例7.4】通过DESCRIBE语句查看视图view_t的定义。

代码如下:

  DESCRIBE view_t;

代码执行结果如下:

  mysql> DESCRIBE view_t;
  +-----------------+------------+------+-----+---------+-------+
  | Field           | Typ        | 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)

执行结果显示出了视图的字段定义、字段的数据类型、是否为空、是否为主/外键、默认值和额外信息。

一般情况下,DESCRIBE都可简写成DESC,输入DESC的执行结果和输入DESCRIBE的执行结果是一样的。

7.3.2 用SHOW TABLE STATUS语句查看视图的基本信息

用SHOW TABLE STATUS语句查看视图的具体语法如下:

  SHOW TABLE STATUS LIKE '视图名';

【例7.5】用SHOW TABLE STATUS语句查看视图信息。

代码如下:

  SHOW TABLE STATUS LIKE 'view_t' \G

执行结果如下:

  mysql> SHOW TABLE STATUS LIKE 'view_t' \G
  *************************** 1. row ***************************
               Name: view_t
             Engine: NULL
            Version: NULL
        Row_format: NULL
              Rows: NULL
   Avg_row_length: NULL
      Data_length: NULL
  Max_data_length: NULL
     Index_length: NULL
        Data_free: NULL
   Auto_increment: NULL
      Create_time: NULL
      Update_time: NULL
       Check_time: NULL
        Collation: NULL
         Checksum: NULL
   Create_options: NULL
          Comment: VIEW
  1 row in set (0.01 sec)

执行结果显示,Comment的值为VIEW说明该表为视图,其他的信息也为NULL说明这是一个虚表。用同样的语句查看数据表t的信息,执行结果如下:

  mysql> SHOW TABLE STATUS LIKE 't' \G
  *************************** 1. row ***************************
              Name: t
            Engine: InnoDB
           Version: 10
        Row_format: Compact
              Rows: 1
   Avg_row_length: 16384
      Data_length: 16384
  Max_data_length: 0
     Index_length: 0
        Data_free: 9437184
   Auto_increment: NULL
      Create_time: 2011-09-04 14:04:55
      Update_time: NULL
       Check_time: NULL
        Collation: utf8_general_ci
         Checksum: NULL
    Create_options:
          Comment:
  1 row in set (0.00 sec)

从查询的结果来看,这里的信息包含存储引擎、创建时间等,Comment的值为空,这就是视图和表的区别。

7.3.3 用SHOW CREATE VIEW语句查看视图的详细信息

用SHOW CREATE VIEW语句查看视图的详细定义,语法格式如下:

  SHOW CREATE VIEW 视图名;

【例7.6】用SHOW CREATE VIEW查看视图的详细定义。

代码如下:

  SHOW CREATE VIEW view_t \G

执行结果如下:

  mysql> SHOW CREATE VIEW view_t \G
  *************************** 1. row ***************************
               View: view_t
  Create View: CREATE ALGORITHM=UNDEFINED  DEFINER='root'@'localhost' SQL SECURITY DEFINER VIEW 'view_t'
   AS select 't'.'quantity' AS 'quantity','t'.'price' AS 'price', ('t'.'quantity' * 't'.'price') AS 'quantity *price' from 't'
  character_set_client: utf8
  collation_connection: utf8_general_ci
  1 row in set (0.00 sec)

执行结果显示了视图的名称、创建视图的语句等信息。

7.3.4 在views表中查看视图的详细信息

在MySQL中,information_schema数据库下的views表中存储了所有视图的定义。通过对views表的查询,可以查看数据库中所有视图的详细信息,查询语句如下:

  SELECT * FROM information_schema.views;

【例7.7】在views表中查看视图的详细定义。

代码如下:

  mysql> SELECT * FROM information_schema.views\G
  *************************** 1. row ***************************
          TABLE_CATALOG: def
           TABLE_SCHEMA: chapter11db
             TABLE_NAME: stu_glass
        VIEW_DEFINITION: select 'chapter11db'.'student'.'s_id' AS 'id', 'chapter11db'.'student'.'name' AS 'name','chapter11db'.'stu_info'.'glass' AS 'glass' from 'chapter11db'.'student' join 'chapter11db'.'stu_info' where ('chapter11db'.'student'.'s_id' = 'chapter11db'.'stu_info'.'s_id')
          CHECK_OPTION: NONE
          IS_UPDATABLE: YES
               DEFINER: root@localhost
         SECURITY_TYPE: DEFINER
  CHARACTER_SET_CLIENT: utf8
  COLLATION_CONNECTION: utf8_general_ci
  *************************** 2. row ***************************
          TABLE_CATALOG: def
           TABLE_SCHEMA: chapter11db
            TABLE_NAME: view_t
       VIEW_DEFINITION: select 'chapter11db'.'t'.'quantity' AS 'quantity', 'chapter11db'.'t'.'price' AS 'price',('chapter11db'.'t'.'quantity' * 'chapter11db'.'t'.'price') AS 'quantity *price' from 'chapter11db'.'t'
          CHECK_OPTION: NONE
          IS_UPDATABLE: YES
               DEFINER: root@localhost
         SECURITY_TYPE: DEFINER
  CHARACTER_SET_CLIENT: utf8
  COLLATION_CONNECTION: utf8_general_ci
  *************************** 3. row ***************************
          TABLE_CATALOG: def
           TABLE_SCHEMA: chapter11db
            TABLE_NAME: view_t2
       VIEW_DEFINITION: select 'chapter11db'.'t'.'quantity' AS 'qty', 'chapter11db'.'t'.'price' AS 'price',('chapter11db'.'t'.'quantity' * 'chapter11db'.'t'.'price') AS 'total' from 'chapter11db'.'t'
          CHECK_OPTION: NONE
          IS_UPDATABLE: YES
               DEFINER: root@localhost
         SECURITY_TYPE: DEFINER
  CHARACTER_SET_CLIENT: utf8
  COLLATION_CONNECTION: utf8_general_ci
  3 rows in set (0.03 sec)

查询的结果中显示了当前以及定义的所有视图的详细信息,在这里也可以看到前面定义的名为stu_glass、view_t和view_t2的三个视图的详细信息。