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

MySQL 5.0

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

Database, table, index, column, and alias names are identifiers. This section describes the allowable syntax for identifiers in MySQL.

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 () 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.

  • 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 are stored in files and to identifiers stored in the grant tables in the 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, 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 ‘’.

The identifier quote character is the backtick (‘’):

mysql> 

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

mysql> 
ERROR 1064: You have an error in your SQL syntax. (...)
mysql> 
mysql> 
Query OK, 0 rows affected (0.00 sec)

Note: Because the 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 that contains a column named :

mysql> 

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

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

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
The column from whichever table used in the statement contains a column of that name.
The column from table of the default database.
The column from table of the database .

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

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

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 means the table in the default database. This syntax is accepted for ODBC compatibility because some ODBC programs prefix table names with a ‘’ character.

9.2.2. Identifier Case Sensitivity

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 determines the case sensitivity of database and table names. This means database and table names are case sensitive in most varieties of Unix, and not case sensitive in Windows. 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 Section 1.9.4, “MySQL Extensions to Standard SQL”. The system variable also affects how the server handles identifier case sensitivity, as described later in this section.

Note: 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 and as :

mysql> 

Column, index and stored routine names are not case sensitive on any platform, nor are column aliases. Trigger names are case sensitive.

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 and as :

mysql>  FROM  AS a
    ->  = 1 OR A. = 2;

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.

How table and database names are stored on disk and used in MySQL is affected by the system variable, which you can set when starting mysqld. can take the values shown in the following table. On Unix, the default value of is 0. On Windows the default value is 1. On Mac OS X, the default value is 2.

Value Meaning
Table and database names are stored on disk using the lettercase specified in the or statement. Name comparisons are case sensitive. Note that if you force this variable to 0 with on a case-insensitive filesystem and access tablenames using different lettercases, index corruption may result.
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.
Table and database names are stored on disk using the lettercase specified in the or statement, but MySQL converts them to lowercase on lookup. Name comparisons are not case sensitive. Note: This works only on filesystems that are not case sensitive! table names are stored in lowercase, as for .

If you are using MySQL on only one platform, you don't normally have to change the variable. 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 and , but on Windows these two names are considered identical. To avoid data transfer problems stemming from lettercase of database or table names, you have two options:

  • Use on all systems. The main disadvantage with this is that when you use or , you don't see the names in their original lettercase.

  • Use on Unix and 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.

    Exception: If you are using tables, you should set to 1 on all platforms to force names to be converted to lowercase.

Note that if you plan to set the system variable to 1 on Unix, you must first convert your old database and table names to lowercase before restarting mysqld with the new variable setting.