When you write SQL statements, you use names to refer to databases and tables as well as to elements of tables such as columns and (sometimes) indexes. It's also possible to create aliases, which act as synonyms for table and column names. All of these types of names are known as identifiers; they identify a specific database element. This section describes the rules for writing identifiers. 4.4.1 Legal Characters for IdentifiersIdentifiers for databases, tables, columns, and indexes may be unquoted or quoted. An unquoted identifier must follow these rules:
An identifier may be quoted, in which case it can contain characters such as spaces or dashes that aren't otherwise legal. To quote an identifier, you may enclose it within backtick (´) characters. If the server was started with the --ansi or --sql-mode=ANSI_QUOTES option, you may also quote an identifier by enclosing it within double quotes ( "). Quoting causes the identifier syntax rules to be relaxed as follows:
An alias identifier can include any character, but should be quoted if it's a reserved word (such as SELECT or DESC), contains special characters, or consists entirely of digits. Aliases may be quoted within single quotes ( '), double quotes, or backticks. 4.4.2 Using Qualifiers for Table and Column NamesColumn and table identifiers can be written in qualified form—that is, together with the identifier of a higher-level element, with a period (.) separator. A table name may be qualified with the name of the database to which it belongs. For example, the Country table in the world database may be referred to as world.Country (note the . separating the two identifiers in the name). If world is the default database, these statements are equivalent: SELECT * FROM Country; SELECT * FROM world.Country; A column name may be qualified with the name of the table to which it belongs. For example, the Name column in the Country table may be referred to as Country.Name. A further level of column qualification is possible because a table name may be qualified with a database name. So, another way to refer to the Name column is world.Country.Name. If world is the default database, the following statements are equivalent. They differ only in having successively more specific levels of name qualification: SELECT Name FROM Country; SELECT Country.Name FROM Country; SELECT world.Country.Name FROM world.Country; Sometimes qualifiers are necessary to resolve ambiguity. Other times you may elect to use them to make a statement clearer or more precise.
blog comments powered by Disqus |