如何在MySQL中创建视图?
要在MySQL中创建视图,可以使用CREATE VIEW命令。语法如下-
CREATE VIEW yourViewName as SELECT yourColumName1, yourColumName2, yourColumName3,...... yourColumNameN from yourTableName;
为了理解上述语法,让我们首先创建一个表。以下是创建表的查询-
create table ViewDemo−> (
−> Id int,
−> Name varchar(200),
−> Age int
−> );
实现以上语法以创建视图。以下是在MySQL中创建视图的查询-
create view LookUpTable as select Id,Name from ViewDemo;
现在检查是否已使用show命令创建视图。查询如下-
show create table LookUpTable;
以下是输出-
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+| View | Create View | character_set_client | collation_connection |
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| lookuptable | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `lookuptable` AS select `viewdemo`.`Id` AS `Id`,`viewdemo`.`Name` AS `Name` from `viewdemo` | utf8 | utf8_general_ci |
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)
以上是 如何在MySQL中创建视图? 的全部内容, 来源链接: utcz.com/z/331530.html