title | summary |
---|---|
CRUD SQL in TiDB |
A brief introduction to TiDB's CURD SQL. |
This document briefly introduces how to use TiDB's CURD SQL.
Please make sure you are connected to a TiDB cluster. If not, refer to Build a TiDB Cluster in TiDB Cloud (Serverless Tier) to create a Serverless Tier cluster.
Note:
This document references and simplifies Explore SQL with TiDB. For more details, see Explore SQL with TiDB.
TiDB is compatible with MySQL, you can use MySQL statements directly in most 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.
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.
The following mainly introduces DML and DQL. For more information about DDL and DCL, see Explore SQL with TiDB or TiDB SQL syntax detailed explanation.
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:
{{< copyable "sql" >}}
INSERT INTO person VALUES(1,'tom','20170912');
To insert a record containing data of some fields into a table, use the INSERT
statement:
{{< copyable "sql" >}}
INSERT INTO person(id,name) VALUES('2','bob');
To update some fields of a record in a table, use the UPDATE
statement:
{{< copyable "sql" >}}
UPDATE person SET birthday='20180808' WHERE id=2;
To delete the data in a table, use the DELETE
statement:
{{< copyable "sql" >}}
DELETE FROM person WHERE id=2;
Note:
The
UPDATE
andDELETE
statements without theWHERE
clause as a filter operate on the entire table.
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:
{{< copyable "sql" >}}
SELECT * FROM person;
To query a specific column, add the column name after the SELECT
keyword:
{{< copyable "sql" >}}
SELECT name FROM person;
The result is as follows:
+------+
| 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:
{{< copyable "sql" >}}
SELECT * FROM person WHERE id < 5;