Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > ebb1914cf182a88528b4547490db1dd8 > files > 395

kdewebdev-quanta-doc-3.5.9-2mdv2008.1.x86_64.rpm

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Chapter 19. Views</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.1"><link rel="start" href="index.html" title="MySQL 5.0 Reference Manual"><link rel="up" href="index.html" title="MySQL 5.0 Reference Manual"><link rel="prev" href="triggers.html" title="Chapter 18. Triggers"><link rel="next" href="information-schema.html" title="Chapter 20. The INFORMATION_SCHEMA Information Database"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 19. Views</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="triggers.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="information-schema.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="views"></a>Chapter 19. Views</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="views.html#alter-view">19.1. <code class="literal">ALTER VIEW</code> Syntax</a></span></dt><dt><span class="section"><a href="views.html#create-view">19.2. <code class="literal">CREATE VIEW</code> Syntax</a></span></dt><dt><span class="section"><a href="views.html#drop-view">19.3. <code class="literal">DROP VIEW</code> Syntax</a></span></dt><dt><span class="section"><a href="views.html#show-create-view">19.4. <code class="literal">SHOW CREATE VIEW</code> Syntax</a></span></dt></dl></div><a class="indexterm" name="id3053876"></a><p>
    Views (including updatable views) are implemented in the 5.0 version
    of MySQL Server. Views are available in binary releases from 5.0.1
    and up.
  </p><p>
    This chapter discusses the following topics:
  </p><div class="itemizedlist"><ul type="disc"><li><p>
        Creating or altering views with <code class="literal">CREATE VIEW</code>
        or <code class="literal">ALTER VIEW</code>
      </p></li><li><p>
        Destroying views with <code class="literal">DROP VIEW</code>
      </p></li><li><p>
        Displaying view metadata with <code class="literal">SHOW CREATE
        VIEW</code>
      </p></li></ul></div><p>
    Discussion of restrictions on use of views is given in
    <a href="restrictions.html" title="Appendix I. Feature Restrictions">Appendix I, <i>Feature Restrictions</i></a>.
  </p><p>
    To use views if you have upgraded to MySQL 5.0.1 from an older
    release, you should upgrade your grant tables so that they contain
    the view-related privileges. See
    <a href="installing.html#upgrading-grant-tables" title="2.10.3. Upgrading the Grant Tables">Section 2.10.3, “Upgrading the Grant Tables”</a>.
  </p><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="alter-view"></a>19.1. <code class="literal">ALTER VIEW</code> Syntax</h2></div></div></div><a class="indexterm" name="id3053966"></a><pre class="programlisting">ALTER
    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = { <em class="replaceable"><code>user</code></em> | CURRENT_USER }]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW <em class="replaceable"><code>view_name</code></em> [(<em class="replaceable"><code>column_list</code></em>)]
    AS <em class="replaceable"><code>select_statement</code></em>
    [WITH [CASCADED | LOCAL] CHECK OPTION]
</pre><p>
      This statement changes the definition of an existing view. The
      syntax is similar to that for <code class="literal">CREATE VIEW</code>. See
      <a href="views.html#create-view" title="19.2. CREATE VIEW Syntax">Section 19.2, “<code class="literal">CREATE VIEW</code> Syntax”</a>. This statement requires the
      <code class="literal">CREATE VIEW</code> and <code class="literal">DROP</code>
      privileges for the view, and some privilege for each column
      referred to in the <code class="literal">SELECT</code> statement.
    </p><p>
      This statement was added in MySQL 5.0.1. The
      <code class="literal">DEFINER</code> and <code class="literal">SQL SECURITY</code>
      clauses may be used as of MySQL 5.0.16 to specify the security
      context to be used when checking access privileges at view
      invocation time. For details, see <a href="views.html#create-view" title="19.2. CREATE VIEW Syntax">Section 19.2, “<code class="literal">CREATE VIEW</code> Syntax”</a>.
    </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="create-view"></a>19.2. <code class="literal">CREATE VIEW</code> Syntax</h2></div></div></div><a class="indexterm" name="id3054082"></a><a class="indexterm" name="id3054089"></a><a class="indexterm" name="id3054099"></a><pre class="programlisting">CREATE
    [OR REPLACE]
    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = { <em class="replaceable"><code>user</code></em> | CURRENT_USER }]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW <em class="replaceable"><code>view_name</code></em> [(<em class="replaceable"><code>column_list</code></em>)]
    AS <em class="replaceable"><code>select_statement</code></em>
    [WITH [CASCADED | LOCAL] CHECK OPTION]
