- Docs Home
- About TiDB Cloud
- Get Started
- Develop Applications
- Overview
- Quick Start
- Build a TiDB Developer Cluster
- CRUD SQL in TiDB
- Build a Simple CRUD App with TiDB
- Example Applications
- Connect to TiDB
- Design Database Schema
- Write Data
- Read Data
- Transaction
- Optimize
- Troubleshoot
- Reference
- Cloud Native Development Environment
- Manage Cluster
- Plan Your Cluster
- Create a TiDB Cluster
- Connect to Your TiDB Cluster
- Set Up VPC Peering Connections
- Use an HTAP Cluster with TiFlash
- Scale a TiDB Cluster
- Upgrade a TiDB Cluster
- Delete a TiDB Cluster
- Use TiDB Cloud API (Beta)
- Migrate Data
- Import Sample Data
- Migrate Data into TiDB
- Configure Amazon S3 Access and GCS Access
- Migrate from MySQL-Compatible Databases
- Migrate Incremental Data from MySQL-Compatible Databases
- Migrate from Amazon Aurora MySQL in Bulk
- Import or Migrate from Amazon S3 or GCS to TiDB Cloud
- Import CSV Files from Amazon S3 or GCS into TiDB Cloud
- Import Apache Parquet Files from Amazon S3 or GCS into TiDB Cloud
- Troubleshoot Access Denied Errors during Data Import from Amazon S3
- Export Data from TiDB
- Back Up and Restore
- Monitor and Alert
- Overview
- Built-in Monitoring
- Built-in Alerting
- Third-Party Monitoring Integrations
- Tune Performance
- Overview
- Analyze Performance
- SQL Tuning
- Overview
- Understanding the Query Execution Plan
- SQL Optimization Process
- Overview
- Logic Optimization
- Physical Optimization
- Prepare Execution Plan Cache
- Control Execution Plans
- TiKV Follower Read
- Coprocessor Cache
- Garbage Collection (GC)
- Tune TiFlash performance
- Manage User Access
- Billing
- Reference
- TiDB Cluster Architecture
- TiDB Cloud Cluster Limits and Quotas
- TiDB Limitations
- SQL
- Explore SQL with TiDB
- SQL Language Structure and Syntax
- SQL Statements
ADD COLUMN
ADD INDEX
ADMIN
ADMIN CANCEL DDL
ADMIN CHECKSUM TABLE
ADMIN CHECK [TABLE|INDEX]
ADMIN SHOW DDL [JOBS|QUERIES]
ALTER DATABASE
ALTER INDEX
ALTER TABLE
ALTER TABLE COMPACT
ALTER USER
ANALYZE TABLE
BATCH
BEGIN
CHANGE COLUMN
COMMIT
CHANGE DRAINER
CHANGE PUMP
CREATE [GLOBAL|SESSION] BINDING
CREATE DATABASE
CREATE INDEX
CREATE ROLE
CREATE SEQUENCE
CREATE TABLE LIKE
CREATE TABLE
CREATE USER
CREATE VIEW
DEALLOCATE
DELETE
DESC
DESCRIBE
DO
DROP [GLOBAL|SESSION] BINDING
DROP COLUMN
DROP DATABASE
DROP INDEX
DROP ROLE
DROP SEQUENCE
DROP STATS
DROP TABLE
DROP USER
DROP VIEW
EXECUTE
EXPLAIN ANALYZE
EXPLAIN
FLASHBACK TABLE
FLUSH PRIVILEGES
FLUSH STATUS
FLUSH TABLES
GRANT <privileges>
GRANT <role>
INSERT
KILL [TIDB]
MODIFY COLUMN
PREPARE
RECOVER TABLE
RENAME INDEX
RENAME TABLE
REPLACE
REVOKE <privileges>
REVOKE <role>
ROLLBACK
SELECT
SET DEFAULT ROLE
SET [NAMES|CHARACTER SET]
SET PASSWORD
SET ROLE
SET TRANSACTION
SET [GLOBAL|SESSION] <variable>
SHOW ANALYZE STATUS
SHOW [GLOBAL|SESSION] BINDINGS
SHOW BUILTINS
SHOW CHARACTER SET
SHOW COLLATION
SHOW [FULL] COLUMNS FROM
SHOW CREATE SEQUENCE
SHOW CREATE TABLE
SHOW CREATE USER
SHOW DATABASES
SHOW DRAINER STATUS
SHOW ENGINES
SHOW ERRORS
SHOW [FULL] FIELDS FROM
SHOW GRANTS
SHOW INDEX [FROM|IN]
SHOW INDEXES [FROM|IN]
SHOW KEYS [FROM|IN]
SHOW MASTER STATUS
SHOW PLUGINS
SHOW PRIVILEGES
SHOW [FULL] PROCESSSLIST
SHOW PROFILES
SHOW PUMP STATUS
SHOW SCHEMAS
SHOW STATS_HEALTHY
SHOW STATS_HISTOGRAMS
SHOW STATS_META
SHOW STATUS
SHOW TABLE NEXT_ROW_ID
SHOW TABLE REGIONS
SHOW TABLE STATUS
SHOW [FULL] TABLES
SHOW [GLOBAL|SESSION] VARIABLES
SHOW WARNINGS
SHUTDOWN
SPLIT REGION
START TRANSACTION
TABLE
TRACE
TRUNCATE
UPDATE
USE
WITH
- Data Types
- Functions and Operators
- Overview
- Type Conversion in Expression Evaluation
- Operators
- Control Flow Functions
- String Functions
- Numeric Functions and Operators
- Date and Time Functions
- Bit Functions and Operators
- Cast Functions and Operators
- Encryption and Compression Functions
- Locking Functions
- Information Functions
- JSON Functions
- Aggregate (GROUP BY) Functions
- Window Functions
- Miscellaneous Functions
- Precision Math
- Set Operations
- List of Expressions for Pushdown
- TiDB Specific Functions
- Clustered Indexes
- Constraints
- Generated Columns
- SQL Mode
- Table Attributes
- Transactions
- Views
- Partitioning
- Temporary Tables
- Cached Tables
- Character Set and Collation
- Read Historical Data
- System Tables
mysql
- INFORMATION_SCHEMA
- Overview
ANALYZE_STATUS
CLIENT_ERRORS_SUMMARY_BY_HOST
CLIENT_ERRORS_SUMMARY_BY_USER
CLIENT_ERRORS_SUMMARY_GLOBAL
CHARACTER_SETS
CLUSTER_INFO
COLLATIONS
COLLATION_CHARACTER_SET_APPLICABILITY
COLUMNS
DATA_LOCK_WAITS
DDL_JOBS
DEADLOCKS
ENGINES
KEY_COLUMN_USAGE
PARTITIONS
PROCESSLIST
REFERENTIAL_CONSTRAINTS
SCHEMATA
SEQUENCES
SESSION_VARIABLES
SLOW_QUERY
STATISTICS
TABLES
TABLE_CONSTRAINTS
TABLE_STORAGE_STATS
TIDB_HOT_REGIONS_HISTORY
TIDB_INDEXES
TIDB_SERVERS_INFO
TIDB_TRX
TIFLASH_REPLICA
TIKV_REGION_PEERS
TIKV_REGION_STATUS
TIKV_STORE_STATUS
USER_PRIVILEGES
VIEWS
- System Variables
- API Reference
- Storage Engines
- Dumpling
- Table Filter
- Troubleshoot Inconsistency Between Data and Indexes
- FAQs
- Release Notes
- Support
- Glossary
Explore SQL with TiDB
TiDB is compatible with MySQL, you can use MySQL statements directly in most of the cases. For unsupported features, see Compatibility with MySQL.
To experiment with SQL and test out TiDB compatibility with MySQL queries, you can run TiDB directly in your web browser without installing it. You can also first deploy a TiDB cluster and then run SQL statements in it.
This page walks you through the basic TiDB SQL statements such as DDL, DML and CRUD operations. For a complete list of TiDB statements, see TiDB SQL Syntax Diagram.
Category
SQL is divided into the following 4 types according to their functions:
DDL (Data Definition Language): It is used to define database objects, including databases, tables, views, and indexes.
DML (Data Manipulation Language): It is used to manipulate application related records.
DQL (Data Query Language): It is used to query the records after conditional filtering.
DCL (Data Control Language): It is used to define access privileges and security levels.
Common DDL features are creating, modifying, and deleting objects (such as tables and indexes). The corresponding commands are CREATE
, ALTER
, and DROP
.
Show, create and drop a database
A database in TiDB can be considered as a collection of objects such as tables and indexes.
To show the list of databases, use the SHOW DATABASES
statement:
SHOW DATABASES;
To use the database named mysql
, use the following statement:
USE mysql;
To show all the tables in a database, use the SHOW TABLES
statement:
SHOW TABLES FROM mysql;
To create a database, use the CREATE DATABASE
statement:
CREATE DATABASE db_name [options];
To create a database named samp_db
, use the following statement:
CREATE DATABASE IF NOT EXISTS samp_db;
Add IF NOT EXISTS
to prevent an error if the database exists.
To delete a database, use the DROP DATABASE
statement:
DROP DATABASE samp_db;
Create, show, and drop a table
To create a table, use the CREATE TABLE
statement:
CREATE TABLE table_name column_name data_type constraint;
For example, to create a table named person
which includes fields such as number, name, and birthday, use the following statement:
CREATE TABLE person (
id INT(11),
name VARCHAR(255),
birthday DATE
);
To view the statement that creates the table (DDL), use the SHOW CREATE
statement:
SHOW CREATE table person;
To delete a table, use the DROP TABLE
statement:
DROP TABLE person;
Create, show, and drop an index
Indexes are used to speed up queries on indexed columns. To create an index for the column whose value is not unique, use the CREATE INDEX
statement:
CREATE INDEX person_id ON person (id);
Or use the ALTER TABLE
statement:
ALTER TABLE person ADD INDEX person_id (id);
To create a unique index for the column whose value is unique, use the CREATE UNIQUE INDEX
statement:
CREATE UNIQUE INDEX person_unique_id ON person (id);
Or use the ALTER TABLE
statement:
ALTER TABLE person ADD UNIQUE person_unique_id (id);
To show all the indexes in a table, use the SHOW INDEX
statement:
SHOW INDEX FROM person;
To delete an index, use the DROP INDEX
or ALTER TABLE
statement. DROP INDEX
can be nested in ALTER TABLE
:
DROP INDEX person_id ON person;
ALTER TABLE person DROP INDEX person_unique_id;
DDL operations are not transactions. You don't need to run a COMMIT
statement when executing DDL operations.
Insert, update, and delete data
Common DML features are adding, modifying, and deleting table records. The corresponding commands are INSERT
, UPDATE
, and DELETE
.
To insert data into a table, use the INSERT
statement:
INSERT INTO person VALUES(1,'tom','20170912');
To insert a record containing data of some fields into a table, use the INSERT
statement:
INSERT INTO person(id,name) VALUES('2','bob');
To update some fields of a record in a table, use the UPDATE
statement:
UPDATE person SET birthday='20180808' WHERE id=2;
To delete the data in a table, use the DELETE
statement:
DELETE FROM person WHERE id=2;
The UPDATE
and DELETE
statements without the WHERE
clause as a filter operate on the entire table.
Query data
DQL is used to retrieve the desired data rows from a table or multiple tables.
To view the data in a table, use the SELECT
statement:
SELECT * FROM person;
To query a specific column, add the column name after the SELECT
keyword:
SELECT name FROM person;
+------+
| name |
+------+
| tom |
+------+
1 rows in set (0.00 sec)
Use the WHERE
clause to filter all records that match the conditions and then return the result:
SELECT * FROM person where id<5;
Create, authorize, and delete a user
DCL are usually used to create or delete users, and manage user privileges.
To create a user, use the CREATE USER
statement. The following example creates a user named tiuser
with the password 123456
:
CREATE USER 'tiuser'@'localhost' IDENTIFIED BY '123456';
To grant tiuser
the privilege to retrieve the tables in the samp_db
database:
GRANT SELECT ON samp_db.* TO 'tiuser'@'localhost';
To check the privileges of tiuser
:
SHOW GRANTS for tiuser@localhost;
To delete tiuser
:
DROP USER 'tiuser'@'localhost';