关键字和保留字

关键字在 SQL 中有特殊的意义, 例如 SELECTUPDATEDELETE,在作为表名跟函数名的时候,需要特殊对待,例如作为表名,保留字需要被反引号包住:

CREATE TABLE select (a INT);
ERROR 1105 (HY000): line 0 column 19 near " (a INT)" (total length 27)
CREATE TABLE `select` (a INT);
Query OK, 0 rows affected (0.09 sec)

BEGINEND 是关键字, 但不是保留字,所以不需要反引号:

CREATE TABLE `select` (BEGIN int, END int);
Query OK, 0 rows affected (0.09 sec)

有一种特殊情况, 如果使用了限定符 .,那么也不需要用反引号:

CREATE TABLE test.select (BEGIN int, END int);
Query OK, 0 rows affected (0.08 sec)

下表列出了 TiDB 中的关键字和保留字。保留字用 (R) 来标识。窗口函数的保留字用 (R-Window) 来标识

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

A

  • ACTION
  • ADD (R)
  • ADDDATE
  • ADMIN
  • AFTER
  • ALL (R)
  • ALTER (R)
  • ALWAYS
  • ANALYZE(R)
  • AND (R)
  • ANY
  • AS (R)
  • ASC (R)
  • ASCII
  • AUTO_INCREMENT
  • AVG
  • AVG_ROW_LENGTH

B

  • BEGIN
  • BETWEEN (R)
  • BIGINT (R)
  • BINARY (R)
  • BINLOG
  • BIT
  • BIT_XOR
  • BLOB (R)
  • BOOL
  • BOOLEAN
  • BOTH (R)
  • BTREE
  • BY (R)
  • BYTE

C

  • CASCADE (R)
  • CASE (R)
  • CAST
  • CHANGE (R)
  • CHAR (R)
  • CHARACTER (R)
  • CHARSET
  • CHECK (R)
  • CHECKSUM
  • COALESCE
  • COLLATE (R)
  • COLLATION
  • COLUMN (R)
  • COLUMNS
  • COMMENT
  • COMMIT
  • COMMITTED
  • COMPACT
  • COMPRESSED
  • COMPRESSION
  • CONNECTION
  • CONSISTENT
  • CONSTRAINT (R)
  • CONVERT (R)
  • COUNT
  • CREATE (R)
  • CROSS (R)
  • CUME_DIST (R-Window)
  • CURRENT_DATE (R)
  • CURRENT_TIME (R)
  • CURRENT_TIMESTAMP (R)
  • CURRENT_USER (R)
  • CURTIME

D

  • DATA
  • DATABASE (R)
  • DATABASES (R)
  • DATE
  • DATE_ADD
  • DATE_SUB
  • DATETIME
  • DAY
  • DAY_HOUR (R)
  • DAY_MICROSECOND (R)
  • DAY_MINUTE (R)
  • DAY_SECOND (R)
  • DDL
  • DEALLOCATE
  • DEC
  • DECIMAL (R)
  • DEFAULT (R)
  • DELAY_KEY_WRITE
  • DELAYED (R)
  • DELETE (R)
  • DENSE_RANK (R-Window)
  • DESC (R)
  • DESCRIBE (R)
  • DISABLE
  • DISTINCT (R)
  • DISTINCTROW (R)
  • DIV (R)
  • DO
  • DOUBLE (R)
  • DROP (R)
  • DUAL (R)
  • DUPLICATE
  • DYNAMIC

E

  • ELSE (R)
  • ENABLE
  • ENCLOSED
  • END
  • ENGINE
  • ENGINES
  • ENUM
  • ESCAPE
  • ESCAPED
  • EVENTS
  • EXCLUSIVE
  • EXECUTE
  • EXISTS
  • EXPLAIN (R)
  • EXTRACT

F

  • FALSE (R)
  • FIELDS
  • FIRST
  • FIRST_VALUE (R-Window)
  • FIXED
  • FLOAT (R)
  • FLUSH
  • FOR (R)
  • FORCE (R)
  • FOREIGN (R)
  • FORMAT
  • FROM (R)
  • FULL
  • FULLTEXT (R)
  • FUNCTION

G

  • GENERATED (R)
  • GET_FORMAT
  • GLOBAL
  • GRANT (R)
  • GRANTS
  • GROUP (R)
  • GROUP_CONCAT
  • GROUPS (R-Window)

H

  • HASH
  • HAVING (R)
  • HIGH_PRIORITY (R)
  • HOUR
  • HOUR_MICROSECOND (R)
  • HOUR_MINUTE (R)
  • HOUR_SECOND (R)

I

  • IDENTIFIED
  • IF (R)
  • IGNORE (R)
  • IN (R)
  • INDEX (R)
  • INDEXES
  • INFILE (R)
  • INNER (R)
  • INSERT (R)
  • INT (R)
  • INTEGER (R)
  • INTERVAL (R)
  • INTO (R)
  • IS (R)
  • ISOLATION

J

  • JOBS
  • JOIN (R)
  • JSON