</pre><p>
      This statement creates a new view, or replaces an existing one if
      the <code class="literal">OR REPLACE</code> clause is given. The
      <em class="replaceable"><code>select_statement</code></em> is a
      <code class="literal">SELECT</code> statement that provides the definition
      of the view. The statement can select from base tables or other
      views.
    </p><p>
      This statement requires the <code class="literal">CREATE VIEW</code>
      privilege for the view, and some privilege for each column
      selected by the <code class="literal">SELECT</code> statement. For columns
      used elsewhere in the <code class="literal">SELECT</code> statement you must
      have the <code class="literal">SELECT</code> privilege. If the <code class="literal">OR
      REPLACE</code> clause is present, you must also have the
      <code class="literal">DROP</code> privilege for the view.
    </p><p>
      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
      <em class="replaceable"><code>db_name.view_name</code></em> when you create it.
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE VIEW test.v AS SELECT * FROM t;</code></strong>
</pre><p>
      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.
    </p><p>
      Views must have unique column names with no duplicates, just like
      base tables. By default, the names of the columns retrieved by the
      <code class="literal">SELECT</code> statement are used for the view column
      names. To define explicit names for the view columns, the optional
      <em class="replaceable"><code>column_list</code></em> clause can be given as a
      list of comma-separated identifiers. The number of names in
      <em class="replaceable"><code>column_list</code></em> must be the same as the
      number of columns retrieved by the <code class="literal">SELECT</code>
      statement.
    </p><p>
      Columns retrieved by the <code class="literal">SELECT</code> statement can
      be simple references to table columns. They can also be
      expressions that use functions, constant values, operators, and so
      forth.
    </p><p>
      Unqualified table or view names in the <code class="literal">SELECT</code>
      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.
    </p><p>
      A view can be created from many kinds of <code class="literal">SELECT</code>
      statements. It can refer to base tables or other views. It can use
      joins, <code class="literal">UNION</code>, and subqueries. The
      <code class="literal">SELECT</code> 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:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE t (qty INT, price INT);</code></strong>
mysql&gt; <strong class="userinput"><code>INSERT INTO t VALUES(3, 50);</code></strong>
mysql&gt; <strong class="userinput"><code>CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT * FROM v;</code></strong>
+------+-------+-------+
| qty  | price | value |
+------+-------+-------+
|    3 |    50 |   150 |
+------+-------+-------+
</pre><p>
      A view definition is subject to the following restrictions:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          The <code class="literal">SELECT</code> statement cannot contain a
          subquery in the <code class="literal">FROM</code> clause.
        </p></li><li><p>
          The <code class="literal">SELECT</code> statement cannot refer to system
          or user variables.
        </p></li><li><p>
          The <code class="literal">SELECT</code> statement cannot refer to
          prepared statement parameters.
        </p></li><li><p>
          Within a stored routine, the definition cannot refer to
          routine parameters or local variables.
        </p></li><li><p>
          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 <code class="literal">CHECK
          TABLE</code> statement.
        </p></li><li><p>
          The definition cannot refer to a <code class="literal">TEMPORARY</code>
          table, and you cannot create a <code class="literal">TEMPORARY</code>
          view.
        </p></li><li><p>
          The tables named in the view definition must already exist.
        </p></li><li><p>
          You cannot associate a trigger with a view.
        </p></li></ul></div><p>
      <code class="literal">ORDER BY</code> is allowed in a view definition, but
      it is ignored if you select from a view using a statement that has
      its own <code class="literal">ORDER BY</code>.
    </p><p>
      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 <code class="literal">LIMIT</code> clause, and you select from
      the view using a statement that has its own
      <code class="literal">LIMIT</code> clause, it is undefined which limit
      applies. This same principle applies to options such as
      <code class="literal">ALL</code>, <code class="literal">DISTINCT</code>, or
      <code class="literal">SQL_SMALL_RESULT</code> that follow the
      <code class="literal">SELECT</code> keyword, and to clauses such as
      <code class="literal">INTO</code>, <code class="literal">FOR UPDATE</code>,
      <code class="literal">LOCK IN SHARE MODE</code>, and
      <code class="literal">PROCEDURE</code>.
    </p><p>
      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:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE VIEW v AS SELECT CHARSET(CHAR(65)), COLLATION(CHAR(65));</code></strong>
