diff --git a/update schema-object-names.md b/update schema-object-names.md new file mode 100644 index 0000000000000..23e2ef3057aa4 --- /dev/null +++ b/update schema-object-names.md @@ -0,0 +1,96 @@ +--- +title: Schema 对象名 +category: reference +summary: 本文介绍 TiDB SQL 语句中的模式对象名。 +aliases: ['/docs-cn/dev/reference/sql/language-structure/schema-object-names/'] +--- + +# Schema Object Names + + + +This article introduces schema object names in TiDB SQL statements. + +Schema object names are used to name all schema objects in TiDB, including database, table, index, column, alias, etc. You can quote these objects by identifiers in SQL statements. + +To quote, you can use the backtick ``(`)`` to wrap the identifier, ie `SELECT * FROM t` can also be written as`` SELECT * FROM `t` ``. But if an identifier contains at least a special character or is a reserved keyword, you must quote it whenever you refer to it. + +{{< copyable "sql" >}} + +```sql +SELECT * FROM `table` WHERE `table`.id = 20; +``` + +If the `ANSI_QUOTES` SQL mode is enabled, you can also quote identifiers within double quotation marks`"`. + +```sql +MySQL [test]> CREATE TABLE "test" (a varchar(10)); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 19 near ""test" (a varchar(10))" + +MySQL [test]> SET SESSION sql_mode='ANSI_QUOTES'; +Query OK, 0 rows affected (0.000 sec) + +MySQL [test]> CREATE TABLE "test" (a varchar(10)); +Query OK, 0 rows affected (0.012 sec) +``` + +If you want to use the backtick character in the quoted identifier, you need to repeat the backtick twice, for example to create a table a`b: + +{{< copyable "sql" >}} + +```sql +CREATE TABLE `a``b` (a int); +``` + +In a `SELECT` statement, the alias part can use an identifier or a string: + +{{< copyable "sql" >}} + +```sql +SELECT 1 AS `identifier`, 2 AS 'string'; +``` + +``` ++------------+--------+ +| identifier | string | ++------------+--------+ +| 1 | 2 | ++------------+--------+ +1 row in set (0.00 sec) +``` + +For more information, see [MySQL Documentation](https://dev.mysql.com/doc/refman/5.7/en/identifiers.html). + +## Identifier Qualifiers + +Object names can be unqualified or qualified. For example, the following statement creates a table using the unqualified name: + +{{< copyable "sql" >}} + +```sql +CREATE TABLE t (i int); +``` + +If you have not used the `USE` or connection parameter to set the database, the `ERROR 1046 (3D000): No database selected` is displayed. At this time you can specify the database qualified name: + +{{< copyable "sql" >}} + +```sql +CREATE TABLE test.t (i int); +``` + +There can be white spaces around `.`, and `table_name.col_name` and `table_name . col_name` are equivalent. + +To quote this identifier, use: + +``` +`table_name`.`col_name` +``` + +Instead of: + +``` +`table_name.col_name` +``` + +For more information, see [MySQL Documentation](https://dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html). \ No newline at end of file