K

  • KEY (R)
  • KEY_BLOCK_SIZE
  • KEYS (R)
  • KILL (R)

L

  • LAG (R-Window)
  • LAST_VALUE (R-Window)
  • LEAD (R-Window)
  • LEADING (R)
  • LEFT (R)
  • LESS
  • LEVEL
  • LIKE (R)
  • LIMIT (R)
  • LINES (R)
  • LOAD (R)
  • LOCAL
  • LOCALTIME (R)
  • LOCALTIMESTAMP (R)
  • LOCK (R)
  • LONGBLOB (R)
  • LONGTEXT (R)
  • LOW_PRIORITY (R)

M

  • MAX
  • MAX_ROWS
  • MAXVALUE (R)
  • MEDIUMBLOB (R)
  • MEDIUMINT (R)
  • MEDIUMTEXT (R)
  • MICROSECOND
  • MIN
  • MIN_ROWS
  • MINUTE
  • MINUTE_MICROSECOND (R)
  • MINUTE_SECOND (R)
  • MIN
  • MIN_ROWS
  • MINUTE
  • MINUTE_MICROSECOND
  • MINUTE_SECOND
  • MOD (R)
  • MODE
  • MODIRY
  • MONTH

N

  • NAMES
  • NATIONAL
  • NATURAL (R)
  • NO
  • NO_WRITE_TO_BINLOG (R)
  • NONE
  • NOT (R)
  • NOW
  • NTH_VALUE (R-Window)
  • NTILE (R-Window)
  • NULL (R)
  • NUMERIC (R)
  • NVARCHAR (R)

O

  • OFFSET
  • ON (R)
  • ONLY
  • OPTION (R)
  • OR (R)
  • ORDER (R)
  • OUTER (R)
  • OVER (R-Window)

P

  • PARTITION (R)
  • PARTITIONS
  • PASSWORD
  • PERCENT_RANK (R-Window)
  • PLUGINS
  • POSITION
  • PRECISION (R)
  • PREPARE
  • PRIMARY (R)
  • PRIVILEGES
  • PROCEDURE (R)
  • PROCESS
  • PROCESSLIST

Q

  • QUARTER
  • QUERY
  • QUICK

R

  • RANGE (R)
  • RANK (R-Window)
  • READ (R)
  • REAL (R)
  • REDUNDANT
  • REFERENCES (R)
  • REGEXP (R)
  • RENAME (R)
  • REPEAT (R)
  • REPEATABLE
  • REPLACE (R)
  • RESTRICT (R)
  • REVERSE
  • REVOKE (R)
  • RIGHT (R)
  • RLIKE (R)
  • ROLLBACK
  • ROW
  • ROW_COUNT
  • ROW_FORMAT
  • ROW_NUMBER (R-Window)
  • ROWS (R-Window)

S

  • SCHEMA
  • SCHEMAS
  • SECOND
  • SECOND_MICROSECOND (R)
  • SELECT (R)
  • SERIALIZABLE
  • SESSION
  • SET (R)
  • SHARE
  • SHARED
  • SHOW (R)
  • SIGNED
  • SMALLINT (R)
  • SNAPSHOT
  • SOME
  • SQL_CACHE
  • SQL_CALC_FOUND_ROWS (R)
  • SQL_NO_CACHE
  • START
  • STARTING (R)
  • STATS
  • STATS_BUCKETS
  • STATS_HISTOGRAMS
  • STATS_META
  • STATS_PERSISTENT
  • STATUS
  • STORED (R)
  • SUBDATE
  • SUBSTR
  • SUBSTRING
  • SUM
  • SUPER

T

  • TABLE (R)
  • TABLES
  • TERMINATED (R)
  • TEXT
  • THAN
  • THEN (R)
  • TIDB
  • TIDB_INLJ
  • TIDB_SMJ
  • TIME
  • TIMESTAMP
  • TIMESTAMPADD
  • TIMESTAMPDIFF
  • TINYBLOB (R)
  • TINYINT (R)
  • TINYTEXT (R)
  • TO (R)
  • TRAILING (R)
  • TRANSACTION
  • TRIGGER (R)
  • TRIGGERS
  • TRIM
  • TRUE (R)
  • TRUNCATE

U

  • UNCOMMITTED
  • UNION (R)
  • UNIQUE (R)
  • UNKNOWN
  • UNLOCK (R)
  • UNSIGNED (R)
  • UPDATE (R)
  • USE (R)
  • USER
  • USING (R)
  • UTC_DATE (R)
  • UTC_TIME (R)
  • UTC_TIMESTAMP (R)

V

  • VALUE
  • VALUES (R)
  • VARBINARY (R)
  • VARCHAR (R)
  • VARIABLES
  • VIEW
  • VIRTUAL (R)

W

  • WARNINGS
  • WEEK
  • WHEN (R)
  • WHERE (R)
  • WINDOW (R-Window)
  • WITH (R)
  • WRITE (R)

X

  • XOR (R)

Y

  • YEAR
  • YEAR_MONTH (R)

Z

  • ZEROFILL (R)