PingCAP
  • Docs
  • Success Stories
  • Blog
  • About
  • Free Consultation
PingCAP
  • Docs
  • Success Stories
  • Blog
  • About
  • Free Consultation

Contact

中文
Documentation
  • About TiDB
    • TiDB Introduction
    • TiDB Architecture
  • Quick Start
    • TiDB Quick Start Guide
    • Basic SQL Statements
    • Bikeshare Example Database
  • TiDB User Guide
    • TiDB Server Administration
      • The TiDB Server
      • The TiDB Command Options
      • The TiDB Data Directory
      • The TiDB System Database
      • The TiDB System Variables
      • The Proprietary System Variables and Syntax in TiDB
      • The TiDB Server Logs
      • The TiDB Access Privilege System
      • TiDB User Account Management
      • Use Encrypted Connections
    • SQL Optimization
      • Understand the Query Execution Plan
      • Introduction to Statistics
    • Language Structure
      • Literal Values
      • Schema Object Names
      • Keywords and Reserved Words
      • User-Defined Variables
      • Expression Syntax
      • Comment Syntax
    • Globalization
      • Character Set Support
      • Character Set Configuration
      • Time Zone Support
    • Data Types
      • Numeric Types
      • Date and Time Types
      • String Types
      • JSON Types
      • The ENUM data type
      • The SET Type
      • Data Type Default Values
    • 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
      • Miscellaneous Functions
      • Precision Math
    • SQL Statement Syntax
      • Data Definition Statements
      • Data Manipulation Statements
      • Transactions
      • Database Administration Statements
      • Prepared SQL Statement Syntax
      • Utility Statements
      • TiDB SQL Syntax Diagram
    • JSON Functions and Generated Column
    • Connectors and APIs
    • TiDB Transaction Isolation Levels
    • Error Codes and Troubleshooting
    • Compatibility with MySQL
    • TiDB Memory Control
    • Slow Query Log
    • Advanced Usage
      • Read Data From History Versions
      • Garbage Collection (GC)
  • TiDB Operations Guide
    • Hardware and Software Requirements
    • Deploy
      • Ansible Deployment (Recommended)
      • Offline Deployment Using Ansible
      • Docker Deployment
      • Docker Compose Deployment
      • Cross-Region Deployment
      • Kubernetes Deployment
    • Configure
      • Configuration Flags
      • Configuration File Description
      • Modify Component Configuration Using Ansible
      • Enable TLS Authentication
      • Generate Self-signed Certificates
    • Monitor
      • Overview of the Monitoring Framework
      • Key Metrics
      • Monitor a TiDB Cluster
    • Scale
      • Scale a TiDB Cluster
      • Scale Using Ansible
    • Upgrade
      • Upgrade the Component Version
      • TiDB 2.0 Upgrade Guide
    • Tune Performance
    • Backup and Migrate
      • Backup and Restore
      • Migrate
        • Migration Overview
        • Migrate All the Data
        • Migrate the Data Incrementally
    • TiDB-Ansible Common Operations
    • Troubleshoot
  • TiDB Enterprise Tools
    • Syncer
    • mydumper
    • Loader
    • TiDB-Binlog
    • PD Control
    • PD Recover
    • TiKV Control
    • TiDB Controller
  • TiKV Documentation
  • TiSpark Documentation
    • Quick Start Guide
    • User Guide
  • Frequently Asked Questions (FAQ)
  • TiDB Best Practices
  • Releases
    • 2.1 RC3
    • 2.1 RC2
    • 2.0.7
    • 2.1 RC1
    • 2.0.6
    • 2.0.5
    • 2.1 Beta
    • 2.0.4
    • 2.0.3
    • 2.0.2
    • 2.0.1
    • 2.0
    • 2.0 RC5
    • 2.0 RC4
    • 2.0 RC3
    • 2.0 RC1
    • 1.1 Beta
    • 1.0.8
    • 1.0.7
    • 1.1 Alpha
    • 1.0.6
    • 1.0.5
    • 1.0.4
    • 1.0.3
    • 1.0.2
    • 1.0.1
    • 1.0
    • Pre-GA
    • RC4
    • RC3
    • RC2
    • RC1
  • TiDB Adopters
  • TiDB Roadmap
  • Connect with us
  • More Resources
    • Frequently Used Tools
    • PingCAP Blog
    • Weekly Update

Try TiDB

After you successfully deploy a TiDB cluster, you can run SQL statements in TiDB. Because TiDB is compatible with MySQL, you can use THE MySQL client to connect to TiDB and run MySQL statements directly in most of the cases. For more information, see Compatibility with MySQL.

This page includes some basic SQL statements such as CRUD operations. For a complete list of the 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 or ALTER 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 or ALTER TABLE statement:

    CREATE UNIQUE INDEX person_num ON person (number);

    or

    ALTER TABLE person ADD UNIQUE person_num on (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 the samp_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';
"Try TiDB" was last updated Sep 5 2018: readme, quickstart: reorganize the quick start structure (#594) (0aabe9d)
Improve this page

What’s on this page

Product

  • TiDB
  • TiSpark
  • Roadmap
  • TiDB Cloud

Docs

  • Quick Start
  • Best Practices
  • FAQ
  • TiDB Utilities
  • Release Notes

Resources

  • Blog
  • Weekly
  • GitHub
  • TiDB Academy
  • TiDB Community

Company

  • About
  • Careers
  • News
  • Contact Us
  • Privacy Policy
  • Terms of Service

Connect

  • Twitter
  • LinkedIn
  • Reddit
  • Google Group
  • Stack Overflow

© 2018 PingCAP. All Rights Reserved.

中文