https://dev.mysql.com/doc/refman/5.7/en/identifiers.html 官方文章摘要,许多细节未录,可访问此页面查看
identifiers
1. MySQL中的某个对象的名字就叫做:identifier
MySQL中的对象有:database(数据库), table(表), index(索引), column(列), alias(别名), view(视图), stored procedure(存储过程), partition(分区), tablespace(表空间),等等
数据库对象的名称就是它的标识符。
2. 是否加引号的问题
标识符identifier可以加引号也可以不加引号,但是如果有特殊字符或者保留字,必须加引号。(例外:A reserved word that follows a period in a qualified name must be an identifier, so it need not be quoted.))
MySQL中语法中标识符的引号是这样的( `) , 学名开单引号,也叫反引号(backtick),在Tab键上方,对应的ASCII码十进制为96,16进制为0x60
比如,有个表名字叫做select,这与MySQL的保留字select冲突,于是用开单引号将其引起来,如果不引起来,MYSQL将把select视为保留字,这条语句执行就会出错。所以,有MYSQL保留字作为字段的,必须加上开单引号来区分。
比如下面这个语句:
mysql> SELECT * FROM `select` WHERE `select`.id > 100
如果开启了ANSI_QUOTES SQL模式,也可以用双引号将其引起来:
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)
如果开启ANSI_QUOTES模式,那么双引号引起来的就做标识符,字符串就必须用单引号引起来,此时字符串不允许用双引号引起来。The 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.
3. 大写、小写字母的敏感性(case sensitivity)
https://dev.mysql.com/doc/refman/5.7/en/identifier-case-sensitivity.html
MySQL中,数据库(databases)对应着directories within the data directory
数据库中的每一张表(table)都对应着 at least one file within the database directory (and possibly more, depending on the storage engine)
触发器Triggers also correspond to files
P:这些文件最终还是要落实到操作系统上!
Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database, table, and trigger names.
操作系统的对大小写的敏感性在数据库的敏感性中扮演重要角色
在windows系统中 not case-sensitive的,到Linux系统中就变成case-sensitive
一个重要的例外就是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.
注意
在同一条语句中指称同一张表不要一会儿用大写一会儿用小写,尽管有些platform不是case-sensitive的
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 WHEREMY_TABLE.col=1;
表名数据库名在硬盘(disk)上存储情况是由lower_case_table_names system 这个变量值决定的,这个是在你starting mysqld的时候设置的
lower_case_table_names system 变量值,在UNIX系统中默认为0,在Windows系统中默认为1,在macOS中默认为2
4. 关键字和保留字
官网保留字地址 https://dev.mysql.com/doc/refman/5.7/en/keywords.html
5. 名字长度限制
Identifiers are stored using Unicode (UTF-8)