9.2 Schema Object Names

Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers. This section describes the permissible syntax for identifiers in MySQL. note identifier-length::, indicates the maximum length of each type of identifier. note identifier-case-sensitivity::, describes which types of identifiers are case-sensitive and under what conditions.

An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. (Exception: A reserved word that follows a period in a qualified name must be an identifier, so it need not be quoted.) Reserved words are listed at *note keywords::.

Internally, identifiers are converted to and are stored as Unicode (UTF-8). The permissible Unicode characters in identifiers are those in the Basic Multilingual Plane (BMP). Supplementary characters are not permitted. Identifiers thus may contain these characters:

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 permissible to quote identifiers within double quotation marks:

 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)

The 'ANSI_QUOTES' mode causes the server to interpret double-quoted strings as identifiers. Consequently, when this mode is enabled, string literals must be enclosed within single quotation marks. They cannot be enclosed within double quotation marks. The server SQL mode is controlled as described in *note 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);

In the select list of a query, a quoted column alias can be specified using identifier or string quoting characters:

 mysql> SELECT 1 AS `one`, 2 AS 'two';
 +-----+-----+
 | one | two |
 +-----+-----+
 |   1 |   2 |
 +-----+-----+

Elsewhere in the statement, quoted references to the alias must use identifier quoting or the reference is treated as a string literal.

It is recommended that you do not use names that begin with 'Me' or 'MeN', where M and N are integers. For example, avoid using '1e' as an identifier, 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'.

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

A user variable cannot be used directly in an SQL statement as an identifier or as part of an identifier. See *note user-variables::, for more information and examples of workarounds.

Special characters in database and table names are encoded in the corresponding file system names as described in *note identifier-mapping::. If you have databases or tables from an older version of MySQL that contain special characters and for which the underlying directory names or file names have not been updated to use the new encoding, the server displays their names with a prefix of '#mysql50#'. For information about referring to such names or converting them to the newer encoding, see that section.

 File: manual.info.tmp, Node: identifier-length, Next: identifier-qualifiers, Prev: identifiers, Up: identifiers

9.2.1 Identifier Length Limits

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

Identifier Maximum Type Length (characters)

Database 64 (*note 'NDB': mysql-cluster. storage engine: 63)

Table 64 (*note 'NDB': mysql-cluster. storage engine: 63)

Column 64

Index 64

Constraint 64

Stored 64 Program

View 64

Tablespace 64

Server 64

Log File 64 Group

Alias 256 (see exception following table)

Compound 16 Statement
Label

User-Defined64 Variable

Aliases for column names in *note 'CREATE VIEW': create-view. statements are checked against the maximum column length of 64 characters (not the maximum alias length of 256 characters).

For constraint definitions that include no constraint name, the server internally generates a name derived from the associated table name. For example, internally generated foreign key constraint names consist of the table name plus 'ibfk' and a number. If the table name is close to the length limit for constraint names, the additional characters required for the constraint name may cause that name to exceed the limit, resulting in an error.

Identifiers are stored using Unicode (UTF-8). This applies to identifiers in table definitions that are stored in '.frm' files and to identifiers stored in the grant tables in the 'mysql' database. The sizes of the identifier string columns in the grant tables are measured in characters. You can use multibyte characters without reducing the number of characters permitted for values stored in these columns.

NDB Cluster imposes a maximum length of 63 characters for names of databases and tables. See *note mysql-cluster-limitations-database-objects::.

Values such as user name and host names in MySQL account names are strings rather than identifiers. For information about the maximum length of such values as stored in grant tables, see *note grant-tables-scope-column-properties::.

 File: manual.info.tmp, Node: identifier-qualifiers, Next: identifier-case-sensitivity, Prev: identifier-length, Up: identifiers

9.2.2 Identifier Qualifiers

Object names may be unqualified or qualified. An unqualified name is permitted in contexts where interpretation of the name is unambiguous. A qualified name includes at least one qualifier to clarify the interpretive context by overriding a default context or providing missing context.

For example, this statement creates a table using the unqualified name 't1':

 CREATE TABLE t1 (i INT);

