- Introduction
- Concepts
- Architecture
- Key Features
- Horizontal Scalability
- MySQL Compatible Syntax
- Replicate from and to MySQL
- Distributed Transactions with Strong Consistency
- Cloud Native Architecture
- Minimize ETL with HTAP
- Fault Tolerance & Recovery with Raft
- Automatic Rebalancing
- Deployment and Orchestration with Ansible, Kubernetes, Docker
- JSON Support
- Spark Integration
- Read Historical Data Without Restoring from Backup
- Fast Import and Restore of Data
- Hybrid of Column and Row Storage
- SQL Plan Management
- Open Source
- Online Schema Changes
- How-to
- Get Started
- Deploy
- Hardware Recommendations
- From Binary Tarball
- Orchestrated Deployment
- Geographic Redundancy
- Data Migration with Ansible
- Configure
- Secure
- Transport Layer Security (TLS)
- Generate Self-signed Certificates
- Monitor
- Migrate
- Maintain
- Common Ansible Operations
- Backup and Restore
- Identify Abnormal Queries
- Scale
- Upgrade
- Troubleshoot
- Reference
- SQL
- MySQL Compatibility
- SQL Language Structure
- Data Types
- Functions and Operators
- Function and Operator Reference
- 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
- Information Functions
- JSON Functions
- Aggregate (GROUP BY) Functions
- Window Functions
- Miscellaneous Functions
- Precision Math
- List of Expressions for Pushdown
- 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 TABLE
ALTER USER
ANALYZE TABLE
BEGIN
CHANGE COLUMN
COMMIT
CREATE DATABASE
CREATE INDEX
CREATE ROLE
CREATE TABLE LIKE
CREATE TABLE
CREATE USER
CREATE VIEW
DEALLOCATE
DELETE
DESC
DESCRIBE
DO
DROP COLUMN
DROP DATABASE
DROP INDEX
DROP ROLE
DROP TABLE
DROP USER
DROP VIEW
EXECUTE
EXPLAIN ANALYZE
EXPLAIN
FLUSH PRIVILEGES
FLUSH STATUS
FLUSH TABLES
GRANT <privileges>
GRANT <role>
INSERT
KILL [TIDB]
LOAD DATA
LOAD STATS
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 CHARACTER SET
SHOW COLLATION
SHOW [FULL] COLUMNS FROM
SHOW CREATE TABLE
SHOW CREATE USER
SHOW DATABASES
SHOW ENGINES
SHOW ERRORS
SHOW [FULL] FIELDS FROM
SHOW GRANTS
SHOW INDEXES [FROM|IN]
SHOW INDEX [FROM|IN]
SHOW KEYS [FROM|IN]
SHOW PRIVILEGES
SHOW [FULL] PROCESSSLIST
SHOW SCHEMAS
SHOW STATUS
SHOW [FULL] TABLES
SHOW TABLE REGIONS
SHOW TABLE STATUS
SHOW [GLOBAL|SESSION] VARIABLES
SHOW WARNINGS
SPLIT REGION
START TRANSACTION
TRACE
TRUNCATE
UPDATE
USE
- Constraints
- Generated Columns
- Partitioning
- Character Set
- SQL Mode
- Views
- Configuration
- Security
- Transactions
- System Databases
- Errors Codes
- Supported Client Drivers
- Garbage Collection (GC)
- Performance
- Overview
- Understanding the Query Execution Plan
- The Blocklist of Optimization Rules and Expression Pushdown
- Introduction to Statistics
- TopN and Limit Push Down
- Optimizer Hints
- Check the TiDB Cluster Status Using SQL Statements
- Execution Plan Binding
- Statement Summary Table
- Tune TiKV
- Operating System Tuning
- Column Pruning
- Key Monitoring Metrics
- Alert Rules
- Best Practices
- TiSpark
- TiKV
- TiDB Binlog
- Tools
- TiDB in Kubernetes
- FAQs
- Support
- Contribute
- Releases
- All Releases
- v3.0
- v2.1
- v2.0
- v1.0
- Glossary
You are viewing the documentation of an older version of the TiDB database (TiDB v3.0).
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.
Create, show, and drop a database
Create a database
To create a database, use the CREATE DATABASE
statement:
CREATE DATABASE db_name [options];
For example, to create a database named samp_db
:
CREATE DATABASE IF NOT EXISTS samp_db;
Show the databases
To show the databases, use the SHOW DATABASES
statement:
SHOW DATABASES;
Delete a database
To delete a database, use the DROP DATABASE
statement:
DROP DATABASE samp_db;
Create, show, and drop a table
Create a table
To create a table, use the
CREATE TABLE
statement:CREATE TABLE table_name column_name data_type constraint;
For example:
CREATE TABLE person ( number INT(11), name VARCHAR(255), birthday DATE );
Add
IF NOT EXISTS
to prevent an error if the table exists:CREATE TABLE IF NOT EXISTS person ( number INT(11), name VARCHAR(255), birthday DATE );
To view the statement that creates the table, use the
SHOW CREATE
statement:SHOW CREATE table person;
Show the tables
To show all the tables in a database, use the
SHOW TABLES
statement:SHOW TABLES FROM samp_db;
To show all the columns in a table, use the
SHOW FULL COLUMNS
statement:SHOW FULL COLUMNS FROM person;
Delete a table
To delete a table, use the DROP TABLE
statement:
DROP TABLE person;
or
DROP TABLE IF EXISTS person;
Create, show, and drop an index
Create an index
To create an index for the column whose value is not unique, use the
CREATE INDEX
orALTER TABLE
statement:CREATE INDEX person_num ON person (number);
or
ALTER TABLE person ADD INDEX person_num (number);
To create a unique index for the column whose value is unique, use the
CREATE UNIQUE INDEX
orALTER TABLE
statement:CREATE UNIQUE INDEX person_num ON person (number);
or
ALTER TABLE person ADD UNIQUE person_num (number);
Show the indexes
To show all the indexes in a table, use the SHOW INDEX
statement:
SHOW INDEX FROM person;
Delete an index
To delete an index, use the DROP INDEX
or ALTER TABLE
statement:
DROP INDEX person_num ON person;
ALTER TABLE person DROP INDEX person_num;
Insert, select, update, and delete data
Insert data
To insert data into a table, use the INSERT
statement:
INSERT INTO person VALUES("1","tom","20170912");
Select data
To view the data in a table, use the SELECT
statement:
SELECT * FROM person;
+--------+------+------------+
| number | name | birthday |
+--------+------+------------+
| 1 | tom | 2017-09-12 |
+--------+------+------------+
Update data
To update the data in a table, use the UPDATE
statement:
UPDATE person SET birthday='20171010' WHERE name='tom';
SELECT * FROM person;
+--------+------+------------+
| number | name | birthday |
+--------+------+------------+
| 1 | tom | 2017-10-10 |
+--------+------+------------+
Delete data
To delete the data in a table, use the DELETE
statement:
DELETE FROM person WHERE number=1;
SELECT * FROM person;
Empty set (0.00 sec)
Create, authorize, and delete a user
Create a user
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';
Authorize a user
To grant
tiuser
the privilege to retrieve the tables in thesamp_db
database:GRANT SELECT ON samp_db.* TO 'tiuser'@'localhost';
To check the privileges of
tiuser
:SHOW GRANTS for tiuser@localhost;
Delete a user
To delete tiuser
:
DROP USER 'tiuser'@'localhost';