MySQL 5.0 Reference Manual :: 9 Language Structure :: 9.2 Database, Table, Index, Column, and Alias Names


  • MySQL 5.0 Reference Manual

  • 9 Language Structure
  • 9.1 Literal Values
  • 9.2 Database, Table, Index, Column, and Alias Names
    • 9.2.1 Identifier Qualifiers
    • 9.2.2 Identifier Case Sensitivity
  • 9.3 User-Defined Variables
  • 9.4 Comment Syntax
  • 9.5 Treatment of Reserved Words in MySQL


MySQL 5.0 Reference Manual :: 9 Language Structure :: 9.2 Database, Table, Index, Column, and Alias Names

9.2. Database, Table, Index, Column, and Alias Names

9.2.1. Identifier Qualifiers
9.2.2. Identifier Case Sensitivity

Database, table, index, column, and alias names are identifiers(標識符 名字). This section describes the allowable syntax for identifiers in MySQL.

注意,從MySQL3.23.6開始規則改變了,此時我們引入了用 ' 引用的標識符(數據庫、表和列命名)(如果你以ANSI模式運行," 也將用于引用標識符)。

The following table describes the maximum length for each type of identifier.

Identifier Maximum Length  
Database 64  
Table 64  
Column 64  
Index 64  
Alias 255  

There are some restrictions(限制) on the characters that may appear in identifiers:

  • No identifier can contain ASCII 0 (0x00) or a byte with a value of 255.

  • The use of identifier quote characters in identifiers is permitted, although it is best to avoid doing so if possible. 引號   `    '   盡可能避免使用 For example

  • Database, table, and column names should not end with space characters不可結束是空白.

  • Database names cannot contain ‘/’ , ‘\’ , ‘.’ , 除了  \  / 斜線 或  .    點 or characters that are not allowed in a directory name.
    你不能在名字中使用“.”,因為它被用來擴充格式

  • Table names cannot contain ‘/’, ‘\’, ‘.’, or characters that are not allowed in a filename.

Identifiers are stored using Unicode (UTF-8). This applies to identifiers in table definitions that stored in .frm files and to identifiers stored in the grant tables in the mysql database. The sizes of the string columns in the grant tables (and in any other tables) in MySQL 5.0 are given as number of characters. This means that (unlike some earlier versions of MySQL) you can use multi-byte characters without reducing the number of characters allowed for values stored in these columns.

An identifier may be quoted or unquoted. If an identifier is a reserved word or contains special characters, you must quote it whenever you refer to it. (Exception: A word that follows a period in a qualified name must be an identifier, so it is not necessary to quote it, even if it is a reserved word.) For a list of reserved words(保留字 MySQL專有名詞), see Section 9.5, “Treatment of Reserved Words in MySQL”. Special characters are those outside the set of alphanumeric characters from the current character set, ‘_’, and ‘$’     (   ‘_’, and ‘$’  可以使用 ).

注意,如果標識符是一個保留字 (MySQL專有名詞)或包含特殊字符,當你使用它時,你必須總是用 引號` 引用它:

The identifier quote character is the backtick ‘`’ ( 鍵盤左上方) :

mysql> SELECT * FROM `select` WHERE `select`.id > 100;

If the ANSI_QUOTES SQL mode is enabled, it is also allowable to quote identifiers within double quotes:

mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax. (...)
mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)

Note: Because the ANSI_QUOTES mode causes the server to interpret double-quoted strings as identifiers, string literals must be enclosed within single quotes when this mode is enabled. They cannot be enclosed within double quotes.

The server SQL mode is controlled as described in Section 5.2.5, “The Server SQL Mode”.

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 a`b that contains a column named c"d:

mysql> CREATE TABLE `a``b` (`c"d` INT);

It is recommended that you do not use names of the form Me or MeN, where M and N are integers. For example, avoid using 1e or 2e2 as identifiers, because an expression such as 1e+3 is ambiguous. Depending on context, it might be interpreted as the expression 1e + 3 or as the number 1e+3.

建議你不使用象1e這樣的名字,因為一個表達式如1e+1是二義性的。它可以 解釋為 表達式1e + 1 ?或 解釋為 數字1e+1 ???。

Be careful when using MD5() to produce table names because it can produce names in illegal or ambiguous formats such as those just described.


User Comments

Posted by RVidal on March 10 2006 4:57pm[Delete] [Edit]

This functionality is very useful. See these two examples:

ok:
select * from `select`;

nok:
SELECT * FROM MyDataTable Where firstfield='`select`'; -> nok

Inside ' '(strings) don't use backtick(`)

Correct way:
SELECT * FROM MyDataTable Where firstfield='select';

Regards,
RVidal

Add your own comment.



MySQL 5.0 Reference Manual :: 9 Language Structure :: 9.2 Database, Table, Index, Column, and Alias Names :: 9.2.1 Identifier Qualifiers

  • MySQL 5.0 Reference Manual
  • 9.2 Database, Table, Index, Column, and Alias Names
  • 9.2.1 Identifier Qualifiers
  • 9.2.2 Identifier Case Sensitivity


9.2.1. Identifier Qualifiers

MySQL allows names that consist of a single identifier or multiple identifiers. The components of a multiple-part name should be separated by period (‘.’) characters. The initial parts of a multiple-part name act as qualifiers that affect the context within which the final identifier is interpreted.

In MySQL you can refer to a column using any of the following forms:

Column Reference Meaning
col_name The column col_name from whichever table used in the statement contains a column of that name.
tbl_name.col_name The column col_name from table tbl_name of the default database.
db_name.tbl_name.col_name The column col_name from table tbl_name of the database db_name.

If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.

You need not specify a tbl_name or db_name.tbl_name prefix for a column reference in a statement unless the reference would be ambiguous. Suppose that tables t1 and t2 each contain a column c, and you retrieve c in a SELECT statement that uses both t1 and t2. In this case, c is ambiguous because it is not unique among the tables used in the statement. You must qualify it with a table name as t1.c or t2.c to indicate which table you mean. Similarly, to retrieve from a table t in database db1 and from a table t in database db2 in the same statement, you must refer to columns in those tables as db1.t.col_name and db2.t.col_name.

A word that follows a period in a qualified name must be an identifier, so it is not necessary to quote it, even if it is a reserved word.

The syntax .tbl_name means the table tbl_name in the default database. This syntax is accepted for ODBC compatibility because some ODBC programs prefix table names with a ‘.’ character.