Because 't1' includes no qualifier to specify a database, the statement creates the table in the default database. If there is no default database, an error occurs.

This statement creates a table using the qualified name 'db1.t1':

 CREATE TABLE db1.t1 (i INT);

Because 'db1.t1' includes a database qualifier 'db1', the statement creates 't1' in the database named 'db1', regardless of the default database. The qualifier must be specified if there is no default database. The qualifier may be specified if there is a default database, to specify a database different from the default, or to make the database explicit if the default is the same as the one specified.

Qualifiers have these characteristics:

The permitted qualifiers for object names depend on the object type:

You need not specify a qualifier for an object reference in a statement unless the unqualified reference is ambiguous. Suppose that column 'c1' occurs only in table 't1', 'c2' only in 't2', and 'c' in both 't1' and 't2'. Any unqualified reference to 'c' is ambiguous in a statement that refers to both tables and must be qualified as 't1.c' or 't2.c' to indicate which table you mean:

 SELECT c1, c2, t1.c FROM t1 INNER JOIN t2
 WHERE t2.c > 100;

Similarly, to retrieve from a table 't' in database 'db1' and from a table 't' in database 'db2' in the same statement, you must qualify the table references: For references to columns in those tables, qualifiers are required only for column names that appear in both tables. Suppose that column 'c1' occurs only in table 'db1.t', 'c2' only in 'db2.t', and 'c' in both 'db1.t' and 'db2.t'. In this case, 'c' is ambiguous and must be qualified but 'c1' and 'c2' need not be:

 SELECT c1, c2, db1.t.c FROM db1.t INNER JOIN db2.t
 WHERE db2.t.c > 100;

Table aliases enable qualified column references to be written more simply:

 SELECT c1, c2, t1.c FROM db1.t AS t1 INNER JOIN db2.t AS t2
 WHERE t2.c > 100;

 File: manual.info.tmp, Node: identifier-case-sensitivity, Next: identifier-mapping, Prev: identifier-qualifiers, Up: identifiers

9.2.3 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). Triggers also correspond to files. Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database, table, and trigger names. This means such names are not case-sensitive in Windows, but are case-sensitive in most varieties of Unix. One notable exception is macOS, which is Unix-based but uses a default file system type (HFS+) that is not case-sensitive. However, macOS also supports UFS volumes, which are case-sensitive just as on any Unix. See *note extensions-to-ansi::. The 'lower_case_table_names' system variable also affects how the server handles identifier case sensitivity, as described later in this section.

Note:

Although database, table, and trigger names are not case-sensitive on some platforms, you should not refer to one of these using different cases within the same statement. The following statement would not work because it refers to a table both as 'my_table' and as 'MY_TABLE':

 mysql> SELECT * FROM my_table WHERE MY_TABLE.col=1;

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

However, names of logfile groups are case-sensitive. This differs from standard SQL.

By default, table aliases are case-sensitive on Unix, but not so on Windows or macOS. The following statement would not work on Unix, because it refers to the alias both as 'a' and as 'A':

 mysql> SELECT COL_NAME FROM TBL_NAME AS a
        WHERE a.COL_NAME = 1 OR A.COL_NAME = 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 'lower_case_table_names' system variable, which you can set when starting *note 'mysqld': mysqld. 'lower_case_table_names' can take the values shown in the following table. This variable does not affect case sensitivity of trigger identifiers. On Unix, the default value of 'lower_case_table_names' is 0. On Windows, the default value is 1. On macOS, the default value is 2.

Value Meaning

'0' Table and database names are stored on disk using the lettercase specified in the note 'CREATE TABLE': create-table. or note 'CREATE DATABASE': create-database. statement. Name comparisons are case-sensitive. You should not set this variable to 0 if you are running MySQL on a system that has case-insensitive file names (such as Windows or macOS). If you force this variable to 0 with '--lower-case-table-names=0' on a case-insensitive file system and access 'MyISAM' tablenames using different lettercases, index corruption may result.

'1' 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.

