如何创建MySQL5的视图
2008-02-23 07:43:03来源:互联网 阅读 ()
基本语法:
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEWview_name
[(column_list
)] ASselect_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
This statement creates a new view, or replaces an existing one if the OR REPLACE
clause is given. The select_statement
is a SELECT
statement that provides the definition of the view. The statement can select from base tables or other views.
This statement requires the CREATE VIEW
privilege for the view, and some privilege for each column selected by the SELECT
statement. For columns used elsewhere in the SELECT
statement you must have the SELECT
privilege. If the OR REPLACE
clause is present, you must also have the DELETE
privilege for the view.
A view belongs to a database. By default, a new view is created in the current database. To create the view explicitly in a given database, specify the name as db_name.view_name
when you create it.
mysql> CREATE VIEW test.v AS SELECT * FROM t;
Tables and views share the same namespace within a database, so a database cannot contain a table and a view that have the same name.
Views must have unique column names with no duplicates, just like base tables. By default, the names of the columns retrieved by the SELECT
statement are used for the view column names. To define explicit names for the view columns, the optional column_list
clause can be given as a list of comma-separated identifiers. The number of names in column_list
must be the same as the number of columns retrieved by the SELECT
statement.
Columns retrieved by the SELECT
statement can be simple references to table columns. They can also be expressions that use functions, constant values, operators, and so forth.
Unqualified table or view names in the SELECT
statement are interpreted with respect to the default database. A view can refer to tables or views in other databases by qualifying the table or view name with the proper database name.
A view can be created from many kinds of SELECT
statements. It can refer to base tables or other views. It can use joins, UNION
, and subqueries. The SELECT
need not even refer to any tables. The following example defines a view that selects two columns from another table, as well as an expression calculated from those columns:
mysql>CREATE TABLE t (qty INT, price INT);
mysql>INSERT INTO t VALUES(3, 50);
mysql>CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
mysql>SELECT * FROM v;
------ ------- ------- | qty | price | value | ------ ------- ------- | 3 | 50 | 150 | ------ ------- -------
A view definition is subject to the following restrictions:
-
The
SELECT
statement cannot contain a subquery in theFROM
clause. -
The
SELECT
statement cannot refer to system or user variables. -
The
SELECT
statement cannot refer to prepared statement parameters. -
Within a stored routine, the definition cannot refer to routine parameters or local variables.
-
Any table or view referred to in the definition must exist. However, after a view has been created, it is possible to drop a table or view that the definition refers to. To check a view definition for problems of this kind, use the
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇: 修改MySQL的默认密码
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash