如何了解 MySQL 视图的构成?


以下为语法 −

show create view yourViewName;

让我们首先创建一个表 −

mysql> create table DemoTable
-> (
-> StudentName varchar(20)
-> );
Query OK, 0 rows affected (0.56 sec)

使用插入命令在表中插入一些记录 −

mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.13 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

输出

这将生成以下输出 −

+-------------+
| StudentName |
+-------------+
| Chris       |
| Robert      |
| David       |
+-------------+
3 rows in set (0.00 sec)

以下是为表创建视图的查询 −

mysql> CREATE VIEW view_DemoTable AS SELECT StudentName from DemoTable;
Query OK, 0 rows affected (0.12 sec)

以下是查看 MySQL 视图的构成的查询 −

mysql> show create view view_DemoTable;

输出

这将生成以下输出 −

+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View              | Create View | character_set_client | collation_connection |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| view_DemoTable     | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL
SECURITY DEFINER VIEW `view_DemoTable` AS select `DemoTable`.`StudentName` AS
`StudentName` from `DemoTable` | utf8 | utf8_general_ci |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

让我们检查视图记录 −

mysql> select *from view_DemoTable;

输出

这将生成以下输出 −

+-------------+
| StudentName |
+-------------+
| Chris       |
| Robert      |
| David       |
+-------------+
3 rows in set (0.05 sec)

更新于: 2020 年 6 月 30 日

119 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告