Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-testing > by-pkgid > b1e2421f2416edfc24c5845fbc1c5a2e > files > 98

mysql-doc-5.0.51a-8mdv2008.1.x86_64.rpm

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Chapter 8. Language Structure</title><link rel="stylesheet" href="mysql-html.css" type="text/css"><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="client-utility-programs.html" title="Chapter 7. Client and Utility Programs"><link rel="next" href="internationalization-localization.html" title="Chapter 9. Internationalization and Localization"></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 8. Language Structure</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="client-utility-programs.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="internationalization-localization.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="language-structure"></a>Chapter 8. Language Structure</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="language-structure.html#literals">8.1. Literal Values</a></span></dt><dd><dl><dt><span class="section"><a href="language-structure.html#string-syntax">8.1.1. Strings</a></span></dt><dt><span class="section"><a href="language-structure.html#number-syntax">8.1.2. Numbers</a></span></dt><dt><span class="section"><a href="language-structure.html#hexadecimal-values">8.1.3. Hexadecimal Values</a></span></dt><dt><span class="section"><a href="language-structure.html#boolean-values">8.1.4. Boolean Values</a></span></dt><dt><span class="section"><a href="language-structure.html#bit-field-values">8.1.5. Bit-Field Values</a></span></dt><dt><span class="section"><a href="language-structure.html#null-values">8.1.6. <code class="literal">NULL</code> Values</a></span></dt></dl></dd><dt><span class="section"><a href="language-structure.html#identifiers">8.2. Schema Object Names</a></span></dt><dd><dl><dt><span class="section"><a href="language-structure.html#identifier-qualifiers">8.2.1. Identifier Qualifiers</a></span></dt><dt><span class="section"><a href="language-structure.html#identifier-case-sensitivity">8.2.2. Identifier Case Sensitivity</a></span></dt><dt><span class="section"><a href="language-structure.html#function-resolution">8.2.3. Function Name Parsing and Resolution</a></span></dt></dl></dd><dt><span class="section"><a href="language-structure.html#reserved-words">8.3. Reserved Words</a></span></dt><dt><span class="section"><a href="language-structure.html#user-variables">8.4. User-Defined Variables</a></span></dt><dt><span class="section"><a href="language-structure.html#comments">8.5. Comment Syntax</a></span></dt></dl></div><p>
    This chapter discusses the rules for writing the following elements
    of SQL statements when using MySQL:
  </p><div class="itemizedlist"><ul type="disc"><li><p>
        Literal values such as strings and numbers
      </p></li><li><p>
        Identifiers such as database, table, and column names
      </p></li><li><p>
        Reserved words
      </p></li><li><p>
        User-defined and system variables
      </p></li><li><p>
        Comments
      </p></li></ul></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="literals"></a>8.1. Literal Values</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="language-structure.html#string-syntax">8.1.1. Strings</a></span></dt><dt><span class="section"><a href="language-structure.html#number-syntax">8.1.2. Numbers</a></span></dt><dt><span class="section"><a href="language-structure.html#hexadecimal-values">8.1.3. Hexadecimal Values</a></span></dt><dt><span class="section"><a href="language-structure.html#boolean-values">8.1.4. Boolean Values</a></span></dt><dt><span class="section"><a href="language-structure.html#bit-field-values">8.1.5. Bit-Field Values</a></span></dt><dt><span class="section"><a href="language-structure.html#null-values">8.1.6. <code class="literal">NULL</code> Values</a></span></dt></dl></div><a class="indexterm" name="id2734384"></a><a class="indexterm" name="id2734397"></a><a class="indexterm" name="id2734409"></a><a class="indexterm" name="id2734418"></a><a class="indexterm" name="id2734427"></a><p>
      This section describes how to write literal values in MySQL. These
      include strings, numbers, hexadecimal values, boolean values, and
      <code class="literal">NULL</code>. The section also covers the various
      nuances and “<span class="quote">gotchas</span>” that you may run into when
      dealing with these basic types in MySQL.
    </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="string-syntax"></a>8.1.1. Strings</h3></div></div></div><p>
        A string is a sequence of bytes or characters, enclosed within
        either single quote (“<span class="quote"><code class="literal">'</code></span>”) or
        double quote (“<span class="quote"><code class="literal">"</code></span>”) characters.
        Examples:
      </p><pre class="programlisting">'a string'
"another string"
</pre><p>
        If the <code class="literal">ANSI_QUOTES</code> SQL mode is enabled,
        string literals can be quoted only within single quotes because
        a string quoted within double quotes is interpreted as an
        identifier.
      </p><p>
        A <em class="firstterm">binary string</em> is a string of bytes that
        has no character set or collation. A <em class="firstterm">non-binary
        string</em> is a string of characters that has a
        character set and collation. For both types of strings,
        comparisons are based on the numeric values of the string unit.
        For binary strings, the unit is the byte. For non-binary strings
        the unit is the character and some character sets allow
        multi-byte characters. Character value ordering is a function of
        the string collation.
      </p><p>
        String literals may have an optional character set introducer
        and <code class="literal">COLLATE</code> clause:
      </p><a class="indexterm" name="id2734534"></a><a class="indexterm" name="id2734546"></a><pre class="programlisting">[_<em class="replaceable"><code>charset_name</code></em>]'<em class="replaceable"><code>string</code></em>' [COLLATE <em class="replaceable"><code>collation_name</code></em>]
</pre><p>
        Examples:
      </p><pre class="programlisting">SELECT _latin1'<em class="replaceable"><code>string</code></em>';
SELECT _latin1'<em class="replaceable"><code>string</code></em>' COLLATE latin1_danish_ci;
</pre><p>
        You can use
        <code class="literal">N'<em class="replaceable"><code>literal</code></em>'</code> (or
        <code class="literal">n'<em class="replaceable"><code>literal</code></em>'</code>) to
        create a string in the national character set. These statements
        are equivalent:
      </p><pre class="programlisting">SELECT N'some text';
SELECT n'some text';
SELECT _utf8'some text';
</pre><p>
        For more information about these forms of string syntax, see
        <a href="internationalization-localization.html#charset-literal" title="9.1.3.5. Character String Literal Character Set and Collation">Section 9.1.3.5, “Character String Literal Character Set and Collation”</a>, and
        <a href="internationalization-localization.html#charset-national" title="9.1.3.6. National Character Set">Section 9.1.3.6, “National Character Set”</a>.
      </p><p>
        Within a string, certain sequences have special meaning. Each of
        these sequences begins with a backslash
        (“<span class="quote"><code class="literal">\</code></span>”), known as the
        <span class="emphasis"><em>escape character</em></span>. MySQL recognizes the
        following escape sequences:
      </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><tbody><tr><td><code class="literal">\0</code>

                <a class="indexterm" name="id2734678"></a>

                <a class="indexterm" name="id2734688"></a></td><td>An ASCII 0 (<code class="literal">NUL</code>) character.</td></tr><tr><td><code class="literal">\'</code>

                <a class="indexterm" name="id2734715"></a>

                <a class="indexterm" name="id2734724"></a></td><td>A single quote (“<span class="quote"><code class="literal">'</code></span>”) character.</td></tr><tr><td><code class="literal">\"</code>

                <a class="indexterm" name="id2734754"></a>

                <a class="indexterm" name="id2734763"></a></td><td>A double quote (“<span class="quote"><code class="literal">"</code></span>”) character.</td></tr><tr><td><code class="literal">\b</code>

                <a class="indexterm" name="id2734792"></a>

                <a class="indexterm" name="id2734801"></a></td><td>A backspace character.</td></tr><tr><td><code class="literal">\n</code>

                <a class="indexterm" name="id2734824"></a>

                <a class="indexterm" name="id2734833"></a>

                <a class="indexterm" name="id2734842"></a>

                <a class="indexterm" name="id2734851"></a></td><td>A newline (linefeed) character.</td></tr><tr><td><code class="literal">\r</code>

                <a class="indexterm" name="id2734874"></a>

                <a class="indexterm" name="id2734883"></a>

                <a class="indexterm" name="id2734892"></a></td><td>A carriage return character.</td></tr><tr><td><code class="literal">\t</code>

                <a class="indexterm" name="id2734915"></a>

                <a class="indexterm" name="id2734924"></a></td><td>A tab character.</td></tr><tr><td><code class="literal">\Z</code>

                <a class="indexterm" name="id2734946"></a>

                <a class="indexterm" name="id2734955"></a></td><td>ASCII 26 (Control-Z). See note following the table.</td></tr><tr><td><code class="literal">\\</code>

                <a class="indexterm" name="id2734978"></a>

                <a class="indexterm" name="id2734987"></a></td><td>A backslash (“<span class="quote"><code class="literal">\</code></span>”) character.</td></tr><tr><td><code class="literal">\%</code>

                <a class="indexterm" name="id2735016"></a>

                <a class="indexterm" name="id2735025"></a></td><td>A “<span class="quote"><code class="literal">%</code></span>” character. See note following the
                table.</td></tr><tr><td><code class="literal">\_</code>

                <a class="indexterm" name="id2735055"></a>

                <a class="indexterm" name="id2735065"></a></td><td>A “<span class="quote"><code class="literal">_</code></span>” character. See note following the
                table.</td></tr></tbody></table></div><p>
        For all other escape sequences, backslash is ignored. That is,
        the escaped character is interpreted as if it was not escaped.
        For example, “<span class="quote"><code class="literal">\x</code></span>” is just
        “<span class="quote"><code class="literal">x</code></span>”.
      </p><p>
        These sequences are case sensitive. For example,
        “<span class="quote"><code class="literal">\b</code></span>” is interpreted as a
        backspace, but “<span class="quote"><code class="literal">\B</code></span>” is
        interpreted as “<span class="quote"><code class="literal">B</code></span>”.
      </p><p>
        The ASCII 26 character can be encoded as
        “<span class="quote"><code class="literal">\Z</code></span>” to enable you to work
        around the problem that ASCII 26 stands for END-OF-FILE on
        Windows. ASCII 26 within a file causes problems if you try to
        use <code class="literal">mysql <em class="replaceable"><code>db_name</code></em> &lt;
        <em class="replaceable"><code>file_name</code></em></code>.
      </p><p>
        Escape processing is done according to the character set
        indicated by the <code class="literal">character_set_connection</code>
        system variable. This is true even for strings that are preceded
        by an introducer that indicates a different character set, as
        discussed in <a href="internationalization-localization.html#charset-literal" title="9.1.3.5. Character String Literal Character Set and Collation">Section 9.1.3.5, “Character String Literal Character Set and Collation”</a>.
      </p><p>
        The “<span class="quote"><code class="literal">\%</code></span>” and
        “<span class="quote"><code class="literal">\_</code></span>” sequences are used to
        search for literal instances of
        “<span class="quote"><code class="literal">%</code></span>” and
        “<span class="quote"><code class="literal">_</code></span>” in pattern-matching contexts
        where they would otherwise be interpreted as wildcard
        characters. See the description of the <code class="literal">LIKE</code>
        operator in <a href="functions.html#string-comparison-functions" title="11.4.1. String Comparison Functions">Section 11.4.1, “String Comparison Functions”</a>. If
        you use “<span class="quote"><code class="literal">\%</code></span>” or
        “<span class="quote"><code class="literal">\_</code></span>” in non-pattern-matching
        contexts, they evaluate to the strings
        “<span class="quote"><code class="literal">\%</code></span>” and
        “<span class="quote"><code class="literal">\_</code></span>”, not to
        “<span class="quote"><code class="literal">%</code></span>” and
        “<span class="quote"><code class="literal">_</code></span>”.
      </p><a class="indexterm" name="id2735268"></a><p>
        There are several ways to include quote characters within a
        string:
      </p><div class="itemizedlist"><ul type="disc"><li><p>
            A “<span class="quote"><code class="literal">'</code></span>” inside a string quoted
            with “<span class="quote"><code class="literal">'</code></span>” may be written as
            “<span class="quote"><code class="literal">''</code></span>”.
          </p></li><li><p>
            A “<span class="quote"><code class="literal">"</code></span>” inside a string quoted
            with “<span class="quote"><code class="literal">"</code></span>” may be written as
            “<span class="quote"><code class="literal">""</code></span>”.
          </p></li><li><p>
            Precede the quote character by an escape character
            (“<span class="quote"><code class="literal">\</code></span>”).
          </p></li><li><p>
            A “<span class="quote"><code class="literal">'</code></span>” inside a string quoted
            with “<span class="quote"><code class="literal">"</code></span>” needs no special
            treatment and need not be doubled or escaped. In the same
            way, “<span class="quote"><code class="literal">"</code></span>” inside a string
            quoted with “<span class="quote"><code class="literal">'</code></span>” needs no
            special treatment.
          </p></li></ul></div><p>
        The following <code class="literal">SELECT</code> statements demonstrate
        how quoting and escaping work:
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT 'hello', '"hello"', '""hello""', 'hel''lo', '\'hello';</code></strong>
+-------+---------+-----------+--------+--------+
| hello | "hello" | ""hello"" | hel'lo | 'hello |
+-------+---------+-----------+--------+--------+

mysql&gt; <strong class="userinput"><code>SELECT "hello", "'hello'", "''hello''", "hel""lo", "\"hello";</code></strong>
+-------+---------+-----------+--------+--------+
| hello | 'hello' | ''hello'' | hel"lo | "hello |
+-------+---------+-----------+--------+--------+

mysql&gt; <strong class="userinput"><code>SELECT 'This\nIs\nFour\nLines';</code></strong>
+--------------------+
| This
Is
Four
Lines |
+--------------------+

mysql&gt; <strong class="userinput"><code>SELECT 'disappearing\ backslash';</code></strong>
+------------------------+
| disappearing backslash |
+------------------------+
</pre><a class="indexterm" name="id2735454"></a><p>
        If you want to insert binary data into a string column (such as
        a <code class="literal">BLOB</code> column), the following characters must
        be represented by escape sequences:
      </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><tbody><tr><td><code class="literal">NUL</code></td><td><code class="literal">NUL</code> byte (ASCII 0). Represent this character by
                “<span class="quote"><code class="literal">\0</code></span>” (a backslash
                followed by an ASCII “<span class="quote"><code class="literal">0</code></span>”
                character).</td></tr><tr><td><code class="literal">\</code></td><td>Backslash (ASCII 92). Represent this character by
                “<span class="quote"><code class="literal">\\</code></span>”.</td></tr><tr><td><code class="literal">'</code></td><td>Single quote (ASCII 39). Represent this character by
                “<span class="quote"><code class="literal">\'</code></span>”.</td></tr><tr><td><code class="literal">"</code></td><td>Double quote (ASCII 34). Represent this character by
                “<span class="quote"><code class="literal">\"</code></span>”.</td></tr></tbody></table></div><a class="indexterm" name="id2735591"></a><a class="indexterm" name="id2735600"></a><a class="indexterm" name="id2735612"></a><a class="indexterm" name="id2735621"></a><p>
        When writing application programs, any string that might contain
        any of these special characters must be properly escaped before
        the string is used as a data value in an SQL statement that is
        sent to the MySQL server. You can do this in two ways:
      </p><div class="itemizedlist"><ul type="disc"><li><p>
            Process the string with a function that escapes the special
            characters. In a C program, you can use the
            <a href="apis.html#mysql-real-escape-string" title="23.2.3.53. mysql_real_escape_string()"><code class="literal">mysql_real_escape_string()</code></a>
            C API function to escape characters. See
            <a href="apis.html#mysql-real-escape-string" title="23.2.3.53. mysql_real_escape_string()">Section 23.2.3.53, “<code class="literal">mysql_real_escape_string()</code>”</a>. The Perl DBI
            interface provides a <code class="literal">quote</code> method to
            convert special characters to the proper escape sequences.
            See <a href="apis.html#perl" title="23.4. MySQL Perl API">Section 23.4, “MySQL Perl API”</a>. Other language interfaces may
            provide a similar capability.
          </p></li><li><p>
            As an alternative to explicitly escaping special characters,
            many MySQL APIs provide a placeholder capability that
            enables you to insert special markers into a statement
            string, and then bind data values to them when you issue the
            statement. In this case, the API takes care of escaping
            special characters in the values for you.
          </p></li></ul></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="number-syntax"></a>8.1.2. Numbers</h3></div></div></div><a class="indexterm" name="id2735701"></a><a class="indexterm" name="id2735710"></a><a class="indexterm" name="id2735722"></a><a class="indexterm" name="id2735731"></a><a class="indexterm" name="id2735740"></a><a class="indexterm" name="id2735749"></a><a class="indexterm" name="id2735758"></a><p>
        Integers are represented as a sequence of digits. Floats use
        “<span class="quote"><code class="literal">.</code></span>” as a decimal separator.
        Either type of number may be preceded by
        “<span class="quote"><code class="literal">-</code></span>” or
        “<span class="quote"><code class="literal">+</code></span>” to indicate a negative or
        positive value, respectively
      </p><p>
        Examples of valid integers:
      </p><pre class="programlisting">1221
0
-32
</pre><p>
        Examples of valid floating-point numbers:
      </p><pre class="programlisting">294.42
-32032.6809e+10
148.00
</pre><p>
        An integer may be used in a floating-point context; it is
        interpreted as the equivalent floating-point number.
      </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="hexadecimal-values"></a>8.1.3. Hexadecimal Values</h3></div></div></div><a class="indexterm" name="id2735830"></a><p>
        MySQL supports hexadecimal values. In numeric contexts, these
        act like integers (64-bit precision). In string contexts, these
        act like binary strings, where each pair of hex digits is
        converted to a character:
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT x'4D7953514C';</code></strong>
        -&gt; 'MySQL'
mysql&gt; <strong class="userinput"><code>SELECT 0x0a+0;</code></strong>
        -&gt; 10
mysql&gt; <strong class="userinput"><code>SELECT 0x5061756c;</code></strong>
        -&gt; 'Paul'
</pre><p>
        The default type of a hexadecimal value is a string. If you want
        to ensure that the value is treated as a number, you can use
        <code class="literal">CAST(... AS UNSIGNED)</code>:
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT 0x41, CAST(0x41 AS UNSIGNED);</code></strong>
        -&gt; 'A', 65
</pre><p>
        The <code class="literal">x'<em class="replaceable"><code>hexstring</code></em>'</code>
        syntax is based on standard SQL. The <code class="literal">0x</code>
        syntax is based on ODBC. Hexadecimal strings are often used by
        ODBC to supply values for <code class="literal">BLOB</code> columns.
      </p><p>
        You can convert a string or a number to a string in hexadecimal
        format with the <a href="functions.html#function_hex"><code class="literal">HEX()</code></a> function:
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT HEX('cat');</code></strong>
        -&gt; '636174'
mysql&gt; <strong class="userinput"><code>SELECT 0x636174;</code></strong>
        -&gt; 'cat'
</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="boolean-values"></a>8.1.4. Boolean Values</h3></div></div></div><a class="indexterm" name="id2735966"></a><a class="indexterm" name="id2735975"></a><p>
        The constants <code class="literal">TRUE</code> and
        <code class="literal">FALSE</code> evaluate to <code class="literal">1</code> and
        <code class="literal">0</code>, respectively. The constant names can be
        written in any lettercase.
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT TRUE, true, FALSE, false;</code></strong>
        -&gt; 1, 1, 0, 0
</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="bit-field-values"></a>8.1.5. Bit-Field Values</h3></div></div></div><p>
        Beginning with MySQL 5.0.3, bit-field values can be written
        using <code class="literal">b'<em class="replaceable"><code>value</code></em>'</code> or
        <code class="literal">0b<em class="replaceable"><code>value</code></em></code> notation.
        <em class="replaceable"><code>value</code></em> is a binary value written using
        zeros and ones.
      </p><p>
        Bit-field notation is convenient for specifying values to be
        assigned to <code class="literal">BIT</code> columns:
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE t (b BIT(8));</code></strong>
mysql&gt; <strong class="userinput"><code>INSERT INTO t SET b = b'11111111';</code></strong>
mysql&gt; <strong class="userinput"><code>INSERT INTO t SET b = b'1010';</code></strong>
mysql&gt; <strong class="userinput"><code>INSERT INTO t SET b = b'0101';</code></strong>
</pre><p>
        Bit values are returned as binary values. To display them in
        printable form, add 0 or use a conversion function such as
        <a href="functions.html#function_bin"><code class="literal">BIN()</code></a>. High-order 0 bits are not
        displayed in the converted value.
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT b+0, BIN(b+0), OCT(b+0), HEX(b+0) FROM t;</code></strong>
+------+----------+----------+----------+
| b+0  | BIN(b+0) | OCT(b+0) | HEX(b+0) |
+------+----------+----------+----------+
|  255 | 11111111 | 377      | FF       | 
|   10 | 1010     | 12       | A        | 
|    5 | 101      | 5        | 5        | 
+------+----------+----------+----------+
</pre><p>
        Bit values assigned to user variables are treated as binary
        strings. To assign a bit value as a number to a user variable,
        use <a href="functions.html#function_cast"><code class="literal">CAST()</code></a> or
        <code class="literal">+0</code>:
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SET @v1 = b'1000001';</code></strong>
mysql&gt; <strong class="userinput"><code>SET @v2 = CAST(b'1000001' AS UNSIGNED), @v3 = b'1000001'+0;</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT @v1, @v2, @v3;</code></strong>
+------+------+------+
| @v1  | @v2  | @v3  |
+------+------+------+
| A    |   65 |   65 | 
+------+------+------+
</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="null-values"></a>8.1.6. <code class="literal">NULL</code> Values</h3></div></div></div><a class="indexterm" name="id2736217"></a><p>
        The <code class="literal">NULL</code> value means “<span class="quote">no data.</span>”
        <code class="literal">NULL</code> can be written in any lettercase. A
        synonym is <code class="literal">\N</code> (case sensitive).
      </p><p>
        For text file import or export operations performed with
        <code class="literal">LOAD DATA INFILE</code> or <code class="literal">SELECT ... INTO
        OUTFILE</code>, <code class="literal">NULL</code> is represented by the
        <code class="literal">\N</code> sequence. See <a href="sql-syntax.html#load-data" title="12.2.5. LOAD DATA INFILE Syntax">Section 12.2.5, “<code class="literal">LOAD DATA INFILE</code> Syntax”</a>.
      </p><p>
        Be aware that the <code class="literal">NULL</code> value is different
        from values such as <code class="literal">0</code> for numeric types or
        the empty string for string types. For more information, see
        <a href="error-handling.html#problems-with-null" title="B.1.5.3. Problems with NULL Values">Section B.1.5.3, “Problems with <code class="literal">NULL</code> Values”</a>.
      </p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="identifiers"></a>8.2. Schema Object Names</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="language-structure.html#identifier-qualifiers">8.2.1. Identifier Qualifiers</a></span></dt><dt><span class="section"><a href="language-structure.html#identifier-case-sensitivity">8.2.2. Identifier Case Sensitivity</a></span></dt><dt><span class="section"><a href="language-structure.html#function-resolution">8.2.3. Function Name Parsing and Resolution</a></span></dt></dl></div><a class="indexterm" name="id2736318"></a><a class="indexterm" name="id2736327"></a><a class="indexterm" name="id2736336"></a><a class="indexterm" name="id2736345"></a><a class="indexterm" name="id2736358"></a><a class="indexterm" name="id2736370"></a><a class="indexterm" name="id2736382"></a><a class="indexterm" name="id2736395"></a><p>
      Database, table, index, column, and alias names are identifiers.
      This section describes the allowable syntax for identifiers in
      MySQL. <a href="language-structure.html#identifier-case-sensitivity" title="8.2.2. Identifier Case Sensitivity">Section 8.2.2, “Identifier Case Sensitivity”</a>, describes
      which types of identifiers are case sensitive and under what
      conditions.
    </p><a class="indexterm" name="id2736420"></a><a class="indexterm" name="id2736432"></a><a class="indexterm" name="id2736441"></a><a class="indexterm" name="id2736450"></a><p>
      An identifier may be quoted or unquoted. If an identifier contains
      special characters or is a reserved word, you
      <span class="emphasis"><em>must</em></span> quote it whenever you refer to it.
      Special characters are those other than the set of alphanumeric
      characters from the current character set,
      “<span class="quote"><code class="literal">_</code></span>”, and
      “<span class="quote"><code class="literal">$</code></span>”. Reserved words are listed at
      <a href="language-structure.html#reserved-words" title="8.3. Reserved Words">Section 8.3, “Reserved Words”</a>. (Exception: A reserved word that
      follows a period in a qualified name must be an identifier, so it
      need not be quoted.)
    </p><p>
      The identifier quote character is the backtick
      (“<span class="quote"><code class="literal">`</code></span>”):
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT * FROM `select` WHERE `select`.id &gt; 100;</code></strong>
</pre><p>
      If the <code class="literal">ANSI_QUOTES</code> SQL mode is enabled, it is
      also allowable to quote identifiers within double quotes:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE "test" (col INT);</code></strong>
ERROR 1064: You have an error in your SQL syntax...
mysql&gt; <strong class="userinput"><code>SET sql_mode='ANSI_QUOTES';</code></strong>
mysql&gt; <strong class="userinput"><code>CREATE TABLE "test" (col INT);</code></strong>
Query OK, 0 rows affected (0.00 sec)
</pre><p>
      The <code class="literal">ANSI_QUOTES</code> mode causes the server to
      interpret double-quoted strings as identifiers. Consequently, when
      this mode is enabled, string literals must be enclosed within
      single quotes. They cannot be enclosed within double quotes.
    </p><p>
      The server SQL mode is controlled as described in
      <a href="server-administration.html#server-sql-mode" title="5.2.6. SQL Modes">Section 5.2.6, “SQL Modes”</a>.
    </p><p>
      Identifier quote characters can be included within an identifier
      if you quote the identifier. If the character to be included
      within the identifier is the same as that used to quote the
      identifier itself, then you need to double the character. The
      following statement creates a table named <code class="literal">a`b</code>
      that contains a column named <code class="literal">c"d</code>:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE `a``b` (`c"d` INT);</code></strong>
</pre><p>
      Identifiers may begin with a digit but unless quoted may not
      consist solely of digits.
    </p><p>
      It is recommended that you do not use names of the form
      <code class="literal"><em class="replaceable"><code>M</code></em>e</code> or
      <code class="literal"><em class="replaceable"><code>M</code></em>e<em class="replaceable"><code>N</code></em></code>,
      where <em class="replaceable"><code>M</code></em> and
      <em class="replaceable"><code>N</code></em> are integers. For example, avoid
      using <code class="literal">1e</code> or <code class="literal">2e2</code> as
      identifiers, because an expression such as <code class="literal">1e+3</code>
      is ambiguous. Depending on context, it might be interpreted as the
      expression <code class="literal">1e + 3</code> or as the number
      <code class="literal">1e+3</code>.
    </p><p>
      Be careful when using <a href="functions.html#function_md5"><code class="literal">MD5()</code></a> to
      produce table names because it can produce names in illegal or
      ambiguous formats such as those just described.
    </p><p>
      A user variable cannot be used directly in an SQL statement as an
      identifier or as part of an identifier. See
      <a href="language-structure.html#user-variables" title="8.4. User-Defined Variables">Section 8.4, “User-Defined Variables”</a>, for more information and
      examples of workarounds.
    </p><p>
      There are some restrictions on the characters that may appear in
      identifiers:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          No identifier can contain ASCII 0 (<code class="literal">0x00</code>) or
          a byte with a value of 255.
        </p></li><li><p>
          Database, table, and column names should not end with space
          characters.
        </p></li><li><p>
          Database and table names cannot contain
          “<span class="quote"><code class="literal">/</code></span>”,
          “<span class="quote"><code class="literal">\</code></span>”,
          “<span class="quote"><code class="literal">.</code></span>”, or characters that are
          not allowed in filenames.
        </p></li></ul></div><p>
      The following table describes the maximum length for each type of
      identifier.
    </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><tbody><tr><td><span class="bold"><strong>Identifier</strong></span></td><td><span class="bold"><strong>Maximum Length (characters)</strong></span></td></tr><tr><td>Database</td><td>64</td></tr><tr><td>Table</td><td>64</td></tr><tr><td>Column</td><td>64</td></tr><tr><td>Index</td><td>64</td></tr><tr><td>Stored Function or Procedure</td><td>64</td></tr><tr><td>Trigger</td><td>64</td></tr><tr><td>View</td><td>64</td></tr><tr><td>Alias</td><td>255</td></tr></tbody></table></div><p>
      Identifiers are stored using Unicode (UTF-8). This applies to
      identifiers in table definitions that are stored in
      <code class="filename">.frm</code> files and to identifiers stored in the
      grant tables in the <code class="literal">mysql</code> database. The sizes
      of the identifier string columns in the grant tables are measured
      in characters. You can use multi-byte characters without reducing
      the number of characters allowed for values stored in these
      columns, something not true prior to MySQL 4.1.
    </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="identifier-qualifiers"></a>8.2.1. Identifier Qualifiers</h3></div></div></div><p>
        MySQL allows names that consist of a single identifier or
        multiple identifiers. The components of a multiple-part name
        must be separated by period
        (“<span class="quote"><code class="literal">.</code></span>”) characters. The initial
        parts of a multiple-part name act as qualifiers that affect the
        context within which the final identifier is interpreted.
      </p><p>
        In MySQL, you can refer to a table column using any of the
        following forms:
      </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><tbody><tr><td><span class="bold"><strong>Column Reference</strong></span></td><td><span class="bold"><strong>Meaning</strong></span></td></tr><tr><td><em class="replaceable"><code>col_name</code></em></td><td>The column <em class="replaceable"><code>col_name</code></em> from whichever table used
                in the statement contains a column of that name.</td></tr><tr><td><em class="replaceable"><code>tbl_name.col_name</code></em></td><td>The column <em class="replaceable"><code>col_name</code></em> from table
                <em class="replaceable"><code>tbl_name</code></em> of the default
                database.</td></tr><tr><td><em class="replaceable"><code>db_name.tbl_name.col_name</code></em></td><td>The column <em class="replaceable"><code>col_name</code></em> from table
                <em class="replaceable"><code>tbl_name</code></em> of the database
                <em class="replaceable"><code>db_name</code></em>.</td></tr></tbody></table></div><p>
        If any components of a multiple-part name require quoting, quote
        them individually rather than quoting the name as a whole. For
        example, write <code class="literal">`my-table`.`my-column`</code>, not
        <code class="literal">`my-table.my-column`</code>.
      </p><p>
        A reserved word that follows a period in a qualified name must
        be an identifier, so in that context it need not be quoted.
      </p><p>
        You need not specify a <em class="replaceable"><code>tbl_name</code></em> or
        <em class="replaceable"><code>db_name.tbl_name</code></em> prefix for a column
        reference in a statement unless the reference would be
        ambiguous. Suppose that tables <code class="literal">t1</code> and
        <code class="literal">t2</code> each contain a column
        <code class="literal">c</code>, and you retrieve <code class="literal">c</code> in a
        <code class="literal">SELECT</code> statement that uses both
        <code class="literal">t1</code> and <code class="literal">t2</code>. In this case,
        <code class="literal">c</code> is ambiguous because it is not unique among
        the tables used in the statement. You must qualify it with a
        table name as <code class="literal">t1.c</code> or <code class="literal">t2.c</code>
        to indicate which table you mean. Similarly, to retrieve from a
        table <code class="literal">t</code> in database <code class="literal">db1</code>
        and from a table <code class="literal">t</code> in database
        <code class="literal">db2</code> in the same statement, you must refer to
        columns in those tables as
        <code class="literal">db1.t.<em class="replaceable"><code>col_name</code></em></code> and
        <code class="literal">db2.t.<em class="replaceable"><code>col_name</code></em></code>.
      </p><a class="indexterm" name="id2737174"></a><a class="indexterm" name="id2737183"></a><p>
        The syntax
        <code class="literal"><em class="replaceable"><code>.tbl_name</code></em></code> means
        the table <em class="replaceable"><code>tbl_name</code></em> in the default
        database. This syntax is accepted for ODBC compatibility because
        some ODBC programs prefix table names with a
        “<span class="quote"><code class="literal">.</code></span>” character.
      </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="identifier-case-sensitivity"></a>8.2.2. Identifier Case Sensitivity</h3></div></div></div><a class="indexterm" name="id2737229"></a><a class="indexterm" name="id2737242"></a><a class="indexterm" name="id2737254"></a><a class="indexterm" name="id2737267"></a><a class="indexterm" name="id2737280"></a><a class="indexterm" name="id2737292"></a><a class="indexterm" name="id2737305"></a><a class="indexterm" name="id2737317"></a><p>
        In MySQL, databases correspond to directories within the data
        directory. Each table within a database corresponds to at least
        one file within the database directory (and possibly more,
        depending on the storage engine). Consequently, the case
        sensitivity of the underlying operating system plays a part in
        the case sensitivity of database and table names. This means
        database and table names are not case sensitive in Windows, and
        case sensitive in most varieties of Unix. One notable exception
        is Mac OS X, which is Unix-based but uses a default filesystem
        type (HFS+) that is not case sensitive. However, Mac OS X also
        supports UFS volumes, which are case sensitive just as on any
        Unix. See <a href="introduction.html#extensions-to-ansi" title="1.8.4. MySQL Extensions to Standard SQL">Section 1.8.4, “MySQL Extensions to Standard SQL”</a>. The
        <code class="literal">lower_case_table_names</code> system variable also
        affects how the server handles identifier case sensitivity, as
        described later in this section.
      </p><p class="mnmas"><b>MySQL Enterprise</b>
          <code class="literal">lower_case_table_names</code> is just one of the
          system variables monitored by the MySQL Enterprise Monitor.
          For information about subscribing to this service, see
          <a href="http://www.mysql.com/products/enterprise/advisors.html" target="_top">http://www.mysql.com/products/enterprise/advisors.html</a>.
        </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
          Although database and table names are not case sensitive on
          some platforms, you should not refer to a given database or
          table using different cases within the same statement. The
          following statement would not work because it refers to a
          table both as <code class="literal">my_table</code> and as
          <code class="literal">MY_TABLE</code>:
        </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT * FROM my_table WHERE MY_TABLE.col=1;</code></strong>
</pre></div><p>
        Column, index, and stored routine names are not case sensitive
        on any platform, nor are column aliases. Trigger names are case
        sensitive, which differs from standard SQL.
      </p><p>
        By default, table aliases are case sensitive on Unix, but not so
        on Windows or Mac OS X. The following statement would not work
        on Unix, because it refers to the alias both as
        <code class="literal">a</code> and as <code class="literal">A</code>:
      </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT <em class="replaceable"><code>col_name</code></em> FROM <em class="replaceable"><code>tbl_name</code></em> AS a</code></strong>
    -&gt; <strong class="userinput"><code>WHERE a.<em class="replaceable"><code>col_name</code></em> = 1 OR A.<em class="replaceable"><code>col_name</code></em> = 2;</code></strong>
</pre><p>
        However, this same statement is permitted on Windows. To avoid
        problems caused by such differences, it is best to adopt a
        consistent convention, such as always creating and referring to
        databases and tables using lowercase names. This convention is
        recommended for maximum portability and ease of use.
      </p><p>
        How table and database names are stored on disk and used in
        MySQL is affected by the
        <code class="literal">lower_case_table_names</code> system variable, which
        you can set when starting <span><strong class="command">mysqld</strong></span>.
        <code class="literal">lower_case_table_names</code> can take the values
        shown in the following table. On Unix, the default value of
        <code class="literal">lower_case_table_names</code> is 0. On Windows the
        default value is 1. On Mac OS X, the default value is 2.
      </p><div class="informaltable"><table border="1"><colgroup><col><col></colgroup><tbody><tr><td><span class="bold"><strong>Value</strong></span></td><td><span class="bold"><strong>Meaning</strong></span></td></tr><tr><td><code class="literal">0</code></td><td>Table and database names are stored on disk using the lettercase
                specified in the <code class="literal">CREATE TABLE</code> or
                <code class="literal">CREATE DATABASE</code> statement. Name
                comparisons are case sensitive. Note that if you force
                this variable to 0 with
                <code class="option">--lower-case-table-names=0</code> on a
                case-insensitive filesystem and access
                <code class="literal">MyISAM</code> tablenames using different
                lettercases, index corruption may result.</td></tr><tr><td><code class="literal">1</code></td><td>Table names are stored in lowercase on disk and name comparisons are not
                case sensitive. MySQL converts all table names to
                lowercase on storage and lookup. This behavior also
                applies to database names and table aliases.</td></tr><tr><td><code class="literal">2</code></td><td>Table and database names are stored on disk using the lettercase
                specified in the <code class="literal">CREATE TABLE</code> or
                <code class="literal">CREATE DATABASE</code> statement, but MySQL
                converts them to lowercase on lookup. Name comparisons
                are not case sensitive. This works
                <span class="emphasis"><em>only</em></span> on filesystems that are not
                case sensitive! <code class="literal">InnoDB</code> table names
                are stored in lowercase, as for
                <code class="literal">lower_case_table_names=1</code>.</td></tr></tbody></table></div><p>
        If you are using MySQL on only one platform, you do not normally
        have to change the <code class="literal">lower_case_table_names</code>
        variable from its default value. However, you may encounter
        difficulties if you want to transfer tables between platforms
        that differ in filesystem case sensitivity. For example, on
        Unix, you can have two different tables named
        <code class="literal">my_table</code> and <code class="literal">MY_TABLE</code>, but
        on Windows these two names are considered identical. To avoid
        data transfer problems arising from lettercase of database or
        table names, you have two options:
      </p><div class="itemizedlist"><ul type="disc"><li><p>
            Use <code class="literal">lower_case_table_names=1</code> on all
            systems. The main disadvantage with this is that when you
            use <code class="literal">SHOW TABLES</code> or <code class="literal">SHOW
            DATABASES</code>, you do not see the names in their
            original lettercase.
          </p></li><li><p>
            Use <code class="literal">lower_case_table_names=0</code> on Unix and
            <code class="literal">lower_case_table_names=2</code> on Windows. This
            preserves the lettercase of database and table names. The
            disadvantage of this is that you must ensure that your
            statements always refer to your database and table names
            with the correct lettercase on Windows. If you transfer your
            statements to Unix, where lettercase is significant, they do
            not work if the lettercase is incorrect.
          </p><p>
            <span class="bold"><strong>Exception</strong></span>: If you are using
            <code class="literal">InnoDB</code> tables and you are trying to avoid
            these data transfer problems, you should set
            <code class="literal">lower_case_table_names</code> to 1 on all
            platforms to force names to be converted to lowercase.
          </p></li></ul></div><p>
        If you plan to set the <code class="literal">lower_case_table_names</code>
        system variable to 1 on Unix, you must first convert your old
        database and table names to lowercase before stopping
        <span><strong class="command">mysqld</strong></span> and restarting it with the new
        variable setting.
      </p><p>
        Object names may be considered duplicates if their uppercase
        forms are equal according to a binary collation. That is true
        for names of cursors, conditions, functions, procedures,
        savepoints, and routine local variables. It is not true for
        names of columns, constraints, databases, statements prepared
        with <code class="literal">PREPARE</code>, tables, triggers, users, and
        user-defined variables.
      </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="function-resolution"></a>8.2.3. Function Name Parsing and Resolution</h3></div></div></div><a class="indexterm" name="id2737833"></a><a class="indexterm" name="id2737846"></a><p>
        MySQL 5.0 supports built-in (native) functions,
        user-defined functions (UDFs), and stored functions. This
        section describes how the server recognizes whether the name of
        a built-in function is used as a function call or as an
        identifier, and how the server determines which function to use
        in cases when functions of different types exist with a given
        name.
      </p><p>
        <span class="bold"><strong>Built-In Function Name Parsing</strong></span>
      </p><p>
        The parser uses default rules for parsing names of built-in
        functions. These rules can be changed by enabling the
        <code class="literal">IGNORE_SPACE</code> SQL mode.
      </p><p>
        When the parser encounters a word that is the name of a built-in
        function, it must determine whether the name signifies a
        function call or is instead a non-expression reference to an
        identifier such as a table or column name. For example, in the
        following statements, the first reference to
        <code class="literal">count</code> is a function call, whereas the second
        reference is a table name:
      </p><pre class="programlisting">SELECT COUNT(*) FROM mytable;
CREATE TABLE count (i INT);
</pre><p>
        The parser should recognize the name of a built-in function as
        indicating a function call only when parsing what is expected to
        be an expression. That is, in non-expression context, function
        names are permitted as identifiers.
      </p><p>
        However, some built-in functions have special parsing or
        implementation considerations, so the parser uses the following
        rules by default to distinguish whether their names are being
        used as function calls or as identifiers in non-expression
        context:
      </p><div class="itemizedlist"><ul type="disc"><li><p>
            To use the name as a function call in an expression, there
            must be no whitespace between the name and the following
            “<span class="quote"><code class="literal">(</code></span>” parenthesis character.
          </p></li><li><p>
            Conversely, to use the function name as an identifier, it
            must not be followed immediately by a parenthesis.
          </p></li></ul></div><p>
        The requirement that function calls be written with no
        whitespace between the name and the parenthesis applies only to
        the built-in functions that have special considerations.
        <code class="literal">COUNT</code> is one such name. The exact list of
        function names for which following whitespace determines their
        interpretation are those listed in the
        <code class="literal">sql_functions[]</code> array of the
        <code class="filename">sql/lex.h</code> source file. Before MySQL 5.1,
        they are rather numerous (about 200), so you may find it easiest
        to treat the no-whitespace requirement as applying to all
        function calls. In MySQL 5.1, parser improvements reduce to
        about 30 the number of affected function names.
      </p><p>
        For functions not listed in the
        <code class="literal">sql_functions[]</code>) array, whitespace does not
        matter. They are interpreted as function calls only when used in
        expression context and may be used freely as identifiers
        otherwise. <code class="literal">ASCII</code> is one such name. However,
        for these non-affected function names, interpretation may vary
        in expression context:
        <code class="literal"><em class="replaceable"><code>func_name</code></em> ()</code> is
        interpreted as a built-in function if there is one with the
        given name; if not,
        <code class="literal"><em class="replaceable"><code>func_name</code></em> ()</code> is
        interpreted as a user-defined function or stored function if one
        exists with that name.
      </p><p>
        The <code class="literal">IGNORE_SPACE</code> SQL mode can be used to
        modify how the parser treats function names that are
        whitespace-sensitive:
      </p><div class="itemizedlist"><ul type="disc"><li><p>
            With <code class="literal">IGNORE_SPACE</code> disabled, the parser
            interprets the name as a function call when there is no
            whitespace between the name and the following parenthesis.
            This occurs even when the function name is used in
            non-expression context:
          </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE count(i INT);</code></strong>
ERROR 1064 (42000): You have an error in your SQL syntax ...
near 'count(i INT)'
</pre><p>
            To eliminate the error and cause the name to be treated as
            an identifier, either use whitespace following the name or
            write it as a quoted identifier (or both):
          </p><pre class="programlisting">CREATE TABLE count (i INT);
CREATE TABLE `count`(i INT);
CREATE TABLE `count` (i INT);
</pre></li><li><p>
            With <code class="literal">IGNORE_SPACE</code> enabled, the parser
            loosens the requirement that there be no whitespace between
            the function name and the following parenthesis. This
            provides more flexibility in writing function calls. For
            example, either of the following function calls are legal:
          </p><pre class="programlisting">SELECT COUNT(*) FROM mytable;
SELECT COUNT (*) FROM mytable;
</pre><p>
            However, enabling <code class="literal">IGNORE_SPACE</code> also has
            the side effect that the parser treats the affected function
            names as reserved words (see
            <a href="language-structure.html#reserved-words" title="8.3. Reserved Words">Section 8.3, “Reserved Words”</a>). This means that a space
            following the name no longer signifies its use as an
            identifier. The name can be used in function calls with or
            without following whitespace, but causes a syntax error in
            non-expression context unless it is quoted. For example,
            with <code class="literal">IGNORE_SPACE</code> enabled, both of the
            following statements fail with a syntax error because the
            parser interprets <code class="literal">count</code> as a reserved
            word:
          </p><pre class="programlisting">CREATE TABLE count(i INT);
CREATE TABLE count (i INT);
</pre><p>
            To use the function name in non-expression context, write it
            as a quoted identifier:
          </p><pre class="programlisting">CREATE TABLE `count`(i INT);
CREATE TABLE `count` (i INT);
</pre></li></ul></div><p>
        To enable the <code class="literal">IGNORE_SPACE</code> SQL mode, use this
        statement:
      </p><pre class="programlisting">SET sql_mode = 'IGNORE_SPACE';
</pre><p>
        <code class="literal">IGNORE_SPACE</code> is also enabled by certain other
        composite modes such as <code class="literal">ANSI</code> that include it
        in their value:
      </p><pre class="programlisting">SET sql_mode = 'ANSI';
</pre><p>
        Check <a href="server-administration.html#server-sql-mode" title="5.2.6. SQL Modes">Section 5.2.6, “SQL Modes”</a>, to see which composite
        modes enable <code class="literal">IGNORE_SPACE</code>.
      </p><p>
        To minimize the dependency of SQL code on the
        <code class="literal">IGNORE_SPACE</code> setting, use these guidelines:
      </p><div class="itemizedlist"><ul type="disc"><li><p>
            Avoid creating UDFs or stored functions that have the same
            name as a built-in function.
          </p></li><li><p>
            Avoid using function names in non-expression context. For
            example, these statements use <code class="literal">count</code> (one
            of the affected function names affected by
            <code class="literal">IGNORE_SPACE</code>), so they fail with or
            without whitespace following the name if
            <code class="literal">IGNORE_SPACE</code> is enabled:
          </p><pre class="programlisting">CREATE TABLE count(i INT);
CREATE TABLE count (i INT);
</pre><p>
            If you must use a function name in non-expression context,
            write it as a quoted identifier:
          </p><pre class="programlisting">CREATE TABLE `count`(i INT);
CREATE TABLE `count` (i INT);
</pre></li></ul></div><p>
        <span class="bold"><strong>Function Name Resolution</strong></span>
      </p><p>
        The following rules describe how the server resolves references
        to function names for function creation and invocation:
      </p><div class="itemizedlist"><ul type="disc"><li><p>
            Built-in functions and user-defined functions
          </p><p>
            A UDF can be created with the same name as a built-in
            function but the UDF cannot be invoked because the parser
            resolves invocations of the function to refer to the
            built-in function. For example, if you create a UDF named
            <code class="literal">ABS</code>, references to
            <a href="functions.html#function_abs"><code class="literal">ABS()</code></a> invoke the built-in
            function.
          </p></li><li><p>
            Built-in functions and stored functions
          </p><p>
            It is possible to create a stored function with the same
            name as a built-in function, but to invoke the stored
            function it is necessary to qualify it with a database name.
            For example, if you create a stored function named
            <code class="literal">PI</code> in the <code class="literal">test</code>
            database, you invoke it as <code class="literal">test.PI()</code>
            because the server resolves
            <a href="functions.html#function_pi"><code class="literal">PI()</code></a> as a reference to the
            built-in function.
          </p></li><li><p>
            User-defined functions and stored functions
          </p><p>
            User-defined functions and stored functions share the same
            namespace, so you cannot create a UDF and a stored function
            with the same name.
          </p></li></ul></div><p>
        The preceding function name resolution rules have implications
        for upgrading to versions of MySQL that implement new built-in
        functions:
      </p><div class="itemizedlist"><ul type="disc"><li><p>
            If you have already created a user-defined function with a
            given name and upgrade MySQL to a version that implements a
            new built-in function with the same name, the UDF becomes
            inaccessible. To correct this, use <code class="literal">DROP
            FUNCTION</code> to drop the UDF, and then use
            <code class="literal">CREATE FUNCTION</code> to re-create the UDF with
            a different non-conflicting name.
          </p></li><li><p>
            If a new version of MySQL implements a built-in function
            with the same name as an existing stored function, you have
            two choices: Rename the stored function to use a
            non-conflicting name, or change calls to the function so
            that they use a schema qualifier (that is, use
            <code class="literal"><em class="replaceable"><code>schema_name</code></em>.<em class="replaceable"><code>func_name</code></em>()</code>
            syntax).
          </p></li></ul></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="reserved-words"></a>8.3. Reserved Words</h2></div></div></div><a class="indexterm" name="id2738471"></a><a class="indexterm" name="id2738480"></a><p>
      Certain words such as <code class="literal">SELECT</code>,
      <code class="literal">DELETE</code>, or <code class="literal">BIGINT</code> are
      reserved and require special treatment for use as identifiers such
      as table and column names. This may also be true for the names of
      built-in functions.
    </p><p>
      Reserved words are permitted as identifiers if you quote them as
      described in <a href="language-structure.html#identifiers" title="8.2. Schema Object Names">Section 8.2, “Schema Object Names”</a>:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE interval (begin INT, end INT);</code></strong>
ERROR 1064 (42000): You have an error in your SQL syntax ...
near 'interval (begin INT, end INT)'

mysql&gt; <strong class="userinput"><code>CREATE TABLE `interval` (begin INT, end INT);</code></strong>
Query OK, 0 rows affected (0.01 sec)
</pre><p>
      Exception: A word that follows a period in a qualified name must
      be an identifier, so it need not be quoted even if it is reserved:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE mydb.interval (begin INT, end INT);</code></strong>
Query OK, 0 rows affected (0.01 sec)
</pre><p>
      Names of built-in functions are permitted as identifiers but may
      require care to be used as such. For example,
      <code class="literal">COUNT</code> is acceptable as a column name. However,
      by default, no whitespace is allowed in function invocations
      between the function name and the following
      “<span class="quote"><code class="literal">(</code></span>” character. This requirement
      enables the parser to distinguish whether the name is used in a
      function call or in non-function context. For further detail on
      recognition of function names, see
      <a href="language-structure.html#function-resolution" title="8.2.3. Function Name Parsing and Resolution">Section 8.2.3, “Function Name Parsing and Resolution”</a>.
    </p><p>
      The words in the following table are explicitly reserved in MySQL
      5.0. At some point, you might upgrade to a higher
      version, so it's a good idea to have a look at future reserved
      words, too. You can find these in the manuals that cover higher
      versions of MySQL. Most of the words in the table are forbidden by
      standard SQL as column or table names (for example,
      <code class="literal">GROUP</code>). A few are reserved because MySQL needs
      them and uses a <span><strong class="command">yacc</strong></span> parser. A reserved word
      can be used as an identifier if you quote it.
    </p><p>
      For a more detailed list of reserved words, including differences
      between versions, see
      <a href="http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-0.html" target="_top">Reserved Words in MySQL 5.0</a>.
    </p><div class="informaltable"><table border="1"><colgroup><col><col><col></colgroup><tbody><tr><td><code class="literal">ADD</code></td><td><code class="literal">ALL</code></td><td><code class="literal">ALTER</code></td></tr><tr><td><code class="literal">ANALYZE</code></td><td><code class="literal">AND</code></td><td><code class="literal">AS</code></td></tr><tr><td><code class="literal">ASC</code></td><td><code class="literal">ASENSITIVE</code></td><td><code class="literal">BEFORE</code></td></tr><tr><td><code class="literal">BETWEEN</code></td><td><code class="literal">BIGINT</code></td><td><code class="literal">BINARY</code></td></tr><tr><td><code class="literal">BLOB</code></td><td><code class="literal">BOTH</code></td><td><code class="literal">BY</code></td></tr><tr><td><code class="literal">CALL</code></td><td><code class="literal">CASCADE</code></td><td><code class="literal">CASE</code></td></tr><tr><td><code class="literal">CHANGE</code></td><td><code class="literal">CHAR</code></td><td><code class="literal">CHARACTER</code></td></tr><tr><td><code class="literal">CHECK</code></td><td><code class="literal">COLLATE</code></td><td><code class="literal">COLUMN</code></td></tr><tr><td><code class="literal">CONDITION</code></td><td><code class="literal">CONSTRAINT</code></td><td><code class="literal">CONTINUE</code></td></tr><tr><td><code class="literal">CONVERT</code></td><td><code class="literal">CREATE</code></td><td><code class="literal">CROSS</code></td></tr><tr><td><code class="literal">CURRENT_DATE</code></td><td><code class="literal">CURRENT_TIME</code></td><td><code class="literal">CURRENT_TIMESTAMP</code></td></tr><tr><td><code class="literal">CURRENT_USER</code></td><td><code class="literal">CURSOR</code></td><td><code class="literal">DATABASE</code></td></tr><tr><td><code class="literal">DATABASES</code></td><td><code class="literal">DAY_HOUR</code></td><td><code class="literal">DAY_MICROSECOND</code></td></tr><tr><td><code class="literal">DAY_MINUTE</code></td><td><code class="literal">DAY_SECOND</code></td><td><code class="literal">DEC</code></td></tr><tr><td><code class="literal">DECIMAL</code></td><td><code class="literal">DECLARE</code></td><td><code class="literal">DEFAULT</code></td></tr><tr><td><code class="literal">DELAYED</code></td><td><code class="literal">DELETE</code></td><td><code class="literal">DESC</code></td></tr><tr><td><code class="literal">DESCRIBE</code></td><td><code class="literal">DETERMINISTIC</code></td><td><code class="literal">DISTINCT</code></td></tr><tr><td><code class="literal">DISTINCTROW</code></td><td><code class="literal">DIV</code></td><td><code class="literal">DOUBLE</code></td></tr><tr><td><code class="literal">DROP</code></td><td><code class="literal">DUAL</code></td><td><code class="literal">EACH</code></td></tr><tr><td><code class="literal">ELSE</code></td><td><code class="literal">ELSEIF</code></td><td><code class="literal">ENCLOSED</code></td></tr><tr><td><code class="literal">ESCAPED</code></td><td><code class="literal">EXISTS</code></td><td><code class="literal">EXIT</code></td></tr><tr><td><code class="literal">EXPLAIN</code></td><td><code class="literal">FALSE</code></td><td><code class="literal">FETCH</code></td></tr><tr><td><code class="literal">FLOAT</code></td><td><code class="literal">FLOAT4</code></td><td><code class="literal">FLOAT8</code></td></tr><tr><td><code class="literal">FOR</code></td><td><code class="literal">FORCE</code></td><td><code class="literal">FOREIGN</code></td></tr><tr><td><code class="literal">FROM</code></td><td><code class="literal">FULLTEXT</code></td><td><code class="literal">GRANT</code></td></tr><tr><td><code class="literal">GROUP</code></td><td><code class="literal">HAVING</code></td><td><code class="literal">HIGH_PRIORITY</code></td></tr><tr><td><code class="literal">HOUR_MICROSECOND</code></td><td><code class="literal">HOUR_MINUTE</code></td><td><code class="literal">HOUR_SECOND</code></td></tr><tr><td><code class="literal">IF</code></td><td><code class="literal">IGNORE</code></td><td><code class="literal">IN</code></td></tr><tr><td><code class="literal">INDEX</code></td><td><code class="literal">INFILE</code></td><td><code class="literal">INNER</code></td></tr><tr><td><code class="literal">INOUT</code></td><td><code class="literal">INSENSITIVE</code></td><td><code class="literal">INSERT</code></td></tr><tr><td><code class="literal">INT</code></td><td><code class="literal">INT1</code></td><td><code class="literal">INT2</code></td></tr><tr><td><code class="literal">INT3</code></td><td><code class="literal">INT4</code></td><td><code class="literal">INT8</code></td></tr><tr><td><code class="literal">INTEGER</code></td><td><code class="literal">INTERVAL</code></td><td><code class="literal">INTO</code></td></tr><tr><td><code class="literal">IS</code></td><td><code class="literal">ITERATE</code></td><td><code class="literal">JOIN</code></td></tr><tr><td><code class="literal">KEY</code></td><td><code class="literal">KEYS</code></td><td><code class="literal">KILL</code></td></tr><tr><td><code class="literal">LEADING</code></td><td><code class="literal">LEAVE</code></td><td><code class="literal">LEFT</code></td></tr><tr><td><code class="literal">LIKE</code></td><td><code class="literal">LIMIT</code></td><td><code class="literal">LINES</code></td></tr><tr><td><code class="literal">LOAD</code></td><td><code class="literal">LOCALTIME</code></td><td><code class="literal">LOCALTIMESTAMP</code></td></tr><tr><td><code class="literal">LOCK</code></td><td><code class="literal">LONG</code></td><td><code class="literal">LONGBLOB</code></td></tr><tr><td><code class="literal">LONGTEXT</code></td><td><code class="literal">LOOP</code></td><td><code class="literal">LOW_PRIORITY</code></td></tr><tr><td><code class="literal">MATCH</code></td><td><code class="literal">MEDIUMBLOB</code></td><td><code class="literal">MEDIUMINT</code></td></tr><tr><td><code class="literal">MEDIUMTEXT</code></td><td><code class="literal">MIDDLEINT</code></td><td><code class="literal">MINUTE_MICROSECOND</code></td></tr><tr><td><code class="literal">MINUTE_SECOND</code></td><td><code class="literal">MOD</code></td><td><code class="literal">MODIFIES</code></td></tr><tr><td><code class="literal">NATURAL</code></td><td><code class="literal">NOT</code></td><td><code class="literal">NO_WRITE_TO_BINLOG</code></td></tr><tr><td><code class="literal">NULL</code></td><td><code class="literal">NUMERIC</code></td><td><code class="literal">ON</code></td></tr><tr><td><code class="literal">OPTIMIZE</code></td><td><code class="literal">OPTION</code></td><td><code class="literal">OPTIONALLY</code></td></tr><tr><td><code class="literal">OR</code></td><td><code class="literal">ORDER</code></td><td><code class="literal">OUT</code></td></tr><tr><td><code class="literal">OUTER</code></td><td><code class="literal">OUTFILE</code></td><td><code class="literal">PRECISION</code></td></tr><tr><td><code class="literal">PRIMARY</code></td><td><code class="literal">PROCEDURE</code></td><td><code class="literal">PURGE</code></td></tr><tr><td><code class="literal">READ</code></td><td><code class="literal">READS</code></td><td><code class="literal">REAL</code></td></tr><tr><td><code class="literal">REFERENCES</code></td><td><code class="literal">REGEXP</code></td><td><code class="literal">RELEASE</code></td></tr><tr><td><code class="literal">RENAME</code></td><td><code class="literal">REPEAT</code></td><td><code class="literal">REPLACE</code></td></tr><tr><td><code class="literal">REQUIRE</code></td><td><code class="literal">RESTRICT</code></td><td><code class="literal">RETURN</code></td></tr><tr><td><code class="literal">REVOKE</code></td><td><code class="literal">RIGHT</code></td><td><code class="literal">RLIKE</code></td></tr><tr><td><code class="literal">SCHEMA</code></td><td><code class="literal">SCHEMAS</code></td><td><code class="literal">SECOND_MICROSECOND</code></td></tr><tr><td><code class="literal">SELECT</code></td><td><code class="literal">SENSITIVE</code></td><td><code class="literal">SEPARATOR</code></td></tr><tr><td><code class="literal">SET</code></td><td><code class="literal">SHOW</code></td><td><code class="literal">SMALLINT</code></td></tr><tr><td><code class="literal">SONAME</code></td><td><code class="literal">SPATIAL</code></td><td><code class="literal">SPECIFIC</code></td></tr><tr><td><code class="literal">SQL</code></td><td><code class="literal">SQLEXCEPTION</code></td><td><code class="literal">SQLSTATE</code></td></tr><tr><td><code class="literal">SQLWARNING</code></td><td><code class="literal">SQL_BIG_RESULT</code></td><td><code class="literal">SQL_CALC_FOUND_ROWS</code></td></tr><tr><td><code class="literal">SQL_SMALL_RESULT</code></td><td><code class="literal">SSL</code></td><td><code class="literal">STARTING</code></td></tr><tr><td><code class="literal">STRAIGHT_JOIN</code></td><td><code class="literal">TABLE</code></td><td><code class="literal">TERMINATED</code></td></tr><tr><td><code class="literal">THEN</code></td><td><code class="literal">TINYBLOB</code></td><td><code class="literal">TINYINT</code></td></tr><tr><td><code class="literal">TINYTEXT</code></td><td><code class="literal">TO</code></td><td><code class="literal">TRAILING</code></td></tr><tr><td><code class="literal">TRIGGER</code></td><td><code class="literal">TRUE</code></td><td><code class="literal">UNDO</code></td></tr><tr><td><code class="literal">UNION</code></td><td><code class="literal">UNIQUE</code></td><td><code class="literal">UNLOCK</code></td></tr><tr><td><code class="literal">UNSIGNED</code></td><td><code class="literal">UPDATE</code></td><td><code class="literal">USAGE</code></td></tr><tr><td><code class="literal">USE</code></td><td><code class="literal">USING</code></td><td><code class="literal">UTC_DATE</code></td></tr><tr><td><code class="literal">UTC_TIME</code></td><td><code class="literal">UTC_TIMESTAMP</code></td><td><code class="literal">VALUES</code></td></tr><tr><td><code class="literal">VARBINARY</code></td><td><code class="literal">VARCHAR</code></td><td><code class="literal">VARCHARACTER</code></td></tr><tr><td><code class="literal">VARYING</code></td><td><code class="literal">WHEN</code></td><td><code class="literal">WHERE</code></td></tr><tr><td><code class="literal">WHILE</code></td><td><code class="literal">WITH</code></td><td><code class="literal">WRITE</code></td></tr><tr><td><code class="literal">XOR</code></td><td><code class="literal">YEAR_MONTH</code></td><td><code class="literal">ZEROFILL</code></td></tr></tbody></table></div><p>The following are new reserved words in MySQL 5.0:</p><div class="informaltable"><table border="1"><colgroup><col><col><col></colgroup><tbody><tr><td><code class="literal">ASENSITIVE</code></td><td><code class="literal">CALL</code></td><td><code class="literal">CONDITION</code></td></tr><tr><td><code class="literal">CONNECTION</code></td><td><code class="literal">CONTINUE</code></td><td><code class="literal">CURSOR</code></td></tr><tr><td><code class="literal">DECLARE</code></td><td><code class="literal">DETERMINISTIC</code></td><td><code class="literal">EACH</code></td></tr><tr><td><code class="literal">ELSEIF</code></td><td><code class="literal">EXIT</code></td><td><code class="literal">FETCH</code></td></tr><tr><td><code class="literal">GOTO</code></td><td><code class="literal">INOUT</code></td><td><code class="literal">INSENSITIVE</code></td></tr><tr><td><code class="literal">ITERATE</code></td><td><code class="literal">LABEL</code></td><td><code class="literal">LEAVE</code></td></tr><tr><td><code class="literal">LOOP</code></td><td><code class="literal">MODIFIES</code></td><td><code class="literal">OUT</code></td></tr><tr><td><code class="literal">READS</code></td><td><code class="literal">RELEASE</code></td><td><code class="literal">REPEAT</code></td></tr><tr><td><code class="literal">RETURN</code></td><td><code class="literal">SCHEMA</code></td><td><code class="literal">SCHEMAS</code></td></tr><tr><td><code class="literal">SENSITIVE</code></td><td><code class="literal">SPECIFIC</code></td><td><code class="literal">SQL</code></td></tr><tr><td><code class="literal">SQLEXCEPTION</code></td><td><code class="literal">SQLSTATE</code></td><td><code class="literal">SQLWARNING</code></td></tr><tr><td><code class="literal">TRIGGER</code></td><td><code class="literal">UNDO</code></td><td><code class="literal">UPGRADE</code></td></tr><tr><td><code class="literal">WHILE</code></td><td class="auto-generated"> </td><td class="auto-generated"> </td></tr></tbody></table></div><p>
      MySQL allows some keywords to be used as unquoted identifiers
      because many people previously used them. Examples are those in
      the following list:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          <code class="literal">ACTION</code>
        </p></li><li><p>
          <code class="literal">BIT</code>
        </p></li><li><p>
          <code class="literal">DATE</code>
        </p></li><li><p>
          <code class="literal">ENUM</code>
        </p></li><li><p>
          <code class="literal">NO</code>
        </p></li><li><p>
          <code class="literal">TEXT</code>
        </p></li><li><p>
          <code class="literal">TIME</code>
        </p></li><li><p>
          <code class="literal">TIMESTAMP</code>
        </p></li></ul></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="user-variables"></a>8.4. User-Defined Variables</h2></div></div></div><a class="indexterm" name="id2740428"></a><a class="indexterm" name="id2740440"></a><a class="indexterm" name="id2740449"></a><a class="indexterm" name="id2740462"></a><a class="indexterm" name="id2740471"></a><a class="indexterm" name="id2740480"></a><p>
      You can store a value in a user-defined variable and then refer to
      it later. This enables you to pass values from one statement to
      another. <span class="emphasis"><em>User-defined variables are
      connection-specific</em></span>. That is, a user variable defined
      by one client cannot be seen or used by other clients. All
      variables for a given client connection are automatically freed
      when that client exits.
    </p><p>
      User variables are written as
      <code class="literal">@<em class="replaceable"><code>var_name</code></em></code>, where the
      variable name <em class="replaceable"><code>var_name</code></em> may consist of
      alphanumeric characters from the current character set,
      “<span class="quote"><code class="literal">.</code></span>”,
      “<span class="quote"><code class="literal">_</code></span>”, and
      “<span class="quote"><code class="literal">$</code></span>”. The default character set is
      <code class="literal">latin1</code> (cp1252 West European). This may be
      changed with the <code class="option">--default-character-set</code> option
      to <span><strong class="command">mysqld</strong></span>. See
      <a href="internationalization-localization.html#charset-configuration" title="9.2. The Character Set Used for Data and Sorting">Section 9.2, “The Character Set Used for Data and Sorting”</a>. A user variable name can
      contain other characters if you quote it as a string or identifier
      (for example, <code class="literal">@'my-var'</code>,
      <code class="literal">@"my-var"</code>, or <code class="literal">@`my-var`</code>).
    </p><p>
      Note: User variable names are case sensitive before MySQL 5.0 and
      not case sensitive in MySQL 5.0 and up.
    </p><p>
      One way to set a user-defined variable is by issuing a
      <code class="literal">SET</code> statement:
    </p><pre class="programlisting">SET @<em class="replaceable"><code>var_name</code></em> = <em class="replaceable"><code>expr</code></em> [, @<em class="replaceable"><code>var_name</code></em> = <em class="replaceable"><code>expr</code></em>] ...
</pre><p>
      For <code class="literal">SET</code>, either <code class="literal">=</code> or
      <code class="literal">:=</code> can be used as the assignment operator. The
      <em class="replaceable"><code>expr</code></em> assigned to each variable can
      evaluate to an integer, decimal, real, string, or
      <code class="literal">NULL</code> value. However, if the value of the
      variable is selected in a result set, it is returned to the client
      as a string. Assignment of decimal and real values does not
      preserve the precision or scale of the value.
    </p><p>
      You can also assign a value to a user variable in statements other
      than <code class="literal">SET</code>. In this case, the assignment operator
      must be <code class="literal">:=</code> and not <code class="literal">=</code> because
      <code class="literal">=</code> is treated as a comparison operator in
      non-<code class="literal">SET</code> statements:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SET @t1=0, @t2=0, @t3=0;</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT @t1:=(@t2:=1)+@t3:=4,@t1,@t2,@t3;</code></strong>
+----------------------+------+------+------+
| @t1:=(@t2:=1)+@t3:=4 | @t1  | @t2  | @t3  |
+----------------------+------+------+------+
|                    5 |    5 |    1 |    4 |
+----------------------+------+------+------+
</pre><p>
      User variables may be used in contexts where expressions are
      allowed. This does not currently include contexts that explicitly
      require a literal value, such as in the <code class="literal">LIMIT</code>
      clause of a <code class="literal">SELECT</code> statement, or the
      <code class="literal">IGNORE <em class="replaceable"><code>N</code></em> LINES</code>
      clause of a <code class="literal">LOAD DATA</code> statement.
    </p><p>
      If a user variable is assigned a string value, it has the same
      character set and collation as the string. The coercibility of
      user variables is implicit as of MySQL 5.0.3. (This is the same
      coercibility as for table column values.)
    </p><p>
      If you refer to a variable that has not been initialized, it has a
      value of <code class="literal">NULL</code> and a type of string.
    </p><p>
      Bit values assigned to user variables are treated as binary
      strings. To assign a bit value as a number to a user variable, use
      <a href="functions.html#function_cast"><code class="literal">CAST()</code></a> or <code class="literal">+0</code>:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SET @v1 = b'1000001';</code></strong>
mysql&gt; <strong class="userinput"><code>SET @v2 = CAST(b'1000001' AS UNSIGNED), @v3 = b'1000001'+0;</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT @v1, @v2, @v3;</code></strong>
+------+------+------+
| @v1  | @v2  | @v3  |
+------+------+------+
| A    |   65 |   65 | 
+------+------+------+
</pre><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
        In a <code class="literal">SELECT</code> statement, each expression is
        evaluated only when sent to the client. This means that in a
        <code class="literal">HAVING</code>, <code class="literal">GROUP BY</code>, or
        <code class="literal">ORDER BY</code> clause, you cannot refer to an
        expression that involves variables that are set in the
        <code class="literal">SELECT</code> list. For example, the following
        statement does <span class="emphasis"><em>not</em></span> work as expected:
      </p></div><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT (@aa:=id) AS a, (@aa+3) AS b FROM <em class="replaceable"><code>tbl_name</code></em> HAVING b=5;</code></strong>
</pre><p>
      The reference to <code class="literal">b</code> in the
      <code class="literal">HAVING</code> clause refers to an alias for an
      expression in the <code class="literal">SELECT</code> list that uses
      <code class="literal">@aa</code>. This does not work as expected:
      <code class="literal">@aa</code> contains the value of <code class="literal">id</code>
      from the previous selected row, not from the current row.
    </p><p>
      The order of evaluation for user variables is undefined and may
      change based on the elements contained within a given query. In
      <code class="literal">SELECT @a, @a := @a+1 ...</code>, you might think that
      MySQL will evaluate <code class="literal">@a</code> first and then do an
      assignment second, but changing the query (for example, by adding
      a <code class="literal">GROUP BY</code>, <code class="literal">HAVING</code>, or
      <code class="literal">ORDER BY</code> clause) may change the order of
      evaluation.
    </p><p>
      The general rule is never to assign a value to a user variable in
      one part of a statement <span class="emphasis"><em>and</em></span> use the same
      variable in some other part of the same statement. You might get
      the results you expect, but this is not guaranteed.
    </p><p>
      Another issue with setting a variable and using it in the same
      statement is that the default result type of a variable is based
      on the type of the variable at the start of the statement. The
      following example illustrates this:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SET @a='test';</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT @a,(@a:=20) FROM <em class="replaceable"><code>tbl_name</code></em>;</code></strong>
</pre><p>
      For this <code class="literal">SELECT</code> statement, MySQL reports to the
      client that column one is a string and converts all accesses of
      <code class="literal">@a</code> to strings, even though @a is set to a
      number for the second row. After the <code class="literal">SELECT</code>
      statement executes, <code class="literal">@a</code> is regarded as a number
      for the next statement.
    </p><p>
      To avoid problems with this behavior, either do not set and use
      the same variable within a single statement, or else set the
      variable to <code class="literal">0</code>, <code class="literal">0.0</code>, or
      <code class="literal">''</code> to define its type before you use it.
    </p><p>
      A user variable cannot be used directly in an SQL statement as an
      identifier or as part of an identifier, even if it is set off with
      backticks. This is shown in the following example:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT c1 FROM t;</code></strong>
+----+
| c1 |
+----+
|  0 |
+----+
|  1 |
+----+
2 rows in set (0.00 sec)

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

mysql&gt; <strong class="userinput"><code>SELECT @col FROM t;</code></strong>
+------+
| @col |
+------+
| c1   |
+------+
1 row in set (0.00 sec)

mysql&gt; <strong class="userinput"><code>SELECT `@col` FROM t;</code></strong>
<span class="errortext">ERROR 1054 (42S22): Unknown column '@col' in 'field list'</span>

mysql&gt; SET @col = "`c1`";
Query OK, 0 rows affected (0.00 sec)

mysql&gt; <strong class="userinput"><code>SELECT @col FROM t;</code></strong>
+------+
| @col |
+------+
| `c1` |
+------+
1 row in set (0.00 sec)
</pre><p>
      One way to work around this problem is to assemble a string for
      the query in application code, as shown here using PHP 5:
    </p><pre class="programlisting">&lt;?php
  $mysqli = new mysqli("localhost", "user", "pass", "test");
  
  if( mysqli_connect_errno() )
    die("Connection failed: %s\n", mysqli_connect_error());

  $col = "c1";
  
  $query = "SELECT $col FROM t";
  
  $result = $mysqli-&gt;query($query);
  
  while($row = $result-&gt;fetch_assoc())
  {
    echo "&lt;p&gt;" . $row["$col"] . "&lt;/p&gt;\n";
  }
  
  $result-&gt;close();
  
  $mysqli-&gt;close();
?&gt;
</pre><p>
      (Assembling an SQL statement in this fashion is sometimes known as
      “<span class="quote">Dynamic SQL</span>”.) It is also possible to perform such
      operations using prepared statements, without the need to
      concatenate strings of SQL in client code. This example
      illustrates how this can be done:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SET @c = "c1";</code></strong>
Query OK, 0 rows affected (0.00 sec)

mysql&gt; <strong class="userinput"><code>SET @s = CONCAT("SELECT ", @c, " FROM t");</code></strong>
Query OK, 0 rows affected (0.00 sec)

mysql&gt; <strong class="userinput"><code>PREPARE stmt FROM @s;</code></strong>
Query OK, 0 rows affected (0.04 sec)
Statement prepared

mysql&gt; <strong class="userinput"><code>EXECUTE stmt;</code></strong>
+----+
| c1 |
+----+
|  0 |
+----+
|  1 |
+----+
2 rows in set (0.00 sec)

mysql&gt; <strong class="userinput"><code>DEALLOCATE PREPARE stmt;</code></strong>
Query OK, 0 rows affected (0.00 sec)
</pre><p>
      You cannot use a placeholder for an identifier (such as the name
      of a database, table, or column) in an SQL prepared statement. See
      <a href="sql-syntax.html#sqlps" title="12.7. SQL Syntax for Prepared Statements">Section 12.7, “SQL Syntax for Prepared Statements”</a>, for more information.
    </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="comments"></a>8.5. Comment Syntax</h2></div></div></div><a class="indexterm" name="id2741206"></a><a class="indexterm" name="id2741215"></a><p>
      MySQL Server supports three comment styles:
    </p><div class="itemizedlist"><ul type="disc"><li><p>
          From a “<span class="quote"><code class="literal">#</code></span>” character to the
          end of the line.
        </p></li><li><p>
          From a “<span class="quote"><code class="literal">-- </code></span>” sequence to
          the end of the line. In MySQL, the
          “<span class="quote"><code class="literal">-- </code></span>” (double-dash)
          comment style requires the second dash to be followed by at
          least one whitespace or control character (such as a space,
          tab, newline, and so on). This syntax differs slightly from
          standard SQL comment syntax, as discussed in
          <a href="introduction.html#ansi-diff-comments" title="1.8.5.6. '--' as the Start of a Comment">Section 1.8.5.6, “'<code class="literal">--</code>' as the Start of a Comment”</a>.
        </p></li><li><p>
          From a <code class="literal">/*</code> sequence to the following
          <code class="literal">*/</code> sequence, as in the C programming
          language. This syntax allows a comment to extend over multiple
          lines because the beginning and closing sequences need not be
          on the same line.
        </p></li></ul></div><p>
      The following example demonstrates all three comment styles:
    </p><pre class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT 1+1;     # This comment continues to the end of line</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT 1+1;     -- This comment continues to the end of line</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT 1 /* this is an in-line comment */ + 1;</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT 1+</code></strong>
<strong class="userinput"><code>/*</code></strong>
<strong class="userinput"><code>this is a</code></strong>
<strong class="userinput"><code>multiple-line comment</code></strong>
<strong class="userinput"><code>*/</code></strong>
<strong class="userinput"><code>1;</code></strong>
</pre><p>
      Nested comments are not supported.
    </p><p>
      MySQL Server supports some variants of C-style comments. These
      enable you to write code that includes MySQL extensions, but is
      still portable, by using comments of the following form:
    </p><pre class="programlisting">/*! <em class="replaceable"><code>MySQL-specific code</code></em> */
</pre><p>
      In this case, MySQL Server parses and executes the code within the
      comment as it would any other SQL statement, but other SQL servers
      will ignore the extensions. For example, MySQL Server recognizes
      the <code class="literal">STRAIGHT_JOIN</code> keyword in the following
      statement, but other servers will not:
    </p><pre class="programlisting">SELECT /*! STRAIGHT_JOIN */ col1 FROM table1,table2 WHERE ...
</pre><p>
      If you add a version number after the
      “<span class="quote"><code class="literal">!</code></span>” character, the syntax within
      the comment is executed only if the MySQL version is greater than
      or equal to the specified version number. The
      <code class="literal">TEMPORARY</code> keyword in the following comment is
      executed only by servers from MySQL 3.23.02 or higher:
    </p><pre class="programlisting">CREATE /*!32302 TEMPORARY */ TABLE t (a INT);
</pre><p>
      The comment syntax just described applies to how the
      <span><strong class="command">mysqld</strong></span> server parses SQL statements. The
      <span><strong class="command">mysql</strong></span> client program also performs some parsing
      of statements before sending them to the server. (It does this to
      determine statement boundaries within a multiple-statement input
      line.)
    </p><p>
      The use of short-form <span><strong class="command">mysql</strong></span> commands such as
      <code class="literal">\C</code> within multi-line <code class="literal">/* ...
      */</code> comments is not supported.
    </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="client-utility-programs.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="internationalization-localization.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 7. Client and Utility Programs </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 9. Internationalization and Localization</td></tr></table></div></body></html>