From 0a9ee551a15fb04b1e84d65bc6f3974f558bc95a Mon Sep 17 00:00:00 2001 From: ireneontheway Date: Thu, 23 Jul 2020 12:36:32 +0800 Subject: [PATCH 1/5] Update basic-sql-operations.md --- basic-sql-operations.md | 269 ++++++++++++++++++---------------------- 1 file changed, 124 insertions(+), 145 deletions(-) diff --git a/basic-sql-operations.md b/basic-sql-operations.md index 87d19763d58e7..f6f4d722a6e48 100644 --- a/basic-sql-operations.md +++ b/basic-sql-operations.md @@ -1,10 +1,10 @@ --- -title: Explore SQL with TiDB +title: Basic SQL Operations summary: Learn about the basic SQL statements for the TiDB database. aliases: ['/docs/dev/basic-sql-operations/','/docs/dev/how-to/get-started/explore-sql/'] --- -# Explore SQL with TiDB +# Basic SQL Operations TiDB is compatible with MySQL, you can use MySQL statements directly in most of the cases. For unsupported features, see [Compatibility with MySQL](/mysql-compatibility.md#unsupported-features). @@ -12,29 +12,25 @@ To experiment with SQL and test out TiDB compatibility with MySQL queries, you c 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](https://pingcap.github.io/sqlgram/). -## Create, show, and drop a database +## Category -### Create a database +SQL is divided into the following 4 types according to their functions: -To create a database, use the `CREATE DATABASE` statement: +- DDL (Data Definition Language): It is used to define database objects, including databases, tables, views, and indexes. -{{< copyable "sql" >}} +- DML (Data Manipulation Language): It is used to manipulate application related records. -```sql -CREATE DATABASE db_name [options]; -``` +- DQL (Data Query Language):It is used to query the records after conditional filtering. -For example, to create a database named `samp_db`: +- DCL (Data Control Language): It is used to define access privileges and security levels. -{{< copyable "sql" >}} +Common DDL features are creating, modifying, and deleting objects (such as tables and indexes). The corresponding commands are `CREATE`, `ALTER`, and `DROP`. -```sql -CREATE DATABASE IF NOT EXISTS samp_db; -``` +## Show, create and drop a database -### Show the databases +Database in TiDB can be considered as a collection of objects such as tables and indexes. -To show the databases, use the `SHOW DATABASES` statement: +To show the list of databases, use the `SHOW DATABASES` statement: {{< copyable "sql" >}} @@ -42,133 +38,119 @@ To show the databases, use the `SHOW DATABASES` statement: SHOW DATABASES; ``` -### Delete a database - -To delete a database, use the `DROP DATABASE` statement: +To use the database named `mysql`, use the following statement: {{< copyable "sql" >}} ```sql -DROP DATABASE samp_db; +use mysql; ``` -## Create, show, and drop a table - -### Create a table - -- To create a table, use the `CREATE TABLE` statement: +To show all the tables in a database, use the `SHOW TABLES` statement: - {{< copyable "sql" >}} +{{< copyable "sql" >}} - ```sql - CREATE TABLE table_name column_name data_type constraint; - ``` +```sql +SHOW TABLES FROM mysql; +``` - For example: +To create a database, use the `CREATE DATABASE` statement: - {{< copyable "sql" >}} +{{< copyable "sql" >}} - ```sql - CREATE TABLE person ( - number INT(11), - name VARCHAR(255), - birthday DATE - ); - ``` +```sql +CREATE DATABASE db_name [options]; +``` -- Add `IF NOT EXISTS` to prevent an error if the table exists: +To create a database named `samp_db`, use the following statement: - {{< copyable "sql" >}} +{{< copyable "sql" >}} - ```sql - CREATE TABLE IF NOT EXISTS person ( - number INT(11), - name VARCHAR(255), - birthday DATE - ); - ``` +```sql +CREATE DATABASE IF NOT EXISTS samp_db; +``` -- To view the statement that creates the table, use the `SHOW CREATE` statement: +Add `IF NOT EXISTS` to prevent an error if the database exists. - {{< copyable "sql" >}} +To delete a database, use the `DROP DATABASE` statement: - ```sql - SHOW CREATE table person; - ``` +{{< copyable "sql" >}} -### Show the tables +```sql +DROP DATABASE samp_db; +``` -- To show all the tables in a database, use the `SHOW TABLES` statement: +## Create, show, and drop a table - {{< copyable "sql" >}} +To create a table, use the `CREATE TABLE` statement: - ```sql - SHOW TABLES FROM samp_db; - ``` +{{< copyable "sql" >}} -- To show all the columns in a table, use the `SHOW FULL COLUMNS` statement: +```sql +CREATE TABLE table_name column_name data_type constraint; +``` - {{< copyable "sql" >}} +For example, to create a table named `person` which includes fields such as number, name, and birthday, use the following statement: - ```sql - SHOW FULL COLUMNS FROM person; - ``` +{{< copyable "sql" >}} -### Delete a table +```sql +CREATE TABLE person ( + id INT(11), + name VARCHAR(255), + birthday DATE + ); +``` -To delete a table, use the `DROP TABLE` statement: +To view the statement that creates the table (DDL), use the `SHOW CREATE` statement: {{< copyable "sql" >}} ```sql -DROP TABLE person; +SHOW CREATE table person; ``` -or +To delete a table, use the `DROP TABLE` statement: {{< copyable "sql" >}} ```sql -DROP TABLE IF EXISTS person; +DROP TABLE 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: - - {{< copyable "sql" >}} +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` or `ALTER TABLE` statement: - ```sql - CREATE INDEX person_num ON person (number); - ``` +{{< copyable "sql" >}} - or +```sql +CREATE INDEX person_id ON person (id); +``` - {{< copyable "sql" >}} +Or - ```sql - ALTER TABLE person ADD INDEX person_num (number); - ``` +{{< copyable "sql" >}} -- To create a unique index for the column whose value is unique, use the `CREATE UNIQUE INDEX` or `ALTER TABLE` statement: +```sql +ALTER TABLE person ADD INDEX person_id (id); +``` - {{< copyable "sql" >}} +To create a unique index for the column whose value is unique, use the `CREATE UNIQUE INDEX` statement: - ```sql - CREATE UNIQUE INDEX person_num ON person (number); - ``` +{{< copyable "sql" >}} - or +```sql +CREATE UNIQUE INDEX person_unique_id ON person (id); +``` - {{< copyable "sql" >}} +Or use the `ALTER TABLE` statement: - ```sql - ALTER TABLE person ADD UNIQUE person_num (number); - ``` +{{< copyable "sql" >}} -### Show the indexes +```sql +ALTER TABLE person ADD UNIQUE person_unique_id (id); +``` To show all the indexes in a table, use the `SHOW INDEX` statement: @@ -178,25 +160,27 @@ 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: +To delete an index, use the `DROP INDEX` or `ALTER TABLE` statement. `DROP INDEX` can be nested in `ALTER TABLE`: {{< copyable "sql" >}} ```sql -DROP INDEX person_num ON person; +DROP INDEX person_id ON person; ``` {{< copyable "sql" >}} ```sql -ALTER TABLE person DROP INDEX person_num; +ALTER TABLE person DROP INDEX person_unique_id; ``` -## Insert, select, update, and delete data +> **Note:** +> +> DDL operations are not transactions. You don't need to correspond COMMIT statements when executing DDL. + +Common DML features are adding, modifying, and deleting table records. The corresponding commands are `INSERT`, `UPDATE`, and `DELETE`. -### Insert data +## Insert, update, and delete data To insert data into a table, use the `INSERT` statement: @@ -206,71 +190,70 @@ 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: +To insert a record containing data of some fields into a table, use the `INSERT` statement: {{< copyable "sql" >}} ```sql -SELECT * FROM person; -``` - -``` -+--------+------+------------+ -| number | name | birthday | -+--------+------+------------+ -| 1 | tom | 2017-09-12 | -+--------+------+------------+ +INSERT INTO person(id,name) VALUES("2","bob"); ``` -### Update data - -To update the data in a table, use the `UPDATE` statement: +To update some fields of a record in a table, use the `UPDATE` statement: {{< copyable "sql" >}} ```sql -UPDATE person SET birthday='20171010' WHERE name='tom'; +UPDATE person SET birthday='20180808' WHERE id=2; ``` +To delete the data in a table, use the `DELETE` statement: + {{< copyable "sql" >}} ```sql -SELECT * FROM person; +DELETE FROM person WHERE id=2; ``` -``` -+--------+------+------------+ -| number | name | birthday | -+--------+------+------------+ -| 1 | tom | 2017-10-10 | -+--------+------+------------+ -``` +> **Note:** +> +> The `UPDATE` and `DELETE` statements without the `WHERE` clause as a filter operate on the entire table. -### Delete data +DQL is used to retrieve the desired data rows from a table or multiple tables. -To delete the data in a table, use the `DELETE` statement: +## Query data + +To view the data in a table, use the `SELECT` statement: {{< copyable "sql" >}} ```sql -DELETE FROM person WHERE number=1; +SELECT * FROM person; ``` +Add the column name to be queried after the `SELECT` statement: + {{< copyable "sql" >}} ```sql -SELECT * FROM person; +SELECT name FROM person; ++------+ +| name | ++------+ +| tom | ++------+ ``` -``` -Empty set (0.00 sec) +Use the `WHERE` clause to filter all records that match the conditions and then return the result: + +{{< copyable "sql" >}} + +```sql +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. -### Create a user +## Create, authorize, and delete a user To create a user, use the `CREATE USER` statement. The following example creates a user named `tiuser` with the password `123456`: @@ -280,25 +263,21 @@ To create a user, use the `CREATE USER` statement. The following example creates CREATE USER 'tiuser'@'localhost' IDENTIFIED BY '123456'; ``` -### Authorize a user +To grant `tiuser` the privilege to retrieve the tables in the `samp_db` database: -- To grant `tiuser` the privilege to retrieve the tables in the `samp_db` database: - - {{< copyable "sql" >}} - - ```sql - GRANT SELECT ON samp_db.* TO 'tiuser'@'localhost'; - ``` +{{< copyable "sql" >}} -- To check the privileges of `tiuser`: +```sql +GRANT SELECT ON samp_db.* TO 'tiuser'@'localhost'; +``` - {{< copyable "sql" >}} +To check the privileges of `tiuser`: - ```sql - SHOW GRANTS for tiuser@localhost; - ``` +{{< copyable "sql" >}} -### Delete a user +```sql +SHOW GRANTS for tiuser@localhost; +``` To delete `tiuser`: From 44e78f49f2e3a7f38dfcc62e55c570e8fb3a5e9b Mon Sep 17 00:00:00 2001 From: ireneontheway <48651140+ireneontheway@users.noreply.github.com> Date: Thu, 23 Jul 2020 19:23:56 +0800 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: Ran --- basic-sql-operations.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/basic-sql-operations.md b/basic-sql-operations.md index f6f4d722a6e48..7e8a12eb91403 100644 --- a/basic-sql-operations.md +++ b/basic-sql-operations.md @@ -20,7 +20,7 @@ SQL is divided into the following 4 types according to their functions: - 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. +- 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. @@ -176,7 +176,7 @@ ALTER TABLE person DROP INDEX person_unique_id; > **Note:** > -> DDL operations are not transactions. You don't need to correspond COMMIT statements when executing DDL. +> DDL operations are not transactions. You don't need to run a `COMMIT` statement when executing DDL operations. Common DML features are adding, modifying, and deleting table records. The corresponding commands are `INSERT`, `UPDATE`, and `DELETE`. @@ -230,7 +230,7 @@ To view the data in a table, use the `SELECT` statement: SELECT * FROM person; ``` -Add the column name to be queried after the `SELECT` statement: +To query a specific column, add the column name after the `SELECT` keyword: {{< copyable "sql" >}} From e60643a9fc59fbc4502727465b8193e00dee9374 Mon Sep 17 00:00:00 2001 From: ireneontheway <48651140+ireneontheway@users.noreply.github.com> Date: Thu, 23 Jul 2020 19:26:06 +0800 Subject: [PATCH 3/5] Update basic-sql-operations.md --- basic-sql-operations.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/basic-sql-operations.md b/basic-sql-operations.md index 7e8a12eb91403..80d82fc67589e 100644 --- a/basic-sql-operations.md +++ b/basic-sql-operations.md @@ -1,10 +1,10 @@ --- -title: Basic SQL Operations +title: Explore SQL with TiDB summary: Learn about the basic SQL statements for the TiDB database. aliases: ['/docs/dev/basic-sql-operations/','/docs/dev/how-to/get-started/explore-sql/'] --- -# Basic SQL Operations +# 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](/mysql-compatibility.md#unsupported-features). @@ -120,7 +120,7 @@ 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` or `ALTER TABLE` statement: +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: {{< copyable "sql" >}} @@ -128,7 +128,7 @@ Indexes are used to speed up queries on indexed columns. To create an index for CREATE INDEX person_id ON person (id); ``` -Or +Or use the `ALTER TABLE` statement: {{< copyable "sql" >}} From 53098e4c013015bd6d63a7a95e98336b3eb40b64 Mon Sep 17 00:00:00 2001 From: ireneontheway <48651140+ireneontheway@users.noreply.github.com> Date: Fri, 24 Jul 2020 11:54:44 +0800 Subject: [PATCH 4/5] Update basic-sql-operations.md --- basic-sql-operations.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/basic-sql-operations.md b/basic-sql-operations.md index 80d82fc67589e..32e46d619cf83 100644 --- a/basic-sql-operations.md +++ b/basic-sql-operations.md @@ -178,10 +178,10 @@ 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. -Common DML features are adding, modifying, and deleting table records. The corresponding commands are `INSERT`, `UPDATE`, and `DELETE`. - ## 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: {{< copyable "sql" >}} @@ -218,10 +218,10 @@ DELETE FROM person WHERE id=2; > > The `UPDATE` and `DELETE` statements without the `WHERE` clause as a filter operate on the entire table. -DQL is used to retrieve the desired data rows from a table or multiple tables. - ## 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: {{< copyable "sql" >}} @@ -251,10 +251,10 @@ Use the `WHERE` clause to filter all records that match the conditions and then SELECT * FROM person where id<5; ``` -DCL are usually used to create or delete users, and manage user privileges. - ## 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`: {{< copyable "sql" >}} From c90b90c136d8ad5acf93a95f91d21806cfaf7e38 Mon Sep 17 00:00:00 2001 From: Ran Date: Tue, 28 Jul 2020 15:55:48 +0800 Subject: [PATCH 5/5] Update basic-sql-operations.md --- basic-sql-operations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basic-sql-operations.md b/basic-sql-operations.md index 32e46d619cf83..34847208abe50 100644 --- a/basic-sql-operations.md +++ b/basic-sql-operations.md @@ -28,7 +28,7 @@ Common DDL features are creating, modifying, and deleting objects (such as table ## Show, create and drop a database -Database in TiDB can be considered as a collection of objects such as tables and indexes. +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: