Skip to content

Commit

Permalink
Update the command and output in SQL statements (#11807) (#11858)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot committed Dec 30, 2022
1 parent 51d70f3 commit 319af1f
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 431 deletions.
29 changes: 14 additions & 15 deletions functions-and-operators/set-operators.md
Expand Up @@ -12,7 +12,7 @@ TiDB supports three set operations using the UNION, EXCEPT, and INTERSECT operat
In mathematics, the union of two sets A and B consists of all elements that are in A or in B. For example:

```sql
select 1 union select 2;
SELECT 1 UNION SELECT 2;
+---+
| 1 |
+---+
Expand All @@ -24,19 +24,17 @@ select 1 union select 2;

TiDB supports both `UNION DISTINCT` and `UNION ALL` operators. `UNION DISTINCT` removes duplicate records from the result set, while `UNION ALL` keeps all records including duplicates. `UNION DISTINCT` is used by default in TiDB.

{{< copyable "sql" >}}

```sql
create table t1 (a int);
create table t2 (a int);
insert into t1 values (1),(2);
insert into t2 values (1),(3);
CREATE TABLE t1 (a int);
CREATE TABLE t2 (a int);
INSERT INTO t1 VALUES (1),(2);
INSERT INTO t2 VALUES (1),(3);
```

Examples for `UNION DISTINCT` and `UNION ALL` queries are respectively as follows:

```sql
select * from t1 union distinct select * from t2;
SELECT * FROM t1 UNION DISTINCT SELECT * FROM t2;
+---+
| a |
+---+
Expand All @@ -45,7 +43,8 @@ select * from t1 union distinct select * from t2;
| 3 |
+---+
3 rows in set (0.00 sec)
select * from t1 union all select * from t2;

SELECT * FROM t1 UNION ALL SELECT * FROM t2;
+---+
| a |
+---+
Expand All @@ -62,7 +61,7 @@ select * from t1 union all select * from t2;
If A and B are two sets, EXCEPT returns the difference set of A and B which consists of elements that are in A but not in B.

```sql
select * from t1 except select * from t2;
SELECT * FROM t1 EXCEPT SELECT * FROM t2;
+---+
| a |
+---+
Expand All @@ -78,7 +77,7 @@ select * from t1 except select * from t2;
In mathematics, the intersection of two sets A and B consists of all elements that are both in A and B, and no other elements.

```sql
select * from t1 intersect select * from t2;
SELECT * FROM t1 INTERSECT SELECT * FROM t2;
+---+
| a |
+---+
Expand All @@ -90,7 +89,7 @@ select * from t1 intersect select * from t2;
`INTERSECT ALL` operator is not yet supported. INTERSECT operator has higher precedence over EXCEPT and UNION operators.

```sql
select * from t1 union all select * from t1 intersect select * from t2;
SELECT * FROM t1 UNION ALL SELECT * FROM t1 INTERSECT SELECT * FROM t2;
+---+
| a |
+---+
Expand All @@ -106,7 +105,7 @@ select * from t1 union all select * from t1 intersect select * from t2;
TiDB supports using parentheses to specify the precedence of set operations. Expressions in parentheses are processed first.

```sql
(select * from t1 union all select * from t1) intersect select * from t2;
(SELECT * FROM t1 UNION ALL SELECT * FROM t1) INTERSECT SELECT * FROM t2;
+---+
| a |
+---+
Expand All @@ -115,12 +114,12 @@ TiDB supports using parentheses to specify the precedence of set operations. Exp
1 rows in set (0.00 sec)
```

## Use `Order By` and `Limit`
## Use `ORDER BY` and `LIMIT`

TiDB supports using [`ORDER BY`](/media/sqlgram/OrderByOptional.png) or [`LIMIT`](/media/sqlgram/LimitClause.png) clause in set operations. These two clauses must be at the end of the entire statement.

```sql
(select * from t1 union all select * from t1 intersect select * from t2) order by a limit 2;
(SELECT * FROM t1 UNION ALL SELECT * FROM t1 INTERSECT SELECT * FROM t2) ORDER BY a LIMIT 2;
+---+
| a |
+---+
Expand Down
22 changes: 12 additions & 10 deletions sql-statements/sql-statement-admin-checksum-table.md
Expand Up @@ -20,25 +20,27 @@ TableNameList ::=

## Examples

Calculate the checksum for a table:
Create table `t1`:

{{< copyable "sql" >}}
```sql
CREATE TABLE t1(id INT PRIMARY KEY);
```

Insert some data into `t1`:

```sql
CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY auto_increment);
INSERT INTO t1 VALUES (1),(2),(3);
ADMIN CHECKSUM TABLE t1;
```

Calculate the checksum for `t1`:

```sql
mysql> CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY auto_increment);
Query OK, 0 rows affected (0.11 sec)
ADMIN CHECKSUM TABLE t1;
```

mysql> INSERT INTO t1 VALUES (1),(2),(3);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
The output is as follows:

mysql> ADMIN CHECKSUM TABLE t1;
```sql
+---------+------------+----------------------+-----------+-------------+
| Db_name | Table_name | Checksum_crc64_xor | Total_kvs | Total_bytes |
+---------+------------+----------------------+-----------+-------------+
Expand Down
108 changes: 41 additions & 67 deletions sql-statements/sql-statement-create-role.md
Expand Up @@ -22,52 +22,38 @@ RoleSpec ::=

## Examples

Create a new role for the analytics team, and a new user called `jennifer`:
Connect to TiDB as the `root` user:

```sql
$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.7.25-TiDB-v4.0.0-beta.2-728-ga9177fe84 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
```shell
mysql -h 127.0.0.1 -P 4000 -u root
```

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Create a new role `analyticsteam` and a new user `jennifer`:

mysql> CREATE ROLE analyticsteam;
```sql
CREATE ROLE analyticsteam;
Query OK, 0 rows affected (0.02 sec)

mysql> GRANT SELECT ON test.* TO analyticsteam;
GRANT SELECT ON test.* TO analyticsteam;
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE USER jennifer;
CREATE USER jennifer;
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT analyticsteam TO jennifer;
GRANT analyticsteam TO jennifer;
Query OK, 0 rows affected (0.01 sec)
```

Note that by default `jennifer` needs to `SET ROLE analyticsteam` in order to be able to use the privileges associated with the role:
Connect to TiDB as the `jennifer` user:

```sql
$ mysql -ujennifer
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.7.25-TiDB-v4.0.0-beta.2-728-ga9177fe84 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
```shell
mysql -h 127.0.0.1 -P 4000 -u jennifer
```

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Note that by default `jennifer` needs to execute `SET ROLE analyticsteam` in order to be able to use the privileges associated with the `analyticsteam` role:

mysql> SHOW GRANTS;
```sql
SHOW GRANTS;
+---------------------------------------------+
| Grants for User |
+---------------------------------------------+
Expand All @@ -76,22 +62,22 @@ mysql> SHOW GRANTS;
+---------------------------------------------+
2 rows in set (0.00 sec)

mysql> SHOW TABLES in test;
SHOW TABLES in test;
ERROR 1044 (42000): Access denied for user 'jennifer'@'%' to database 'test'
mysql> SET ROLE analyticsteam;
SET ROLE analyticsteam;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW GRANTS;
SHOW GRANTS;
+---------------------------------------------+
| Grants for User |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'jennifer'@'%' |
| GRANT Select ON test.* TO 'jennifer'@'%' |
| GRANT SELECT ON test.* TO 'jennifer'@'%' |
| GRANT 'analyticsteam'@'%' TO 'jennifer'@'%' |
+---------------------------------------------+
3 rows in set (0.00 sec)

mysql> SHOW TABLES IN test;
SHOW TABLES IN test;
+----------------+
| Tables_in_test |
+----------------+
Expand All @@ -100,51 +86,39 @@ mysql> SHOW TABLES IN test;
1 row in set (0.00 sec)
```

The statement `SET DEFAULT ROLE` can be used to associate a role to `jennifer` so that she will not have to execute the statement `SET ROLE` in order to assume the privileges associated with the role:
Connect to TiDB as the `root` user:

```sql
$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 5.7.25-TiDB-v4.0.0-beta.2-728-ga9177fe84 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
```shell
mysql -h 127.0.0.1 -P 4000 -u root
```

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
The statement `SET DEFAULT ROLE` can be used to associate the role `analyticsteam` to `jennifer`:

mysql> SET DEFAULT ROLE analyticsteam TO jennifer;
```sql
SET DEFAULT ROLE analyticsteam TO jennifer;
Query OK, 0 rows affected (0.02 sec)
```

```sql
$ mysql -ujennifer
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 35
Server version: 5.7.25-TiDB-v4.0.0-beta.2-728-ga9177fe84 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Connect to TiDB as the `jennifer` user:

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
```shell
mysql -h 127.0.0.1 -P 4000 -u jennifer
```

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
After this, the user `jennifer` has the privileges associated with the role `analyticsteam` and `jennifer` does not have to execute the statement `SET ROLE`:

mysql> SHOW GRANTS;
```sql
SHOW GRANTS;
+---------------------------------------------+
| Grants for User |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'jennifer'@'%' |
| GRANT Select ON test.* TO 'jennifer'@'%' |
| GRANT SELECT ON test.* TO 'jennifer'@'%' |
| GRANT 'analyticsteam'@'%' TO 'jennifer'@'%' |
+---------------------------------------------+
3 rows in set (0.00 sec)

mysql> SHOW TABLES IN test;
SHOW TABLES IN test;
+----------------+
| Tables_in_test |
+----------------+
Expand All @@ -159,11 +133,11 @@ This statement is understood to be fully compatible with roles, which are a feat

## See also

* [DROP ROLE](/sql-statements/sql-statement-drop-role.md)
* [`DROP ROLE`](/sql-statements/sql-statement-drop-role.md)
* [`GRANT <role>`](/sql-statements/sql-statement-grant-role.md)
* [`REVOKE <role>`](/sql-statements/sql-statement-revoke-role.md)
* [SET ROLE](/sql-statements/sql-statement-set-role.md)
* [SET DEFAULT ROLE](/sql-statements/sql-statement-set-default-role.md)
* [`SET ROLE`](/sql-statements/sql-statement-set-role.md)
* [`SET DEFAULT ROLE`](/sql-statements/sql-statement-set-default-role.md)

<CustomContent platform="tidb">

Expand Down

0 comments on commit 319af1f

Please sign in to comment.