Query OK, 0 rows affected (0.00 sec)

mysql&gt; <strong class="userinput"><code>SET NAMES 'latin1';</code></strong>
Query OK, 0 rows affected (0.00 sec)

mysql&gt; <strong class="userinput"><code>SELECT * FROM v;</code></strong>
+-------------------+---------------------+
| CHARSET(CHAR(65)) | COLLATION(CHAR(65)) |
+-------------------+---------------------+
| latin1            | latin1_swedish_ci   |
+-------------------+---------------------+
1 row in set (0.00 sec)

mysql&gt; <strong class="userinput"><code>SET NAMES 'utf8';</code></strong>
Query OK, 0 rows affected (0.00 sec)

mysql&gt; <strong class="userinput"><code>SELECT * FROM v;</code></strong>
+-------------------+---------------------+
| CHARSET(CHAR(65)) | COLLATION(CHAR(65)) |
+-------------------+---------------------+
| utf8              | utf8_general_ci     |
+-------------------+---------------------+
1 row in set (0.00 sec)
</pre><p>
      The <code class="literal">DEFINER</code> and <code class="literal">SQL SECURITY</code>
      clauses specify the security context to be used when checking
      access privileges at view invocation time. They were addded in
      MySQL 5.0.13, but have no effect until MySQL 5.0.16.
    </p><p>
      <code class="literal">CURRENT_USER</code> also can be given as
      <code class="literal">CURRENT_USER()</code>.
    </p><p>
      Within a stored routine that is defined with the <code class="literal">SQL
      SECURITY DEFINER</code> characteristic,
      <code class="literal">CURRENT_USER</code> returns the routine creator. This
      also affects a view defined within such a routine, if the view
      definition contains a <code class="literal">DEFINER</code> value of
      <code class="literal">CURRENT_USER</code>.
    </p><p>
      The default <code class="literal">DEFINER</code> value is the the user who
      executes the <code class="literal">CREATE VIEW</code> statement. (This is
      the same as <code class="literal">DEFINER = CURRENT_USER</code>.) If a
      <em class="replaceable"><code>user</code></em> value is given, it should be a
      MySQL account in
      <code class="literal">'<em class="replaceable"><code>user_name</code></em>'@'<em class="replaceable"><code>host_name</code></em>'</code>
      format (the same format as for the <code class="literal">GRANT</code>
      statement). The <em class="replaceable"><code>user_name</code></em> and
      <em class="replaceable"><code>host_name</code></em> values both are required.
    </p><p>
      If you specify the <code class="literal">DEFINER</code> clause, you cannot
      set the value to any user but your own unless you have the
      <code class="literal">SUPER</code> privilege. These rules determine the
      legal <code class="literal">DEFINER</code> user values:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          If you do not have the <code class="literal">SUPER</code> privilege, the
          only legal <em class="replaceable"><code>user</code></em> value is your own
          account, either specified literally or by using
          <code class="literal">CURRENT_USER</code>. You cannot set the definer to
          some other account.
        </p></li><li><p>
          If you have the <code class="literal">SUPER</code> privilege, you can
          specify any syntactically legal account name. If the account
          does not actually exist, a warning is generated.
        </p></li></ul></div><p>
      The <code class="literal">SQL SECURITY</code> characteristic determines
      which account to use when checking access privileges for the view
      when the view is executed. The legal characteristic values are
      <code class="literal">DEFINER</code> and <code class="literal">INVOKER</code>. These
      indicate that the view must be executable by the user who defined
      it or invoked it, respectively. The default <code class="literal">SQL
      SECURITY</code> value is <code class="literal">DEFINER</code>.
    </p><p>
      As of MySQL 5.0.16, view privileges are checked like this:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          At view definition time, the view creator must have the
          privileges needed to use the top-level objects accessed by the
          view. For example, if the view definition refers to a stored
          function, only the privileges needed to invoke the function
          can be checked. The privileges required when the function runs
          can be checked only as it executes: For different invocations
          of the function, different execution paths within the function
          might be taken.
        </p></li><li><p>
          At view execution time, privileges for objects accessed by the
          view are checked against the privileges held by the view
          creator or invoker, depending on whether the <code class="literal">SQL
          SECURITY</code> characteristic is
          <code class="literal">DEFINER</code> or <code class="literal">INVOKER</code>,
          respectively.
        </p></li><li><p>
          If view execution causes execution of a stored function,
          privilege checking for statements executed within the function
          depend on whether the function is defined with a <code class="literal">SQL
          SECURITY</code> characteristic of
          <code class="literal">DEFINER</code> or <code class="literal">INVOKER</code>. If
          the security characteristic is <code class="literal">DEFINER</code>,
          privileges within the function runs with the privileges of its
          creator. If the characteristic is <code class="literal">INVOKER</code>,
          the function runs with the privileges determined by the view's
          <code class="literal">SQL SECURITY</code> characteristic.
        </p></li></ul></div><p>
      Prior to MySQL 5.0.16 (before the <code class="literal">DEFINE</code> and
      <code class="literal">SQL SECURITY</code> clauses were implemented),
      privileges required for objects used in a view are checked at view
      creation time.
    </p><p>
      Example: A view might depend on a stored function, and that
      function might invoke other stored routines. For example, the
      following view invokes a stored function <code class="literal">f()</code>:
    </p><pre class="programlisting">CREATE VIEW v AS SELECT * FROM t WHERE t.id = f(t.name);
