关键字是在 SQL 中具有重要意义的词。某些关键字(如SELECT
、
DELETE
或
BIGINT
)是保留关键字,需要特殊处理才能用作表名和列名等标识符。这也可能适用于内置函数的名称。
允许非保留关键字作为标识符而不用引号。如果您按照第 9.2 节,“模式对象名称”中的描述引用保留字,则允许它们作为标识符:
mysql> CREATE TABLE interval (begin INT, end INT);
ERROR 1064 (42000): You have an error in your SQL syntax ...
near 'interval (begin INT, end INT)'
BEGIN
并且END
是关键字但不是保留的,因此它们用作标识符不需要引用。INTERVAL
是保留关键字,必须用引号引起来用作标识符:
mysql> CREATE TABLE `interval` (begin INT, end INT);
Query OK, 0 rows affected (0.01 sec)
例外:限定名称中句点后面的单词必须是标识符,因此即使保留也不需要引用:
mysql> CREATE TABLE mydb.interval (begin INT, end INT);
Query OK, 0 rows affected (0.01 sec)
允许将内置函数的名称用作标识符,但可能需要谨慎使用。例如,
COUNT
可以作为列名。(
但是,默认情况下,函数调用中函数名称和后续字符之间不允许有空格。此要求使解析器能够区分该名称是在函数调用中还是在非函数上下文中使用。有关函数名称识别的更多详细信息,请参阅
第 9.2.5 节,“函数名称解析和解析”。
以下列表显示了 MySQL 5.6 中的关键字和保留字,以及各个版本对各个字的更改。保留关键字标有 (R)。此外,_FILENAME
保留。
在某些时候,您可能会升级到更高版本,因此最好也看看未来的保留字。您可以在涵盖更高版本 MySQL 的手册中找到它们。标准 SQL 禁止列表中的大多数保留字作为列名或表名(例如,
GROUP
)。一些是保留的,因为 MySQL 需要它们并使用yacc解析器。
一个 | 乙 | 丙 | D | 乙 | 女 | 格 | H | 我 | 杰 | K | 大号 | 男 | 否 | 欧 | 磷 | 问 | 右 | 年代 | 吨 | ü | 五 | 瓦 | × | 是 | Z
ACCESSIBLE
(右)ACTION
ADD
(右)AFTER
AGAINST
AGGREGATE
ALGORITHM
ALL
(右)ALTER
(右)ANALYSE
; 5.6.6新增(非保留)ANALYZE
(右)AND
(右)ANY
AS
(右)ASC
(右)ASCII
ASENSITIVE
(右)AT
AUTHORS
; 在 5.6.8 中移除AUTOEXTEND_SIZE
AUTO_INCREMENT
AVG
AVG_ROW_LENGTH
BACKUP
BEFORE
(右)BEGIN
BETWEEN
(右)BIGINT
(右)BINARY
(右)BINLOG
BIT
BLOB
(右)BLOCK
BOOL
BOOLEAN
BOTH
(右)BTREE
BY
(右)BYTE
CACHE
CALL
(右)CASCADE
(右)CASCADED
CASE
(右)CATALOG_NAME
CHAIN
CHANGE
(右)CHANGED
CHAR
(右)CHARACTER
(右)CHARSET
CHECK
(右)CHECKSUM
CIPHER
CLASS_ORIGIN
CLIENT
CLOSE
COALESCE
CODE
COLLATE
(右)COLLATION
COLUMN
(右)COLUMNS
COLUMN_FORMAT
; 5.6.6新增(非保留)COLUMN_NAME
COMMENT
COMMIT
COMMITTED
COMPACT
COMPLETION
COMPRESSED
CONCURRENT
CONDITION
(右)CONNECTION
CONSISTENT
CONSTRAINT
(右)CONSTRAINT_CATALOG
CONSTRAINT_NAME
CONSTRAINT_SCHEMA
CONTAINS
CONTEXT
CONTINUE
(右)CONTRIBUTORS
; 在 5.6.8 中移除CONVERT
(右)CPU
CREATE
(右)CROSS
(右)CUBE
CURRENT
; 5.6.4新增(非保留)CURRENT_DATE
(右)CURRENT_TIME
(右)CURRENT_TIMESTAMP
(右)CURRENT_USER
(右)CURSOR
(右)CURSOR_NAME
DATA
DATABASE
(右)DATABASES
(右)DATAFILE
DATE
DATETIME
DAY
DAY_HOUR
(右)DAY_MICROSECOND
(右)DAY_MINUTE
(右)DAY_SECOND
(右)DEALLOCATE
DEC
(右)DECIMAL
(右)DECLARE
(右)DEFAULT
(右)DEFAULT_AUTH
; 5.6.4新增(非保留)DEFINER
DELAYED
(右)DELAY_KEY_WRITE
DELETE
(右)DESC
(右)DESCRIBE
(右)DES_KEY_FILE
DETERMINISTIC
(右)DIAGNOSTICS
; 5.6.4新增(非保留)DIRECTORY
DISABLE
DISCARD
DISK
DISTINCT
(右)DISTINCTROW
(右)DIV
(右)DO
DOUBLE
(右)DROP
(右)DUAL
(右)DUMPFILE
DUPLICATE
DYNAMIC
EACH
(右)ELSE
(右)ELSEIF
(右)ENABLE
ENCLOSED
(右)END
ENDS
ENGINE
ENGINES
ENUM
ERROR
ERRORS
ESCAPE
ESCAPED
(右)EVENT
EVENTS
EVERY
EXCHANGE
EXECUTE
EXISTS
(右)EXIT
(右)EXPANSION
EXPIRE
; 5.6.6新增(非保留)EXPLAIN
(右)EXPORT
; 5.6.6新增(非保留)EXTENDED
EXTENT_SIZE
FALSE
(右)FAST
FAULTS
FETCH
(右)FIELDS
FILE
FIRST
FIXED
FLOAT
(右)FLOAT4
(右)FLOAT8
(右)FLUSH
FOR
(右)FORCE
(右)FOREIGN
(右)FORMAT
; 5.6.5新增(非保留)FOUND
FROM
(右)FULL
FULLTEXT
(右)FUNCTION
GENERAL
; 在 5.6.1 中成为非保留的GEOMETRY
GEOMETRYCOLLECTION
GET
(R); 5.6.4新增(保留)GET_FORMAT
GLOBAL
GRANT
(右)GRANTS
GROUP
(右)
HANDLER
HASH
HAVING
(右)HELP
HIGH_PRIORITY
(右)HOST
HOSTS
HOUR
HOUR_MICROSECOND
(右)HOUR_MINUTE
(右)HOUR_SECOND
(右)
IDENTIFIED
IF
(右)IGNORE
(右)IGNORE_SERVER_IDS
; 在 5.6.1 中成为非保留的IMPORT
IN
(右)INDEX
(右)INDEXES
INFILE
(右)INITIAL_SIZE
INNER
(右)INOUT
(右)INSENSITIVE
(右)INSERT
(右)INSERT_METHOD
INSTALL
INT
(右)INT1
(右)INT2
(右)INT3
(右)INT4
(右)INT8
(右)INTEGER
(右)INTERVAL
(右)INTO
(右)INVOKER
IO
IO_AFTER_GTIDS
(R); 5.6.5新增(保留)IO_BEFORE_GTIDS
(R); 5.6.5新增(保留)IO_THREAD
IPC
IS
(右)ISOLATION
ISSUER
ITERATE
(右)
JOIN
(右)
KEY
(右)KEYS
(右)KEY_BLOCK_SIZE
KILL
(右)
LANGUAGE
LAST
LEADING
(右)LEAVE
(右)LEAVES
LEFT
(右)LESS
LEVEL
LIKE
(右)LIMIT
(右)LINEAR
(右)LINES
(右)LINESTRING
LIST
LOAD
(右)LOCAL
LOCALTIME
(右)LOCALTIMESTAMP
(右)LOCK
(右)LOCKS
LOGFILE
LOGS
LONG
(右)LONGBLOB
(右)LONGTEXT
(右)LOOP
(右)LOW_PRIORITY
(右)
MASTER
MASTER_AUTO_POSITION
; 5.6.5新增(非保留)MASTER_BIND
(R); 5.6.1新增(保留)MASTER_CONNECT_RETRY
MASTER_DELAY
MASTER_HEARTBEAT_PERIOD
; 在 5.6.1 中成为非保留的MASTER_HOST
MASTER_LOG_FILE
MASTER_LOG_POS
MASTER_PASSWORD
MASTER_PORT
MASTER_RETRY_COUNT
; 5.6.1新增(非保留)MASTER_SERVER_ID
MASTER_SSL
MASTER_SSL_CA
MASTER_SSL_CAPATH
MASTER_SSL_CERT
MASTER_SSL_CIPHER
MASTER_SSL_CRL
; 5.6.3新增(非保留)MASTER_SSL_CRLPATH
; 5.6.3新增(非保留)MASTER_SSL_KEY
MASTER_SSL_VERIFY_SERVER_CERT
(右)MASTER_USER
MATCH
(右)MAXVALUE
(右)MAX_CONNECTIONS_PER_HOUR
MAX_QUERIES_PER_HOUR
MAX_ROWS
MAX_SIZE
MAX_UPDATES_PER_HOUR
MAX_USER_CONNECTIONS
MEDIUM
MEDIUMBLOB
(右)MEDIUMINT
(右)MEDIUMTEXT
(右)MEMORY
MERGE
MESSAGE_TEXT
MICROSECOND
MIDDLEINT
(右)MIGRATE
MINUTE
MINUTE_MICROSECOND
(右)MINUTE_SECOND
(右)MIN_ROWS
MOD
(右)MODE
MODIFIES
(右)MODIFY
MONTH
MULTILINESTRING
MULTIPOINT
MULTIPOLYGON
MUTEX
MYSQL_ERRNO
NAME
NAMES
NATIONAL
NATURAL
(右)NCHAR
NDB
NDBCLUSTER
NEW
NEXT
NO
NODEGROUP
NONE
NOT
(右)NO_WAIT
NO_WRITE_TO_BINLOG
(右)NULL
(右)NUMBER
; 5.6.4新增(非保留)NUMERIC
(右)NVARCHAR
OFFSET
OLD_PASSWORD
ON
(右)ONE
ONE_SHOT
; 在 5.6.1 中保留;在 5.6.5 中移除ONLY
; 5.6.5新增(非保留)OPEN
OPTIMIZE
(右)OPTION
(右)OPTIONALLY
(右)OPTIONS
OR
(右)ORDER
(右)OUT
(右)OUTER
(右)OUTFILE
(右)OWNER
PACK_KEYS
PAGE
PARSER
PARTIAL
PARTITION
(R); 在 5.6.2 中保留PARTITIONING
PARTITIONS
PASSWORD
PHASE
PLUGIN
PLUGINS
PLUGIN_DIR
; 5.6.4新增(非保留)POINT
POLYGON
PORT
PRECISION
(右)PREPARE
PRESERVE
PREV
PRIMARY
(右)PRIVILEGES
PROCEDURE
(右)PROCESSLIST
PROFILE
PROFILES
PROXY
; 5.6.1新增(非保留)PURGE
(右)
QUARTER
QUERY
QUICK
RANGE
(右)READ
(右)READS
(右)READ_ONLY
READ_WRITE
(右)REAL
(右)REBUILD
RECOVER
REDOFILE
REDO_BUFFER_SIZE
REDUNDANT
REFERENCES
(右)REGEXP
(右)RELAY
RELAYLOG
RELAY_LOG_FILE
RELAY_LOG_POS
RELAY_THREAD
RELEASE
(右)RELOAD
REMOVE
RENAME
(右)REORGANIZE
REPAIR
REPEAT
(右)REPEATABLE
REPLACE
(右)REPLICATION
REQUIRE
(右)RESET
RESIGNAL
(右)RESTORE
RESTRICT
(右)RESUME
RETURN
(右)RETURNED_SQLSTATE
; 5.6.4新增(非保留)RETURNS
REVERSE
REVOKE
(右)RIGHT
(右)RLIKE
(右)ROLLBACK
ROLLUP
ROUTINE
ROW
ROWS
ROW_COUNT
; 5.6.4新增(非保留)ROW_FORMAT
RTREE
SAVEPOINT
SCHEDULE
SCHEMA
(右)SCHEMAS
(右)SCHEMA_NAME
SECOND
SECOND_MICROSECOND
(右)SECURITY
SELECT
(右)SENSITIVE
(右)SEPARATOR
(右)SERIAL
SERIALIZABLE
SERVER
SESSION
SET
(右)SHARE
SHOW
(右)SHUTDOWN
SIGNAL
(右)SIGNED
SIMPLE
SLAVE
SLOW
; 在 5.6.1 中成为非保留的SMALLINT
(右)SNAPSHOT
SOCKET
SOME
SONAME
SOUNDS
SOURCE
SPATIAL
(右)SPECIFIC
(右)SQL
(右)SQLEXCEPTION
(右)SQLSTATE
(右)SQLWARNING
(右)SQL_AFTER_GTIDS
; 5.6.5新增(预留);在 5.6.6 中成为非保留的SQL_AFTER_MTS_GAPS
; 5.6.6新增(非保留)SQL_BEFORE_GTIDS
; 5.6.5新增(预留);在 5.6.6 中成为非保留的SQL_BIG_RESULT
(右)SQL_BUFFER_RESULT
SQL_CACHE
SQL_CALC_FOUND_ROWS
(右)SQL_NO_CACHE
SQL_SMALL_RESULT
(右)SQL_THREAD
SQL_TSI_DAY
SQL_TSI_HOUR
SQL_TSI_MINUTE
SQL_TSI_MONTH
SQL_TSI_QUARTER
SQL_TSI_SECOND
SQL_TSI_WEEK
SQL_TSI_YEAR
SSL
(右)START
STARTING
(右)STARTS
STATS_AUTO_RECALC
; 5.6.6新增(非保留)STATS_PERSISTENT
; 5.6.6新增(非保留)STATS_SAMPLE_PAGES
; 5.6.6新增(非保留)STATUS
STOP
STORAGE
STRAIGHT_JOIN
(右)STRING
SUBCLASS_ORIGIN
SUBJECT
SUBPARTITION
SUBPARTITIONS
SUPER
SUSPEND
SWAPS
SWITCHES
TABLE
(右)TABLES
TABLESPACE
TABLE_CHECKSUM
TABLE_NAME
TEMPORARY
TEMPTABLE
TERMINATED
(右)TEXT
THAN
THEN
(右)TIME
TIMESTAMP
TIMESTAMPADD
TIMESTAMPDIFF
TINYBLOB
(右)TINYINT
(右)TINYTEXT
(右)TO
(右)TRAILING
(右)TRANSACTION
TRIGGER
(右)TRIGGERS
TRUE
(右)TRUNCATE
TYPE
TYPES
UNCOMMITTED
UNDEFINED
UNDO
(右)UNDOFILE
UNDO_BUFFER_SIZE
UNICODE
UNINSTALL
UNION
(右)UNIQUE
(右)UNKNOWN
UNLOCK
(右)UNSIGNED
(右)UNTIL
UPDATE
(右)UPGRADE
USAGE
(右)USE
(右)USER
USER_RESOURCES
USE_FRM
USING
(右)UTC_DATE
(右)UTC_TIME
(右)UTC_TIMESTAMP
(右)
VALUE
VALUES
(右)VARBINARY
(右)VARCHAR
(右)VARCHARACTER
(右)VARIABLES
VARYING
(右)VIEW
WAIT
WARNINGS
WEEK
WEIGHT_STRING
WHEN
(右)WHERE
(右)WHILE
(右)WITH
(右)WORK
WRAPPER
WRITE
(右)
X509
XA
XML
XOR
(右)
YEAR
YEAR_MONTH
(右)
ZEROFILL
(右)
以下列表显示了 MySQL 5.6 中添加的关键字和保留字,与 MySQL 5.5 相比。保留关键字标有 (R)。
一个 | 丙 | D | 乙 | 女 | 格 | 我 | 男 | 否 | 欧 | 磷 | 右 | 年代 | W
ANALYSE
COLUMN_FORMAT
CURRENT
DEFAULT_AUTH
DIAGNOSTICS
EXCHANGE
EXPIRE
EXPORT
FORMAT
GET
(右)
IO_AFTER_GTIDS
(右)IO_BEFORE_GTIDS
(右)
MASTER_AUTO_POSITION
MASTER_BIND
(右)MASTER_DELAY
MASTER_RETRY_COUNT
MASTER_SSL_CRL
MASTER_SSL_CRLPATH
NUMBER
ONLY
PLUGIN_DIR
RETURNED_SQLSTATE
REVERSE
ROW_COUNT
SQL_AFTER_GTIDS
SQL_AFTER_MTS_GAPS
SQL_BEFORE_GTIDS
STATS_AUTO_RECALC
STATS_PERSISTENT
STATS_SAMPLE_PAGES
WEIGHT_STRING