TiDB - MySQL Compatibility
TiDB is highly compatible with the MySQL 5.7 protocol and common MySQL 5.7 features and syntax. MySQL 5.7 ecosystem tools such as PHPMyAdmin, Navicat, MySQL Workbench, mysqldump, and Mydumper/myloader, as well as MySQL clients, can be used with TiDB.
However, some MySQL features are not supported. This may be because there is a better way to solve the problem, such as replacing XML functions with JSON, or because demand is currently low compared with the effort required, such as stored procedures or functions. Some features can also be difficult to implement in a distributed system.
- TiDB also does not support the MySQL replication protocol, but it provides specific tools for replicating data from MySQL.
- Replicating data from MySQL: TiDB Data Migration, or DM, is a tool that supports full data migration and incremental data replication from MySQL/MariaDB to TiDB.
- Replicating data to MySQL: TiCDC is a tool that pulls TiKV change logs and replicates TiDB incremental data. TiCDC replicates TiDB incremental data to MySQL by using a MySQL sink.
Note:
This page explains general differences between MySQL and TiDB. For compatibility in safe and pessimistic transaction modes, see the dedicated page.
Unsupported Features
- Stored procedures and functions
- Triggers
- Events
- User-defined functions
FOREIGN KEYconstraints #18209FULLTEXTsyntax and indexes #1793SPATIAL, also known asGISorGEOMETRY, functions, data types, and indexes #6347- Character sets other than
ascii,binary,latin1,utf8,utf8mb4, andgbk - SYS schema
- Optimizer trace
- XML functions
- X Protocol #1109
- Savepoints #6840
- Column-level privileges #9766
XAsyntax. TiDB internally uses two-phase commit, but this is not exposed through the SQL interface.CREATE TABLE tblName AS SELECT stmtsyntax #4754CHECK TABLEsyntax #4673CHECKSUM TABLEsyntax #1895REPAIR TABLEsyntaxOPTIMIZE TABLEsyntaxHANDLERsyntaxCREATE TABLESPACEsyntax
Features That Differ from MySQL
Auto-increment IDs
-
In TiDB, auto-increment columns are globally unique. They are incremental on a single TiDB server, but they are not necessarily incremental or allocated in order across multiple TiDB servers. It is better not to mix default values and custom values. Otherwise, a
Duplicated Errormessage may appear. -
You can use the
tidb_allow_remove_auto_incsystem variable to allow or forbid removing theAUTO_INCREMENTcolumn attribute. The syntax for removing the column attribute isALTER TABLE MODIFYorALTER TABLE CHANGE. -
TiDB does not support adding the
AUTO_INCREMENTcolumn attribute. If this attribute is removed, it cannot be restored. -
For details, see
AUTO_INCREMENT.
mysql> CREATE TABLE t(id INT UNIQUE KEY AUTO_INCREMENT);
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO t VALUES(),(),();
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT _tidb_rowid, id FROM t;
+-------------+------+
| _tidb_rowid | id |
+-------------+------+
| 4 | 1 |
| 5 | 2 |
| 6 | 3 |
+-------------+------+
3 rows in set (0.01 sec)
Performance Schema
TiDB stores and queries performance monitoring metrics with the combination of Prometheus and Grafana. Performance Schema tables return empty results in TiDB.
Query Execution Plans
The output format, output content, and permission settings of EXPLAIN FOR query execution plans, or EXPLAIN, differ significantly from MySQL.
The MySQL system variable optimizer_switch is read-only in TiDB and does not affect query plans. Optimizer hints can also be used with syntax similar to MySQL, but the available hints and implementations may differ.
For details, see “Understanding Query Execution Plans.”
Built-in Functions
TiDB supports most MySQL built-in functions, but not all of them. The syntax SHOW BUILTINS provides a list of available functions.
Reference: TiDB SQL syntax.
DDL
All DDL changes supported by TiDB are performed online. Compared with MySQL DDL operations, TiDB DDL operations have the following main limitations.
-
You cannot complete multiple operations in one
ALTER TABLEstatement. For example, you cannot add multiple columns or indexes in one statement. Otherwise, anUnsupported multi schema changeerror message may appear. -
TiDB’s
ALTER TABLEdoes not support some data type changes. For example, TiDB does not support changing fromDECIMALtoDATE. If a data type change is not supported, TiDB displays anUnsupported modify column: type %d not match origin %derror. For details, seeALTER TABLE. -
The
ALGORITHM={INSTANT,INPLACE,COPY}syntax only works as an assertion in TiDB and does not change theALTERalgorithm. For details, seeALTER TABLE. -
Adding or removing a
CLUSTEREDprimary key is not supported. For details aboutCLUSTEREDprimary keys, see clustered indexes. -
Various index types, such as
HASH|BTREE|RTREE|FULLTEXT, are not supported. If specified, they are parsed and ignored. -
Table partitioning supports
HASH,RANGE, andLISTpartitioning types. For unsupported partition types,Warning: Unsupported partition type %s, treat as normal tablemay be output, where%sis the specific partition type. -
Table partitioning also supports
ADD,DROP, andTRUNCATEoperations. Other partition operations are ignored. The following table partition syntaxes are not supported:PARTITION BY KEYSUBPARTITION{CHECK|TRUNCATE|OPTIMIZE|REPAIR|IMPORT|DISCARD|REBUILD|REORGANIZE|COALESCE} PARTITION
For details, see partitioning.
Table Analysis
Statistics collection behaves differently in TiDB and MySQL. In MySQL/InnoDB it is a relatively lightweight and short-lived task, while in TiDB it rebuilds table statistics completely and can take much longer to complete.
These differences are described in detail under ANALYZE TABLE.
SELECT Syntax Limitations
- The syntax
SELECT ... INTO @variableis not supported. - The syntax
SELECT ... GROUP BY ... WITH ROLLUPis not supported. - The syntax
SELECT .. GROUP BY exprdoes not implyGROUP BY expr ORDER BY expras it does in MySQL 5.7.
For details, see the SELECT statement reference.
UPDATE Statement
See the UPDATE statement reference.
Views
TiDB views cannot be updated. Operations such as INSERT and UPDATE are not supported, and DELETE is also not supported.
Temporary Tables
For details, see compatibility between TiDB local temporary tables and MySQL temporary tables.
Character Sets and Collations
-
For details about character sets and collations supported by TiDB, see the character set and collation overview.
-
For details about MySQL compatibility for the GBK character set, see GBK compatibility.
-
TiDB inherits the character set used in a table as the national character set.
Storage Engines
For compatibility, TiDB supports syntax for creating tables with alternative storage engines. In implementation, TiDB describes metadata as the InnoDB storage engine.
TiDB supports storage engine abstraction similar to MySQL, but when starting the TiDB server, you must specify the storage engine with the --store option.
SQL Modes
TiDB supports most SQL modes.
- Compatibility modes such as
OracleorPostgreSQLare parsed but ignored. Compatibility modes are deprecated in MySQL 5.7 and were removed from MySQL 5.7. - The
ONLY_FULL_GROUP_BYmode has minor semantic differences from MySQL 5.7. - MySQL’s
MySQL NO_DIR_IN_CREATEandNO_ENGINE_SUBSTITUTIONmodes are accepted for compatibility, but they do not apply to TiDB.
Default Differences
- Default character set:
- TiDB’s default is
utf8mb4. - MySQL 5.7’s default is
latin1. - MySQL 8.0’s default is
utf8mb4.
- TiDB’s default is
- Default collation:
- TiDB’s default collation for
utf8mb4isutf8mb4_bin. - MySQL 5.7’s default collation for
utf8mb4isutf8mb4_general_ci. - MySQL 8.0’s default collation for
utf8mb4isutf8mb4_0900_ai_ci.
- TiDB’s default collation for
- Default value of
foreign_key_checks:- TiDB’s default is
OFF, and currently TiDB supports onlyOFF. - MySQL 5.7’s default is
ON.
- TiDB’s default is
- Default SQL mode:
- TiDB’s default SQL mode includes the following modes.
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION. - MySQL’s default SQL mode:
- MySQL 5.7’s default SQL mode is the same as TiDB’s.
- MySQL 8.0’s default SQL mode includes the following modes:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION.
- TiDB’s default SQL mode includes the following modes.
- Default value of
lower_case_table_names:- TiDB’s default is
2, and currently TiDB supports only2. - MySQL defaults:
- Linux:
0 - Windows:
1 - macOS:
2
- Linux:
- TiDB’s default is
- Default value of
explicit_defaults_for_timestamp:- TiDB’s default is
ON, and currently TiDB supports onlyON. - MySQL defaults:
- MySQL 5.7:
OFF. - MySQL 8.0:
ON.
- MySQL 5.7:
- TiDB’s default is
Date and Time
Named Time Zones
- TiDB currently uses all time zone rules installed on the system, generally packaged as
tzdata, for calculations. You can use all time zone names without importing time zone table data. You cannot import time zone table data or change calculation rules. - MySQL uses the local time zone by default and depends on the current time zone rules built into the system for calculations, such as the start of daylight saving time. Also, a time zone cannot be specified by name without importing time zone table data.
Differences in the Type System
The following column types are supported by MySQL but not by TiDB.
- FLOAT4/FLOAT8
SQL_TSI_*, including SQL_TSI_MONTH, SQL_TSI_WEEK, SQL_TSI_DAY, SQL_TSI_HOUR, SQL_TSI_MINUTE, and SQL_TSI_SECOND, excluding SQL_TSI_YEAR
Incompatibilities Caused by Deprecated Features
TiDB does not implement certain features marked as deprecated in MySQL.
- Specifying precision for floating-point types. This is deprecated in MySQL 8.0, and using the
DECIMALtype instead is recommended. - The
ZEROFILLattribute. This is deprecated in MySQL 8.0, and padding numbers in the application instead is recommended.
MySQL Compatibility last modified 2022-07-21 19:23:10: cloud: add SQL and TiFlash related docs (#9001) (#9479)