'2' Table and database names are stored on disk using the lettercase specified in the note 'CREATE TABLE': create-table. or note 'CREATE DATABASE': create-database. statement, but MySQL converts them to lowercase on lookup. Name comparisons are not case-sensitive. This works only on file systems that are not case-sensitive! 'InnoDB' table names and view names are stored in lowercase, as for 'lower_case_table_names=1'.

If you are using MySQL on only one platform, you do not normally have to change the 'lower_case_table_names' variable from its default value. However, you may encounter difficulties if you want to transfer tables between platforms that differ in file system case sensitivity. For example, on Unix, you can have two different tables named 'my_table' and 'MY_TABLE', 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:

If you plan to set the 'lower_case_table_names' system variable to 1 on Unix, you must first convert your old database and table names to lowercase before stopping note 'mysqld': mysqld. and restarting it with the new variable setting. To do this for an individual table, use note 'RENAME TABLE': rename-table.:

 RENAME TABLE T1 TO t1;

To convert one or more entire databases, dump them before setting 'lower_case_table_names', then drop the databases, and reload them after setting 'lower_case_table_names':

  1. Use *note 'mysqldump': mysqldump. to dump each database:

      mysqldump --databases db1 > db1.sql
      mysqldump --databases db2 > db2.sql
      ...

    Do this for each database that must be recreated.

  2. Use 'DROP DATABASE' to drop each database.

  3. Stop the server, set 'lower_case_table_names', and restart the server.

  4. Reload the dump file for each database. Because 'lower_case_table_names' is set, each database and table name is converted to lowercase as it is re-created:

      mysql < db1.sql
      mysql < db2.sql
      ...

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, procedures, functions, savepoints, stored routine parameters, stored program local variables, and plugins. It is not true for names of columns, constraints, databases, partitions, statements prepared with *note 'PREPARE': prepare, tables, triggers, users, and user-defined variables.

File system case sensitivity can affect searches in string columns of 'INFORMATION_SCHEMA' tables. For more information, see *note charset-collation-information-schema::.

 File: manual.info.tmp, Node: identifier-mapping, Next: function-resolution, Prev: identifier-case-sensitivity, Up: identifiers

9.2.4 Mapping of Identifiers to File Names

There is a correspondence between database and table identifiers and names in the file system. For the basic structure, MySQL represents each database as a directory in the data directory, and each table by one or more files in the appropriate database directory. For the table format files ('.FRM'), the data is always stored in this structure and location.

For the data and index files, the exact representation on disk is storage engine specific. These files may be stored in the same location as the 'FRM' files, or the information may be stored in a separate file. 'InnoDB' data is stored in the InnoDB data files. If you are using tablespaces with 'InnoDB', then the specific tablespace files you create are used instead.

Any character is legal in database or table identifiers except ASCII NUL ('X'00''). MySQL encodes any characters that are problematic in the corresponding file system objects when it creates database directories or table files:

On Windows, some names such as 'nul', 'prn', and 'aux' are encoded by appending '@@@' to the name when the server creates the corresponding file or directory. This occurs on all platforms for portability of the corresponding database object between platforms.

If you have databases or tables from a version of MySQL older than 5.1.6 that contain special characters and for which the underlying directory names or file names have not been updated to use the new encoding, the server displays their names with a prefix of '#mysql50#' in the output from 'INFORMATION_SCHEMA' tables or note 'SHOW': show. statements. For example, if you have a table named 'a@b' and its name encoding has not been updated, note 'SHOW TABLES': show-tables. displays it like this:

 mysql> SHOW TABLES;
 +----------------+
 | Tables_in_test |
 +----------------+
 | #mysql50#a@b   |
 +----------------+

To refer to such a name for which the encoding has not been updated, you must supply the '#mysql50#' prefix:

 mysql> SHOW COLUMNS FROM `a@b`;
 ERROR 1146 (42S02): Table 'test.a@b' doesn't exist

 mysql> SHOW COLUMNS FROM `#mysql50#a@b`;
 +-------+---------+------+-----+---------+-------+
 | Field | Type    | Null | Key | Default | Extra |
 +-------+---------+------+-----+---------+-------+
 | i     | int(11) | YES  |     | NULL    |       |
 +-------+---------+------+-----+---------+-------+

