| title | category |
|---|---|
Data Definition Statements |
user guide |
DDL (Data Definition Language) is used to define the database structure or schema, and to manage the database and statements of various objects in the database.
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_nameThe CREATE DATABASE statement is used to create a database, and to specify the default properties of the database, such as the default character set and validation rules. CREATE SCHEMA is a synonym for CREATE DATABASE.
If you create an existing database and does not specify IF NOT EXISTS, an error is displayed.
The create_specification option is used to specify the specific CHARACTER SET and COLLATE in the database. Currently, the option is only supported in syntax.
DROP {DATABASE | SCHEMA} [IF EXISTS] db_nameThe DROP DATABASE statement is used to delete the specified database and its tables.
The IF EXISTS statement is used to prevent an error if the database does not exist.
CREATE TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
CREATE TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
create_definition:
col_name column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
| {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
| {FULLTEXT} [INDEX|KEY] [index_name] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) reference_definition
column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string']
[reference_definition]
| data_type [GENERATED ALWAYS] AS (expression)
[VIRTUAL | STORED] [UNIQUE [KEY]] [COMMENT comment]
[NOT NULL | NULL] [[PRIMARY] KEY]
data_type:
BIT[(length)]
| TINYINT[(length)] [UNSIGNED] [ZEROFILL]
| SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
| MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
| INT[(length)] [UNSIGNED] [ZEROFILL]
| INTEGER[(length)] [UNSIGNED] [ZEROFILL]
| BIGINT[(length)] [UNSIGNED] [ZEROFILL]
| REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
| FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
| NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL]
| DATE
| TIME[(fsp)]
| TIMESTAMP[(fsp)]
| DATETIME[(fsp)]
| YEAR
| CHAR[(length)] [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| VARCHAR(length) [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| BINARY[(length)]
| VARBINARY(length)
| TINYBLOB
| BLOB
| MEDIUMBLOB
| LONGBLOB
| TINYTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| TEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| MEDIUMTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| LONGTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| ENUM(value1,value2,value3,...)
[CHARACTER SET charset_name] [COLLATE collation_name]
| SET(value1,value2,value3,...)
[CHARACTER SET charset_name] [COLLATE collation_name]
| JSON
index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH}
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| COMMENT 'string'
reference_definition:
REFERENCES tbl_name (index_col_name,...)
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
table_options:
table_option [[,] table_option] ...
table_option:
AUTO_INCREMENT [=] value
| AVG_ROW_LENGTH [=] value
| [DEFAULT] CHARACTER SET [=] charset_name
| CHECKSUM [=] {0 | 1}
| [DEFAULT] COLLATE [=] collation_name
| COMMENT [=] 'string'
| COMPRESSION [=] {'ZLIB'|'LZ4'|'NONE'}
| CONNECTION [=] 'connect_string'
| DELAY_KEY_WRITE [=] {0 | 1}
| ENGINE [=] engine_name
| KEY_BLOCK_SIZE [=] value
| MAX_ROWS [=] value
| MIN_ROWS [=] value
| ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
| STATS_PERSISTENT [=] {DEFAULT|0|1}The CREATE TABLE statement is used to create a table. Currently, it does not support temporary tables, CHECK constraints, or importing data from other tables while creating tables. It supports some of the Partition_options in syntax.
-
When you create an existing table and if you specify
IF NOT EXIST, it does not report an error. Otherwise, it reports an error. -
Use
LIKEto create an empty table based on the definition of another table including its column and index properties. -
The
FULLTEXTandFOREIGN KEYincreate_definitionare currently only supported in syntax. -
For the
data_type, see Data Types. -
The
[ASC | DESC]inindex_col_nameis currently only supported in syntax. -
The
index_typeis currently only supported in syntax. -
The
KEY_BLOCK_SIZEinindex_optionis currently only supported in syntax. -
The
table_optioncurrently only supportsAUTO_INCREMENT,CHARACTER SETandCOMMENT, while the others are only supported in syntax. The clauses are separated by a comma,. See the following table for details:Parameters Description Example AUTO_INCREMENTThe initial value of the increment field AUTO_INCREMENT= 5CHARACTER SETTo specify the string code for the table; currently only support UTF8MB4 CHARACTER SET= 'utf8mb4'COMMENTThe comment information COMMENT= 'comment info'
The TiDB automatic increment ID (AUTO_INCREMENT ID) only guarantees automatic increment and uniqueness and does not guarantee continuous allocation. Currently, TiDB adopts bulk allocation. If you insert data into multiple TiDB servers at the same time, the allocated automatic increment ID is not continuous.
You can specify the AUTO_INCREMENT for integer fields. A table only supports one field with the AUTO_INCREMENT property.
DROP TABLE [IF EXISTS]
tbl_name [, tbl_name] ...You can delete multiple tables at the same time. The tables are separated by a comma ,.
If you delete a table that does not exist and does not specify the use of IF EXISTS, an error is displayed.
TRUNCATE [TABLE] tbl_nameThe TRUNCATE TABLE statement is used to clear all the data in the specified table but keeps the table structure.
This operation is similar to deleting all the data of a specified table, but it is much faster and is not affected by the number of rows in the table.
Note: If you use the
TRUNCATE TABLEstatement, the value ofAUTO_INCREMENTin the original table is reset to its starting value.
RENAME TABLE
tbl_name TO new_tbl_nameThe RENAME TABLE statement is used to rename a table.
This statement is equivalent to the following ALTER TABLE statement:
ALTER TABLE old_table RENAME new_table;ALTER TABLE tbl_name
[alter_specification]
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| ADD [COLUMN] (col_name column_definition,...)
| ADD {INDEX|KEY} [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] PRIMARY KEY
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
UNIQUE [INDEX|KEY] [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD FULLTEXT [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
reference_definition
| ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
| CHANGE [COLUMN] old_col_name new_col_name column_definition
[FIRST|AFTER col_name]
| {DISABLE|ENABLE} KEYS
| DROP [COLUMN] col_name
| DROP {INDEX|KEY} index_name
| DROP PRIMARY KEY
| DROP FOREIGN KEY fk_symbol
| LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE}
| MODIFY [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| RENAME [TO|AS] new_tbl_name
| {WITHOUT|WITH} VALIDATION
index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH}
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| COMMENT 'string'
table_options:
table_option [[,] table_option] ...
table_option:
AVG_ROW_LENGTH [=] value
| [DEFAULT] CHARACTER SET [=] charset_name
| CHECKSUM [=] {0 | 1}
| [DEFAULT] COLLATE [=] collation_name
| COMMENT [=] 'string'
| COMPRESSION [=] {'ZLIB'|'LZ4'|'NONE'}
| CONNECTION [=] 'connect_string'
| DELAY_KEY_WRITE [=] {0 | 1}
| ENGINE [=] engine_name
| KEY_BLOCK_SIZE [=] value
| MAX_ROWS [=] value
| MIN_ROWS [=] value
| ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
| STATS_PERSISTENT [=] {DEFAULT|0|1}The ALTER TABLE statement is used to update the structure of an existing table, such as updating the table or table properties, adding or deleting columns, creating or deleting indexes, updating columns or column properties. The descriptions of several field types are as follows:
- For
index_col_name,index_type, andindex_option, see CREATE INDEX Syntax. - Currently, the
table_optionis only supported in syntax.
The support for specific operation types is as follows:
ADD/DROP INDEX/COLUMN: currently, does not support the creation or deletion of multiple indexes or columns at the same timeADD/DROP PRIMARY KEY: currently not supportedDROP COLUMN: currently does not support the deletion of columns that are primary key columns or index columnsADD COLUMN: currently, does not support setting the newly added column as the primary key or unique index at the same time, and does not support setting the column property toAUTO_INCREMENTCHANGE/MODIFY COLUMN: currently supports some of the syntaxes, and the details are as follows:- In updating data types, the
CHANGE/MODIFY COLUMNonly supports updates between integer types, updates between string types, and updates between Blob types. You can only extend the length of the original type. Besides, the column properties ofunsigned/charset/collatecannot be changed. The specific supported types are classified as follows:- Integer types:
TinyInt,SmallInt,MediumInt,Int,BigInt - String types:
Char,Varchar,Text,TinyText,MediumText,LongText - Blob types:
Blob,TinyBlob,MediumBlob,LongBlob
- Integer types:
- In updating type definition, the
CHANGE/MODIFY COLUMNsupportsdefault value,comment,null,not nullandOnUpdate, but does not support the update fromnulltonot null. - The
CHANGE/MODIFY COLUMNdoes not support the update ofenumtype column.
- In updating data types, the
LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE}: is currently only supported in syntax
CREATE [UNIQUE] INDEX index_name
[index_type]
ON tbl_name (index_col_name,...)
[index_option] ...
index_col_name:
col_name [(length)] [ASC | DESC]
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| COMMENT 'string'
index_type:
USING {BTREE | HASH}The CREATE INDEX statement is used to create the index for an existing table. In function, CREATE INDEX corresponds to the index creation of ALTER TABLE. Similar to MySQL, the CREATE INDEX cannot create a primary key index.
- The
CREATE INDEXsupports theUNIQUEindex and does not supportFULLTEXTandSPATIALindexes. - The
index_col_namesupports the length option with a maximum length limit of 3072 bytes. The length limit does not change depending on the storage engine, and character set used when building the table. This is because TiDB does not use storage engines like InnoDB and MyISAM, and only provides syntax compatibility with MySQL for the storage engine options when creating tables. Similarly, TiDB uses the utf8mb4 character set, and only provides syntax compatibility with MySQL for the character set options when creating tables. For more information, see Compatibility with MySQL. - The
index_col_namesupports the index sorting options ofASCandDESC. The behavior of sorting options is similar to MySQL, and only syntax parsing is supported. All the internal indexes are stored in ascending order ascending order. For more information, see CREATE INDEX Syntax. - The
index_optionsupportsKEY_BLOCK_SIZE,index_typeandCOMMENT. TheCOMMENTsupports a maximum of 1024 characters and does not support theWITH PARSERoption. - The
index_typesupportsBTREEandHASHonly in MySQL syntax, which means the index type is independent of the storage engine option in the creating table statement. For example, in MySQL, when you useCREATE INDEXon a table using InnoDB, it only supports theBTREEindex, while TiDB supports bothBTREEandHASHindexes. - The
CREATE INDEXdoes not support thealgorithm_optionandlock_optionin MySQL.
DROP INDEX index_name ON tbl_nameThe DROP INDEX statement is used to delete a table index. Currently, it does not support deleting the primary key index.