</pre><p>
      Suppose that <code class="literal">f()</code> contains a statement such as
      this:
    </p><pre class="programlisting">IF name IS NULL then
  CALL p1();
ELSE
  CALL p2();
END IF;
</pre><p>
      The privileges required for executing statements within
      <code class="literal">f()</code> need to be checked when
      <code class="literal">f()</code> executes. This might mean that privileges
      are needed for <code class="literal">p1()</code> or <code class="literal">p2()</code>,
      depending on the execution path within <code class="literal">f()</code>.
      Those privileges need to be checked at runtime, and the user who
      must possess the privileges is determined by the <code class="literal">SQL
      SECURITY</code> values of the function <code class="literal">f()</code>
      and the view <code class="literal">v</code>.
    </p><p>
      The <code class="literal">DEFINER</code> and <code class="literal">SQL SECURITY</code>
      clauses for views are extensions to standard SQL. In standard SQL,
      views are handled using the rules for <code class="literal">SQL SECURITY
      INVOKER</code>.
    </p><p>
      If you invoke a view that was created before MySQL 5.0.13, it is
      treated as though it was created with a <code class="literal">SQL SECURITY
      INVOKER</code> clause and with a <code class="literal">DEFINER</code>
      value that is the same as your account. However, because the
      actual definer is unknown, MySQL issues a warning. To make the
      warning go away, it is sufficient to recreate the view so that the
      view definition includes a <code class="literal">DEFINER</code> clause.
    </p><p>
      The optional <code class="literal">ALGORITHM</code> clause is a MySQL
      extension to standard SQL. <code class="literal">ALGORITHM</code> takes
      three values: <code class="literal">MERGE</code>,
      <code class="literal">TEMPTABLE</code>, or <code class="literal">UNDEFINED</code>. The
      default algorithm is <code class="literal">UNDEFINED</code> if no
      <code class="literal">ALGORITHM</code> clause is present. The algorithm
      affects how MySQL processes the view.
    </p><p>
      For <code class="literal">MERGE</code>, 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.
    </p><p>
      For <code class="literal">TEMPTABLE</code>, the results from the view are
      retrieved into a temporary table, which then is used to execute
      the statement.
    </p><p>
      For <code class="literal">UNDEFINED</code>, MySQL chooses which algorithm to
      use. It prefers <code class="literal">MERGE</code> over
      <code class="literal">TEMPTABLE</code> if possible, because
      <code class="literal">MERGE</code> is usually more efficient and because a
      view cannot be updatable if a temporary table is used.
    </p><p>
      A reason to choose <code class="literal">TEMPTABLE</code> 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
      <code class="literal">MERGE</code> algorithm so that other clients that use
      the view are not blocked as long.
    </p><p>
      A view algorithm can be <code class="literal">UNDEFINED</code> three ways:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          No <code class="literal">ALGORITHM</code> clause is present in the
          <code class="literal">CREATE VIEW</code> statement.
        </p></li><li><p>
          The <code class="literal">CREATE VIEW</code> statement has an explicit
          <code class="literal">ALGORITHM = UNDEFINED</code> clause.
        </p></li><li><p>
          <code class="literal">ALGORITHM = MERGE</code> is specified for a view
          that can be processed only with a temporary table. In this
          case, MySQL generates a warning and sets the algorithm to
          <code class="literal">UNDEFINED</code>.
        </p></li></ul></div><p>
      As mentioned earlier, <code class="literal">MERGE</code> is handled by
      merging corresponding parts of a view definition into the
      statement that refers to the view. The following examples briefly
      illustrate how the <code class="literal">MERGE</code> algorithm works. The
      examples assume that there is a view <code class="literal">v_merge</code>
      that has this definition:
    </p><pre class="programlisting">CREATE ALGORITHM = MERGE VIEW v_merge (vc1, vc2) AS
