手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Mysql>列表

如何创建MySQL5的视图

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
CHECK TABLE statement.

  • The definition cannot refer to a TEMPORARY table, and you cannot create a TEMPORARY view.

  • The tables named in the view definition must already exist.

  • You cannot associate a trigger with a view.

  • ORDER BY is allowed in a view definition, but it is ignored if you select from a view using a statement that has its own ORDER BY.

    For other options or clauses in the definition, they are added to the options or clauses of the statement that references the view, but the effect is undefined. For example, if a view definition includes a LIMIT clause, and you select from the view using a statement that has its own LIMIT clause, it is undefined which limit applies. This same principle applies to options such as ALL, DISTINCT, or SQL_SMALL_RESULT that follow the SELECT keyword, and to clauses such as INTO, FOR UPDATE, LOCK IN SHARE MODE, and PROCEDURE.

    If you create a view and then change the query processing environment by changing system variables, that may affect the results you get from the view:

    mysql> CREATE VIEW v AS SELECT CHARSET(CHAR(65)), COLLATION(CHAR(65));
    
    Query OK, 0 rows affected (0.00 sec)
    
    
    
    mysql> SET NAMES 'latin1';
    
    Query OK, 0 rows affected (0.00 sec)
    
    
    
    mysql> SELECT * FROM v;
    
     ------------------- --------------------- 
    
    | CHARSET(CHAR(65)) | COLLATION(CHAR(65)) |
    
     ------------------- --------------------- 
    
    | latin1            | latin1_swedish_ci   |
    
     ------------------- --------------------- 
    
    1 row in set (0.00 sec)
    
    
    
    mysql> SET NAMES 'utf8';
    
    Query OK, 0 rows affected (0.00 sec)
    
    
    
    mysql> SELECT * FROM v;
    
     ------------------- --------------------- 
    
    | CHARSET(CHAR(65)) | COLLATION(CHAR(65)) |
    
     ------------------- --------------------- 
    
    | utf8              | utf8_general_ci     |
    
     ------------------- --------------------- 
    
    1 row in set (0.00 sec)
    
    

    The optional ALGORITHM clause is a MySQL extension to standard SQL. ALGORITHM takes three values: MERGE, TEMPTABLE, or UNDEFINED. The default algorithm is UNDEFINED if no ALGORITHM clause is present. The algorithm affects how MySQL processes the view.

    For MERGE, the text of a statement that refers to the view and the view definition are merged such that parts of the view definition replace corresponding parts of the statement.

    For TEMPTABLE, the results from the view are retrieved into a temporary table, which then is used to execute the statement.

    For UNDEFINED, MySQL chooses which algorithm to use. It prefers MERGE over TEMPTABLE if possible, because MERGE is usually more efficient and because a view cannot be updatable if a temporary table is used.

    A reason to choose TEMPTABLE explicitly is that locks can be released on underlying tables after the temporary table has been created and before it is used to finish processing the statement. This might result in quicker lock release than the MERGE algorithm so that other clients that use the view are not blocked as long.

    A view algorithm can be UNDEFINED three ways: