diff --git a/functions-and-operators/set-operators.md b/functions-and-operators/set-operators.md index ce36ed1529db9..e5177dcf7012a 100644 --- a/functions-and-operators/set-operators.md +++ b/functions-and-operators/set-operators.md @@ -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 | +---+ @@ -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 | +---+ @@ -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 | +---+ @@ -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 | +---+ @@ -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 | +---+ @@ -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 | +---+ @@ -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 | +---+ @@ -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 | +---+ diff --git a/sql-statements/sql-statement-admin-checksum-table.md b/sql-statements/sql-statement-admin-checksum-table.md index 80d1ff05028d9..a20fd802aef83 100644 --- a/sql-statements/sql-statement-admin-checksum-table.md +++ b/sql-statements/sql-statement-admin-checksum-table.md @@ -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 | +---------+------------+----------------------+-----------+-------------+ diff --git a/sql-statements/sql-statement-create-role.md b/sql-statements/sql-statement-create-role.md index 985dcf4c3b916..7b5cd58ba4f29 100644 --- a/sql-statements/sql-statement-create-role.md +++ b/sql-statements/sql-statement-create-role.md @@ -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 | +---------------------------------------------+ @@ -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 | +----------------+ @@ -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 | +----------------+ @@ -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 `](/sql-statements/sql-statement-grant-role.md) * [`REVOKE `](/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) diff --git a/sql-statements/sql-statement-drop-role.md b/sql-statements/sql-statement-drop-role.md index ccd1032f3a4f0..7fcbf62ef2d98 100644 --- a/sql-statements/sql-statement-drop-role.md +++ b/sql-statements/sql-statement-drop-role.md @@ -19,52 +19,38 @@ RolenameList ::= ## 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: - -```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. +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. +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 | +---------------------------------------------+ @@ -73,22 +59,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 | +----------------+ @@ -97,51 +83,39 @@ mysql> SHOW TABLES IN test; 1 row in set (0.00 sec) ``` -The statement `SET DEFAULT ROLE` can be used to associated 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 | +----------------+ @@ -150,43 +124,31 @@ mysql> SHOW TABLES IN test; 1 row in set (0.00 sec) ``` -Drop the role for the analyticsteam: - -```sql -$ mysql -uroot -Welcome to the MySQL monitor. Commands end with ; or \g. -Your MySQL connection id is 41 -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 `root` 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 root +``` -Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. +Drop the role for the `analyticsteam`: -mysql> DROP ROLE analyticsteam; +```sql +DROP ROLE analyticsteam; Query OK, 0 rows affected (0.02 sec) ``` -Jennifer no longer has the default role of analyticsteam associated, or can set the role to analyticsteam: +`jennifer` no longer has the default role of `analyticsteam` associated, nor can set the role to `analyticsteam`. -```sql -$ mysql -ujennifer -Welcome to the MySQL monitor. Commands end with ; or \g. -Your MySQL connection id is 42 -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 +Connect to TiDB as the `jennifer` user: -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. +Show the privileges of `jennifer`: -mysql> SHOW GRANTS; +```sql +SHOW GRANTS; +--------------------------------------+ | Grants for User | +--------------------------------------+ @@ -194,7 +156,7 @@ mysql> SHOW GRANTS; +--------------------------------------+ 1 row in set (0.00 sec) -mysql> SET ROLE analyticsteam; +SET ROLE analyticsteam; ERROR 3530 (HY000): `analyticsteam`@`%` is is not granted to jennifer@% ``` @@ -204,11 +166,11 @@ This statement is understood to be fully compatible with roles, which are a feat ## See also -* [CREATE ROLE](/sql-statements/sql-statement-create-role.md) +* [`CREATE ROLE`](/sql-statements/sql-statement-create-role.md) * [`GRANT `](/sql-statements/sql-statement-grant-role.md) * [`REVOKE `](/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) diff --git a/sql-statements/sql-statement-grant-role.md b/sql-statements/sql-statement-grant-role.md index 5f1dadbe948ab..02e8ac7f40720 100644 --- a/sql-statements/sql-statement-grant-role.md +++ b/sql-statements/sql-statement-grant-role.md @@ -22,52 +22,38 @@ UsernameList ::= ## 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 | +---------------------------------------------+ @@ -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 | +----------------+ @@ -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 associated 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 | +----------------+ @@ -160,11 +134,11 @@ This statement is understood to be fully compatible with roles, which are a feat ## See also * [`GRANT `](/sql-statements/sql-statement-grant-privileges.md) -* [CREATE ROLE](/sql-statements/sql-statement-create-role.md) -* [DROP ROLE](/sql-statements/sql-statement-drop-role.md) +* [`CREATE ROLE`](/sql-statements/sql-statement-create-role.md) +* [`DROP ROLE`](/sql-statements/sql-statement-drop-role.md) * [`REVOKE `](/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) diff --git a/sql-statements/sql-statement-revoke-role.md b/sql-statements/sql-statement-revoke-role.md index 12264ae44a4fd..e3765cfca91ae 100644 --- a/sql-statements/sql-statement-revoke-role.md +++ b/sql-statements/sql-statement-revoke-role.md @@ -22,52 +22,38 @@ UsernameList ::= ## 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: - -```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. +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. +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 | +---------------------------------------------+ @@ -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 | +----------------+ @@ -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 associated 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: - -```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 +Connect to TiDB as the `root` user: -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 | +----------------+ @@ -153,41 +127,29 @@ mysql> SHOW TABLES IN test; 1 row in set (0.00 sec) ``` -Revoke the role of analyticsteam from `jennifer`: - -```sql -$ mysql -uroot -Welcome to the MySQL monitor. Commands end with ; or \g. -Your MySQL connection id is 38 -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 `root` 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 root +``` -Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. +Revoke the role of analyticsteam from `jennifer`: -mysql> REVOKE analyticsteam FROM jennifer; +```sql +REVOKE analyticsteam FROM jennifer; Query OK, 0 rows affected (0.01 sec) ``` -```sql -$ mysql -ujennifer -Welcome to the MySQL monitor. Commands end with ; or \g. -Your MySQL connection id is 39 -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 +Connect to TiDB as the `jennifer` user: -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. +Show the privileges of `jennifer`: -mysql> SHOW GRANTS; +```sql +SHOW GRANTS; +--------------------------------------+ | Grants for User | +--------------------------------------+ @@ -202,11 +164,11 @@ This statement is understood to be fully compatible with roles, which are a feat ## See also -* [CREATE ROLE](/sql-statements/sql-statement-create-role.md) -* [DROP ROLE](/sql-statements/sql-statement-drop-role.md) +* [`CREATE ROLE`](/sql-statements/sql-statement-create-role.md) +* [`DROP ROLE`](/sql-statements/sql-statement-drop-role.md) * [`GRANT `](/sql-statements/sql-statement-grant-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) diff --git a/sql-statements/sql-statement-set-default-role.md b/sql-statements/sql-statement-set-default-role.md index ba56a6bb7af90..1b114a8c05d46 100644 --- a/sql-statements/sql-statement-set-default-role.md +++ b/sql-statements/sql-statement-set-default-role.md @@ -27,52 +27,38 @@ This statement sets a specific role to be applied to a user by default. Thus, th ## 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 | +---------------------------------------------+ @@ -81,12 +67,12 @@ 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 | +---------------------------------------------+ @@ -96,7 +82,7 @@ mysql> SHOW GRANTS; +---------------------------------------------+ 3 rows in set (0.00 sec) -mysql> SHOW TABLES IN test; +SHOW TABLES IN test; +----------------+ | Tables_in_test | +----------------+ @@ -105,41 +91,29 @@ mysql> SHOW TABLES IN test; 1 row in set (0.00 sec) ``` -The statement `SET DEFAULT ROLE` can be used to associated 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 | +---------------------------------------------+ @@ -149,7 +123,7 @@ mysql> SHOW GRANTS; +---------------------------------------------+ 3 rows in set (0.00 sec) -mysql> SHOW TABLES IN test; +SHOW TABLES IN test; +----------------+ | Tables_in_test | +----------------+ @@ -161,7 +135,7 @@ mysql> SHOW TABLES IN test; `SET DEFAULT ROLE` will not automatically `GRANT` the associated role to the user. Attempting to `SET DEFAULT ROLE` for a role that `jennifer` does not have granted results in the following error: ```sql -mysql> SET DEFAULT ROLE analyticsteam TO jennifer; +SET DEFAULT ROLE analyticsteam TO jennifer; ERROR 3530 (HY000): `analyticsteam`@`%` is is not granted to jennifer@% ``` @@ -171,11 +145,11 @@ This statement is understood to be fully compatible with roles, which are a feat ## See also -* [CREATE ROLE](/sql-statements/sql-statement-create-role.md) -* [DROP ROLE](/sql-statements/sql-statement-drop-role.md) +* [`CREATE ROLE`](/sql-statements/sql-statement-create-role.md) +* [`DROP ROLE`](/sql-statements/sql-statement-drop-role.md) * [`GRANT `](/sql-statements/sql-statement-grant-role.md) * [`REVOKE `](/sql-statements/sql-statement-revoke-role.md) -* [SET ROLE](/sql-statements/sql-statement-set-role.md) +* [`SET ROLE`](/sql-statements/sql-statement-set-role.md) diff --git a/sql-statements/sql-statement-table.md b/sql-statements/sql-statement-table.md index 2bdba64f50f37..1de4078e00dd5 100644 --- a/sql-statements/sql-statement-table.md +++ b/sql-statements/sql-statement-table.md @@ -16,28 +16,19 @@ TableStmt ::= ## Examples -{{< copyable "sql" >}} +Create table `t1`: ```sql CREATE TABLE t1(id INT PRIMARY KEY); ``` -```sql -Query OK, 0 rows affected (0.31 sec) -``` - -{{< copyable "sql" >}} +Insert some data into `t1`: ```sql INSERT INTO t1 VALUES (1),(2),(3); ``` -```sql -Query OK, 3 rows affected (0.06 sec) -Records: 3 Duplicates: 0 Warnings: 0 -``` - -{{< copyable "sql" >}} +View the data in table `t1`: ```sql TABLE t1; @@ -54,7 +45,7 @@ TABLE t1; 3 rows in set (0.01 sec) ``` -{{< copyable "sql" >}} +Query `t1` and sort the result by the `id` field in descending order: ```sql TABLE t1 ORDER BY id DESC; @@ -71,7 +62,7 @@ TABLE t1 ORDER BY id DESC; 3 rows in set (0.01 sec) ``` -{{< copyable "sql" >}} +Query the first record in `t1`: ```sql TABLE t1 LIMIT 1; @@ -92,5 +83,5 @@ The `TABLE` statement was introduced in MySQL 8.0.19. ## See also -- [SELECT](/sql-statements/sql-statement-select.md) -- [TABLE statements in MySQL](https://dev.mysql.com/doc/refman/8.0/en/table.html) +- [`SELECT`](/sql-statements/sql-statement-select.md) +- [`TABLE` statements in MySQL](https://dev.mysql.com/doc/refman/8.0/en/table.html)