SELECT c1, c2 FROM t WHERE c3 &gt; 100;
</pre><p>
      Example 1: Suppose that we issue this statement:
    </p><pre class="programlisting">SELECT * FROM v_merge;
</pre><p>
      MySQL handles the statement as follows:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          <code class="literal">v_merge</code> becomes <code class="literal">t</code>
        </p></li><li><p>
          <code class="literal">*</code> becomes <code class="literal">vc1, vc2</code>,
          which corresponds to <code class="literal">c1, c2</code>
        </p></li><li><p>
          The view <code class="literal">WHERE</code> clause is added
        </p></li></ul></div><p>
      The resulting statement to be executed becomes:
    </p><pre class="programlisting">SELECT c1, c2 FROM t WHERE c3 &gt; 100;
</pre><p>
      Example 2: Suppose that we issue this statement:
    </p><pre class="programlisting">SELECT * FROM v_merge WHERE vc1 &lt; 100;
</pre><p>
      This statement is handled similarly to the previous one, except
      that <code class="literal">vc1 &lt; 100</code> becomes <code class="literal">c1 &lt;
      100</code> and the view <code class="literal">WHERE</code> clause is
      added to the statement <code class="literal">WHERE</code> clause using an
      <code class="literal">AND</code> connective (and parentheses are added to
      make sure the parts of the clause are executed with correct
      precedence). The resulting statement to be executed becomes:
    </p><pre class="programlisting">SELECT c1, c2 FROM t WHERE (c3 &gt; 100) AND (c1 &lt; 100);
</pre><p>
      Effectively, the statement to be executed has a
      <code class="literal">WHERE</code> clause of this form:
    </p><pre class="programlisting">WHERE (select WHERE) AND (view WHERE)
</pre><p>
      The <code class="literal">MERGE</code> algorithm requires a one-to
      relationship between the rows in the view and the rows in the
      underlying table. If this relationship does not hold, a temporary
      table must be used instead. Lack of a one-to-one relationship
      occurs if the view contains any of a number of constructs:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          Aggregate functions (<code class="literal">SUM()</code>,
          <code class="literal">MIN()</code>, <code class="literal">MAX()</code>,
          <code class="literal">COUNT()</code>, and so forth)
        </p></li><li><p>
          <code class="literal">DISTINCT</code>
        </p></li><li><p>
          <code class="literal">GROUP BY</code>
        </p></li><li><p>
          <code class="literal">HAVING</code>
        </p></li><li><p>
          <code class="literal">UNION</code> or <code class="literal">UNION ALL</code>
        </p></li><li><p>
          Refers only to literal values (in this case, there is no
          underlying table)
        </p></li></ul></div><p>
      Some views are updatable. That is, you can use them in statements
      such as <code class="literal">UPDATE</code>, <code class="literal">DELETE</code>, or
      <code class="literal">INSERT</code> to update the contents of the underlying
      table. For a view to be updatable, there must be a one-to
      relationship between the rows in the view and the rows in the
      underlying table. There are also certain other constructs that
      make a view non-updatable. To be more specific, a view is not
      updatable if it contains any of the following:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          Aggregate functions (<code class="literal">SUM()</code>,
          <code class="literal">MIN()</code>, <code class="literal">MAX()</code>,
          <code class="literal">COUNT()</code>, and so forth)
        </p></li><li><p>
          <code class="literal">DISTINCT</code>
        </p></li><li><p>
          <code class="literal">GROUP BY</code>
        </p></li><li><p>
          <code class="literal">HAVING</code>
        </p></li><li><p>
          <code class="literal">UNION</code> or <code class="literal">UNION ALL</code>
        </p></li><li><p>
          Subquery in the select list
        </p></li><li><p>
          Join
        </p></li><li><p>
          Non-updatable view in the <code class="literal">FROM</code> clause
        </p></li><li><p>
          A subquery in the <code class="literal">WHERE</code> clause that refers
          to a table in the <code class="literal">FROM</code> clause
        </p></li><li><p>
          Refers only to literal values (in this case, there is no
          underlying table to update)
        </p></li><li><p>
          <code class="literal">ALGORITHM = TEMPTABLE</code> (use of a temporary
          table always makes a view non-updatable)
        </p></li></ul></div><p>
      With respect to insertability (being updatable with
      <code class="literal">INSERT</code> statements), an updatable view is
      insertable if it also satisfies these additional requirements for
      the view columns:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          There must be no duplicate view column names.
        </p></li><li><p>
          The view must contain all columns in the base table that do
          not have a default value.
        </p></li><li><p>
          The view columns must be simple column references and not
          derived columns. A derived column is one that is not a simple
          column reference but is derived from an expression. These are
          examples of derived columns:
        </p><pre class="programlisting">3.14159