To update old names to eliminate the need to use the special prefix to refer to them, re-encode them with *note 'mysqlcheck': mysqlcheck. The following commands update all names to the new encoding:

 mysqlcheck --check-upgrade --all-databases
 mysqlcheck --fix-db-names --fix-table-names --all-databases

To check only specific databases or tables, omit '--all-databases' and provide the appropriate database or table arguments. For information about note 'mysqlcheck': mysqlcheck. invocation syntax, see note mysqlcheck::.

Note:

The '#mysql50#' prefix is intended only to be used internally by the server. You should not create databases or tables with names that use this prefix.

Also, note 'mysqlcheck': mysqlcheck. cannot fix names that contain literal instances of the '@' character that is used for encoding special characters. If you have databases or tables that contain this character, use note 'mysqldump': mysqldump. to dump them before upgrading to MySQL 5.1.6 or later, and then reload the dump file after upgrading.

Note:

Conversion of pre-MySQL 5.1 database names containing special characters to 5.1 format with the addition of a '#mysql50#' prefix is deprecated; expect it to be removed in a future version of MySQL. Because such conversions are deprecated, the '--fix-db-names' and '--fix-table-names' options for note 'mysqlcheck': mysqlcheck. and the 'UPGRADE DATA DIRECTORY NAME' clause for the note 'ALTER DATABASE': alter-database. statement are also deprecated.

Upgrades are supported only from one release series to another (for example, 5.0 to 5.1, or 5.1 to 5.5), so there should be little remaining need for conversion of older 5.0 database names to current versions of MySQL. As a workaround, upgrade a MySQL 5.0 installation to MySQL 5.1 before upgrading to a more recent release.

 File: manual.info.tmp, Node: function-resolution, Prev: identifier-mapping, Up: identifiers

9.2.5 Function Name Parsing and Resolution

MySQL supports built-in (native) functions, loadable functions, 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.

Built-In Function Name Parsing

The parser uses default rules for parsing names of built-in functions. These rules can be changed by enabling the 'IGNORE_SPACE' SQL mode.

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 nonexpression reference to an identifier such as a table or column name. For example, in the following statements, the first reference to 'count' is a function call, whereas the second reference is a table name:

 SELECT COUNT(*) FROM mytable;
 CREATE TABLE count (i INT);

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 nonexpression context, function names are permitted as identifiers.

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 nonexpression context:

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. 'COUNT' is one such name. The 'sql/lex.h' source file lists the names of these special functions for which following whitespace determines their interpretation: names defined by the 'SYM_FN()' macro in the 'symbols[]' array.

The following list names the functions in MySQL 5.7 that are affected by the 'IGNORE_SPACE' setting and listed as special in the 'sql/lex.h' source file. You may find it easiest to treat the no-whitespace requirement as applying to all function calls.

For functions not listed as special in 'sql/lex.h', whitespace does not matter. They are interpreted as function calls only when used in expression context and may be used freely as identifiers otherwise. 'ASCII' is one such name. However, for these nonaffected function names, interpretation may vary in expression context: 'FUNC_NAME ()' is interpreted as a built-in function if there is one with the given name; if not, 'FUNC_NAME ()' is interpreted as a loadable function or stored function if one exists with that name.

The 'IGNORE_SPACE' SQL mode can be used to modify how the parser treats function names that are whitespace-sensitive:

To enable the 'IGNORE_SPACE' SQL mode, use this statement:

 SET sql_mode = 'IGNORE_SPACE';

'IGNORE_SPACE' is also enabled by certain other composite modes such as 'ANSI' that include it in their value:

 SET sql_mode = 'ANSI';

Check *note sql-mode::, to see which composite modes enable 'IGNORE_SPACE'.

To minimize the dependency of SQL code on the 'IGNORE_SPACE' setting, use these guidelines:

Function Name Resolution

The following rules describe how the server resolves references to function names for function creation and invocation:

The preceding function name resolution rules have implications for upgrading to versions of MySQL that implement new built-in functions:

 File: manual.info.tmp, Node: keywords, Next: user-variables, Prev: identifiers, Up: language-structure