关键字是在 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.7 中的关键字和保留字,以及各个版本对各个字的更改。保留关键字标有 (R)。此外,_FILENAME
保留。
在某些时候,您可能会升级到更高版本,因此最好也看看未来的保留字。您可以在涵盖更高版本 MySQL 的手册中找到它们。标准 SQL 禁止列表中的大多数保留字作为列名或表名(例如,
GROUP
)。一些是保留的,因为 MySQL 需要它们并使用yacc解析器。
一个 | 乙 | 丙 | D | 乙 | 女 | 格 | H | 我 | 杰 | K | 大号 | 男 | 否 | 欧 | 磷 | 问 | 右 | 年代 | 吨 | ü | 五 | 瓦 | × | 是 | Z
ACCESSIBLE
(右)ACCOUNT
; 5.7.6新增(非保留)ACTION
ADD
(右)AFTER
AGAINST
AGGREGATE
ALGORITHM
ALL
(右)ALTER
(右)ALWAYS
; 5.7.6新增(非保留)ANALYSE
ANALYZE
(右)AND
(右)ANY
AS
(右)ASC
(右)ASCII
ASENSITIVE
(右)AT
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
CHANNEL
; 5.7.6新增(非保留)CHAR
(右)CHARACTER
(右)CHARSET
CHECK
(右)CHECKSUM
CIPHER
CLASS_ORIGIN
CLIENT
CLOSE
COALESCE
CODE
COLLATE
(右)COLLATION
COLUMN
(右)COLUMNS
COLUMN_FORMAT
COLUMN_NAME
COMMENT
COMMIT
COMMITTED
COMPACT
COMPLETION
COMPRESSED
COMPRESSION
; 5.7.8新增(非保留)CONCURRENT
CONDITION
(右)CONNECTION
CONSISTENT
CONSTRAINT
(右)CONSTRAINT_CATALOG
CONSTRAINT_NAME
CONSTRAINT_SCHEMA
CONTAINS
CONTEXT
CONTINUE
(右)CONVERT
(右)CPU
CREATE
(右)CROSS
(右)CUBE
CURRENT
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
DEFINER
DELAYED
(右)DELAY_KEY_WRITE
DELETE
(右)DESC
(右)DESCRIBE
(右)DES_KEY_FILE
DETERMINISTIC
(右)DIAGNOSTICS
DIRECTORY
DISABLE
DISCARD
DISK
DISTINCT
(右)DISTINCTROW
(右)DIV
(右)DO
DOUBLE
(右)DROP
(右)DUAL
(右)DUMPFILE
DUPLICATE
DYNAMIC
EACH
(右)ELSE
(右)ELSEIF
(右)ENABLE
ENCLOSED
(右)ENCRYPTION
; 5.7.11 新增(非保留)END
ENDS
ENGINE
ENGINES
ENUM
ERROR
ERRORS
ESCAPE
ESCAPED
(右)EVENT
EVENTS
EVERY
EXCHANGE
EXECUTE
EXISTS
(右)EXIT
(右)EXPANSION
EXPIRE
EXPLAIN
(右)EXPORT
EXTENDED
EXTENT_SIZE
FALSE
(右)FAST
FAULTS
FETCH
(右)FIELDS
FILE
FILE_BLOCK_SIZE
; 5.7.6新增(非保留)FILTER
; 5.7.3新增(非保留)FIRST
FIXED
FLOAT
(右)FLOAT4
(右)FLOAT8
(右)FLUSH
FOLLOWS
; 5.7.2新增(非保留)FOR
(右)FORCE
(右)FOREIGN
(右)FORMAT
FOUND
FROM
(右)FULL
FULLTEXT
(右)FUNCTION
GENERAL
GENERATED
(R); 5.7.6新增(保留)GEOMETRY
GEOMETRYCOLLECTION
GET
(右)GET_FORMAT
GLOBAL
GRANT
(右)GRANTS
GROUP
(右)GROUP_REPLICATION
; 5.7.6新增(非保留)
HANDLER
HASH
HAVING
(右)HELP
HIGH_PRIORITY
(右)HOST
HOSTS
HOUR
HOUR_MICROSECOND
(右)HOUR_MINUTE
(右)HOUR_SECOND
(右)
IDENTIFIED
IF
(右)IGNORE
(右)IGNORE_SERVER_IDS
IMPORT
IN
(右)INDEX
(右)INDEXES
INFILE
(右)INITIAL_SIZE
INNER
(右)INOUT
(右)INSENSITIVE
(右)INSERT
(右)INSERT_METHOD
INSTALL
INSTANCE
; 5.7.11 新增(非保留)INT
(右)INT1
(右)INT2
(右)INT3
(右)INT4
(右)INT8
(右)INTEGER
(右)INTERVAL
(右)INTO
(右)INVOKER
IO
IO_AFTER_GTIDS
(右)IO_BEFORE_GTIDS
(右)IO_THREAD
IPC
IS
(右)ISOLATION
ISSUER
ITERATE
(右)
JOIN
(右)JSON
; 5.7.8新增(非保留)
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
MASTER_BIND
(右)MASTER_CONNECT_RETRY
MASTER_DELAY
MASTER_HEARTBEAT_PERIOD
MASTER_HOST
MASTER_LOG_FILE
MASTER_LOG_POS
MASTER_PASSWORD
MASTER_PORT
MASTER_RETRY_COUNT
MASTER_SERVER_ID
MASTER_SSL
MASTER_SSL_CA
MASTER_SSL_CAPATH
MASTER_SSL_CERT
MASTER_SSL_CIPHER
MASTER_SSL_CRL
MASTER_SSL_CRLPATH
MASTER_SSL_KEY
MASTER_SSL_VERIFY_SERVER_CERT
(右)MASTER_TLS_VERSION
; 5.7.10 新增(非保留)MASTER_USER
MATCH
(右)MAXVALUE
(右)MAX_CONNECTIONS_PER_HOUR
MAX_QUERIES_PER_HOUR
MAX_ROWS
MAX_SIZE
MAX_STATEMENT_TIME
; 5.7.4新增(非保留);在 5.7.8 中移除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
NEVER
; 5.7.4新增(非保留)NEW
NEXT
NO
NODEGROUP
NONBLOCKING
; 在 5.7.6 中移除NONE
NOT
(右)NO_WAIT
NO_WRITE_TO_BINLOG
(右)NULL
(右)NUMBER
NUMERIC
(右)NVARCHAR
OFFSET
OLD_PASSWORD
; 在 5.7.5 中移除ON
(右)ONE
ONLY
OPEN
OPTIMIZE
(右)OPTIMIZER_COSTS
(R); 5.7.5新增(保留)OPTION
(右)OPTIONALLY
(右)OPTIONS
OR
(右)ORDER
(右)OUT
(右)OUTER
(右)OUTFILE
(右)OWNER
PACK_KEYS
PAGE
PARSER
PARSE_GCOL_EXPR
; 5.7.6新增(预留);在 5.7.8 中成为非保留的PARTIAL
PARTITION
(右)PARTITIONING
PARTITIONS
PASSWORD
PHASE
PLUGIN
PLUGINS
PLUGIN_DIR
POINT
POLYGON
PORT
PRECEDES
; 5.7.2新增(非保留)PRECISION
(右)PREPARE
PRESERVE
PREV
PRIMARY
(右)PRIVILEGES
PROCEDURE
(右)PROCESSLIST
PROFILE
PROFILES
PROXY
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
(右)REPLICATE_DO_DB
; 5.7.3新增(非保留)REPLICATE_DO_TABLE
; 5.7.3新增(非保留)REPLICATE_IGNORE_DB
; 5.7.3新增(非保留)REPLICATE_IGNORE_TABLE
; 5.7.3新增(非保留)REPLICATE_REWRITE_DB
; 5.7.3新增(非保留)REPLICATE_WILD_DO_TABLE
; 5.7.3新增(非保留)REPLICATE_WILD_IGNORE_TABLE
; 5.7.3新增(非保留)REPLICATION
REQUIRE
(右)RESET
RESIGNAL
(右)RESTORE
RESTRICT
(右)RESUME
RETURN
(右)RETURNED_SQLSTATE
RETURNS
REVERSE
REVOKE
(右)RIGHT
(右)RLIKE
(右)ROLLBACK
ROLLUP
ROTATE
; 5.7.11 新增(非保留)ROUTINE
ROW
ROWS
ROW_COUNT
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
SMALLINT
(右)SNAPSHOT
SOCKET
SOME
SONAME
SOUNDS
SOURCE
SPATIAL
(右)SPECIFIC
(右)SQL
(右)SQLEXCEPTION
(右)SQLSTATE
(右)SQLWARNING
(右)SQL_AFTER_GTIDS
SQL_AFTER_MTS_GAPS
SQL_BEFORE_GTIDS
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
(右)STACKED
START
STARTING
(右)STARTS
STATS_AUTO_RECALC
STATS_PERSISTENT
STATS_SAMPLE_PAGES
STATUS
STOP
STORAGE
STORED
(R); 5.7.6新增(保留)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
(右)
VALIDATION
; 5.7.5新增(非保留)VALUE
VALUES
(右)VARBINARY
(右)VARCHAR
(右)VARCHARACTER
(右)VARIABLES
VARYING
(右)VIEW
VIRTUAL
(R); 5.7.6新增(保留)
WAIT
WARNINGS
WEEK
WEIGHT_STRING
WHEN
(右)WHERE
(右)WHILE
(右)WITH
(右)WITHOUT
; 5.7.5新增(非保留)WORK
WRAPPER
WRITE
(右)
X509
XA
XID
; 5.7.5新增(非保留)XML
XOR
(右)
YEAR
YEAR_MONTH
(右)
ZEROFILL
(右)
以下列表显示了与 MySQL 5.6 相比,MySQL 5.7 中添加的关键字和保留字。保留关键字标有 (R)。
一个 | 丙 | 乙 | 女 | 格 | 我 | 杰 | 男 | 否 | 欧 | 磷 | 右 | 年代 | 五 | 瓦 | X
ACCOUNT
ALWAYS
CHANNEL
COMPRESSION
ENCRYPTION
FILE_BLOCK_SIZE
FILTER
FOLLOWS
GENERATED
(右)GROUP_REPLICATION
INSTANCE
JSON
MASTER_TLS_VERSION
NEVER
OPTIMIZER_COSTS
(右)
PARSE_GCOL_EXPR
PRECEDES
REPLICATE_DO_DB
REPLICATE_DO_TABLE
REPLICATE_IGNORE_DB
REPLICATE_IGNORE_TABLE
REPLICATE_REWRITE_DB
REPLICATE_WILD_DO_TABLE
REPLICATE_WILD_IGNORE_TABLE
ROTATE
STACKED
STORED
(右)
VALIDATION
VIRTUAL
(右)
WITHOUT
XID