col1 + 3
UPPER(col2)
col3 / col4
(<em class="replaceable"><code>subquery</code></em>)
</pre></li></ul></div><p>
      A view that has a mix of simple column references and derived
      columns is not insertable, but it can be updatable if you update
      only those columns that are not derived. Consider this view:
    </p><pre class="programlisting">CREATE VIEW v AS SELECT col1, 1 AS col2 FROM t;
</pre><p>
      This view is not insertable because <code class="literal">col2</code> is
      derived from an expression. But it is updatable if the update does
      not try to update <code class="literal">col2</code>. This update is
      allowable:
    </p><pre class="programlisting">UPDATE v SET col1 = 0;
</pre><p>
      This update is not allowable because it attempts to update a
      derived column:
    </p><pre class="programlisting">UPDATE v SET col2 = 0;
</pre><p>
      It is sometimes possible for a multiple-table view to be
      updatable, assuming that it can be processed with the
      <code class="literal">MERGE</code> algorithm. For this to work, the view
      must use an inner join (not an outer join or a
      <code class="literal">UNION</code>). Also, only a single table in the view
      definition can be updated, so the <code class="literal">SET</code> clause
      must name only columns from one of the tables in the view. Views
      that use <code class="literal">UNION ALL</code> are disallowed even though
      they might be theoretically updatable, because the implementation
      uses temporary tables to process them.
    </p><p>
      For a multiple-table updatable view, <code class="literal">INSERT</code> can
      work if it inserts into a single table. <code class="literal">DELETE</code>
      is not supported.
    </p><p>
      The <code class="literal">WITH CHECK OPTION</code> clause can be given for
      an updatable view to prevent inserts or updates to rows except
      those for which the <code class="literal">WHERE</code> clause in the
      <em class="replaceable"><code>select_statement</code></em> is true.
    </p><p>
      In a <code class="literal">WITH CHECK OPTION</code> clause for an updatable
      view, the <code class="literal">LOCAL</code> and <code class="literal">CASCADED</code>
      keywords determine the scope of check testing when the view is
      defined in terms of another view. <code class="literal">LOCAL</code> keyword
      restricts the <code class="literal">CHECK OPTION</code> only to the view
      being defined. <code class="literal">CASCADED</code> causes the checks for
      underlying views to be evaluated as well. When neither keyword is
      given, the default is <code class="literal">CASCADED</code>. Consider the
      definitions for the following table and set of views:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE t1 (a INT);</code></strong>
mysql&gt; <strong class="userinput"><code>CREATE VIEW v1 AS SELECT * FROM t1 WHERE a &lt; 2</code></strong>
    -&gt; <strong class="userinput"><code>WITH CHECK OPTION;</code></strong>
mysql&gt; <strong class="userinput"><code>CREATE VIEW v2 AS SELECT * FROM v1 WHERE a &gt; 0</code></strong>
    -&gt; <strong class="userinput"><code>WITH LOCAL CHECK OPTION;</code></strong>
mysql&gt; <strong class="userinput"><code>CREATE VIEW v3 AS SELECT * FROM v1 WHERE a &gt; 0</code></strong>
    -&gt; <strong class="userinput"><code>WITH CASCADED CHECK OPTION;</code></strong>
</pre><p>
      Here the <code class="literal">v2</code> and <code class="literal">v3</code> views are
      defined in terms of another view, <code class="literal">v1</code>.
      <code class="literal">v2</code> has a <code class="literal">LOCAL</code> check option,
      so inserts are tested only against the <code class="literal">v2</code>
      check. <code class="literal">v3</code> has a <code class="literal">CASCADED</code>
      check option, so inserts are tested not only against its own
      check, but against those of underlying views. The following
      statements illustrate these differences:
    </p><pre class="programlisting">ql&gt; INSERT INTO v2 VALUES (2);
Query OK, 1 row affected (0.00 sec)
mysql&gt; <strong class="userinput"><code>INSERT INTO v3 VALUES (2);</code></strong>
ERROR 1369 (HY000): CHECK OPTION failed 'test.v3'
</pre><p>
      The updatability of views may be affected by the value of the
      <code class="literal">updatable_views_with_limit</code> system variable. See
      <a href="database-administration.html#server-system-variables" title="5.3.3. Server System Variables">Section 5.3.3, “Server System Variables”</a>.
    </p><p>
      The <code class="literal">CREATE VIEW</code> statement was added in MySQL
      5.0.1. The <code class="literal">WITH CHECK OPTION</code> clause was
      implemented in MySQL 5.0.2.
    </p><p>
      <code class="literal">INFORMATION_SCHEMA</code> contains a
      <code class="literal">VIEWS</code> table from which information about view
      objects can be obtained. See <a href="information-schema.html#views-table" title="20.1.15. The INFORMATION_SCHEMA VIEWS Table">Section 20.1.15, “The <code class="literal">INFORMATION_SCHEMA VIEWS</code> Table”</a>.
    </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="drop-view"></a>19.3. <code class="literal">DROP VIEW</code> Syntax</h2></div></div></div><a class="indexterm" name="id3055767"></a><pre class="programlisting">DROP VIEW [IF EXISTS]
    <em class="replaceable"><code>view_name</code></em> [, <em class="replaceable"><code>view_name</code></em>] ...
    [RESTRICT | CASCADE]
</pre><p>
      <code class="literal">DROP VIEW</code> removes one or more views. You must
      have the <code class="literal">DROP</code> privilege for each view.
    </p><p>
      You can use the keywords <code class="literal">IF EXISTS</code> to prevent
      an error from occurring for views that don't exist. When this
      clause is given, a <code class="literal">NOTE</code> is generated for each
      non-existent view. See <a href="sql-syntax.html#show-warnings" title="13.5.4.22. SHOW WARNINGS Syntax">Section 13.5.4.22, “<code class="literal">SHOW WARNINGS</code> Syntax”</a>.
    </p><p>
      <code class="literal">RESTRICT</code> and <code class="literal">CASCADE</code>, if
      given, are parsed and ignored.
    </p><p>
      This statement was added in MySQL 5.0.1.
    </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="show-create-view"></a>19.4. <code class="literal">SHOW CREATE VIEW</code> Syntax</h2></div></div></div><a class="indexterm" name="id3055875"></a><pre class="programlisting">SHOW CREATE VIEW <em class="replaceable"><code>view_name</code></em>
</pre><p>
      This statement shows a <code class="literal">CREATE VIEW</code> statement
      that creates the given view.
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SHOW CREATE VIEW v;</code></strong>
+------+----------------------------------------------------+
| View | Create View                                        |
+------+----------------------------------------------------+
| v    | CREATE VIEW `test`.`v` AS select 1 AS `a`,2 AS `b` |
+------+----------------------------------------------------+
</pre><p>
      This statement was added in MySQL 5.0.1.
    </p><p>
      Previous to MySQL 5.0.11, the output columns from this statement
      were shown as <code class="literal">Table</code> and <code class="literal">Create
      Table</code>.
    </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="triggers.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="information-schema.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 18. Triggers </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 20. The <code class="literal">INFORMATION_SCHEMA</code> Information Database</td></tr></table></div></body></html>