|
| 1 | +```markdown |
| 2 | +title: Column-Level Privilege Management |
| 3 | +summary: TiDB supports a MySQL-compatible column-level privilege management mechanism, allowing you to grant or revoke `SELECT`, `INSERT`, `UPDATE`, and `REFERENCES` privileges on specific columns of a table using `GRANT` or `REVOKE`, thus achieving finer-grained access control. |
| 4 | +--- |
| 5 | + |
| 6 | +# Column-Level Privilege Management |
| 7 | + |
| 8 | +Starting from version v8.5.6, TiDB supports a MySQL-compatible column-level privilege management mechanism. With column-level privileges, you can grant or revoke `SELECT`, `INSERT`, `UPDATE`, and `REFERENCES` privileges on specific columns of a table, achieving finer-grained data access control. |
| 9 | + |
| 10 | +> **Note:** |
| 11 | +> |
| 12 | +> Although MySQL syntax allows column-level specification like `REFERENCES(col_name)`, `REFERENCES` itself is a database/table-level privilege used for foreign key-related permission checks. Therefore, column-level `REFERENCES` does not actually take effect in MySQL. TiDB's behavior is consistent with MySQL. |
| 13 | +
|
| 14 | +## Syntax |
| 15 | + |
| 16 | +Granting and revoking column-level privileges are similar to table-level privileges, with the following differences: |
| 17 | + |
| 18 | +- The column name list is placed after the **privilege type**, not after the **table name**. |
| 19 | +- Multiple column names are separated by commas (`,`). |
| 20 | + |
| 21 | +```sql |
| 22 | +GRANT priv_type(col_name [, col_name] ...) [, priv_type(col_name [, col_name] ...)] ... |
| 23 | + ON db_name.tbl_name |
| 24 | + TO 'user'@'host'; |
| 25 | + |
| 26 | +REVOKE priv_type(col_name [, col_name] ...) [, priv_type(col_name [, col_name] ...)] ... |
| 27 | + ON db_name.tbl_name |
| 28 | + FROM 'user'@'host'; |
| 29 | +``` |
| 30 | + |
| 31 | +Where: |
| 32 | + |
| 33 | +* `priv_type` supports `SELECT`, `INSERT`, `UPDATE`, and `REFERENCES`. |
| 34 | +* The `ON` clause requires specifying the specific table, for example, `test.tbl`. |
| 35 | +* A single `GRANT` or `REVOKE` statement can include multiple privilege items, and each privilege item can specify its own list of column names. |
| 36 | + |
| 37 | +For example, the following statement grants `SELECT` privileges on `col1` and `col2` and `UPDATE` privilege on `col3` to the user: |
| 38 | + |
| 39 | +```sql |
| 40 | +GRANT SELECT(col1, col2), UPDATE(col3) ON test.tbl TO 'user'@'host'; |
| 41 | +``` |
| 42 | + |
| 43 | +## Granting Column-Level Privileges |
| 44 | + |
| 45 | +The following example grants the `SELECT` privilege on `col1` and `col2` of table `test.tbl` to user `newuser`, and grants the `UPDATE` privilege on `col3` to the same user: |
| 46 | + |
| 47 | +```sql |
| 48 | +CREATE DATABASE IF NOT EXISTS test; |
| 49 | +USE test; |
| 50 | + |
| 51 | +DROP TABLE IF EXISTS tbl; |
| 52 | +CREATE TABLE tbl (col1 INT, col2 INT, col3 INT); |
| 53 | + |
| 54 | +DROP USER IF EXISTS 'newuser'@'%'; |
| 55 | +CREATE USER 'newuser'@'%'; |
| 56 | + |
| 57 | +GRANT SELECT(col1, col2), UPDATE(col3) ON test.tbl TO 'newuser'@'%'; |
| 58 | +SHOW GRANTS FOR 'newuser'@'%'; |
| 59 | +``` |
| 60 | + |
| 61 | +``` |
| 62 | ++---------------------------------------------------------------------+ |
| 63 | +| Grants for newuser@% | |
| 64 | ++---------------------------------------------------------------------+ |
| 65 | +| GRANT USAGE ON *.* TO 'newuser'@'%' | |
| 66 | +| GRANT SELECT(col1, col2), UPDATE(col3) ON test.tbl TO 'newuser'@'%' | |
| 67 | ++---------------------------------------------------------------------+ |
| 68 | +``` |
| 69 | +
|
| 70 | +In addition to using `SHOW GRANTS`, you can also view column-level privilege information by querying [`INFORMATION_SCHEMA.COLUMN_PRIVILEGES`](/information-schema/information-schema-columns.md). |
| 71 | +
|
| 72 | +## Revoking Column-Level Privileges |
| 73 | +
|
| 74 | +The following example revokes the `SELECT` privilege on column `col2` from user `newuser`: |
| 75 | +
|
| 76 | +```sql |
| 77 | +REVOKE SELECT(col2) ON test.tbl FROM 'newuser'@'%'; |
| 78 | +SHOW GRANTS FOR 'newuser'@'%'; |
| 79 | +``` |
| 80 | + |
| 81 | +``` |
| 82 | ++---------------------------------------------------------------+ |
| 83 | +| Grants for newuser@% | |
| 84 | ++---------------------------------------------------------------+ |
| 85 | +| GRANT USAGE ON *.* TO 'newuser'@'%' | |
| 86 | +| GRANT SELECT(col1), UPDATE(col3) ON test.tbl TO 'newuser'@'%' | |
| 87 | ++---------------------------------------------------------------+ |
| 88 | +``` |
| 89 | + |
| 90 | +## Column-Level Privilege Access Control Example |
| 91 | + |
| 92 | +After granting or revoking column-level privileges, TiDB performs privilege checks on columns referenced in SQL statements. For example: |
| 93 | + |
| 94 | +* `SELECT` statements: `SELECT` column privileges affect columns referenced in the `SELECT` list as well as `WHERE`, `ORDER BY`, and other clauses. |
| 95 | +* `UPDATE` statements: Columns being updated in the `SET` clause require `UPDATE` column privileges. Columns read in expressions or conditions usually also require `SELECT` column privileges. |
| 96 | +* `INSERT` statements: Columns being written to require `INSERT` column privileges. `INSERT INTO t VALUES (...)` is equivalent to writing to all columns. |
| 97 | + |
| 98 | +In the following example, user `newuser` can only query `col1` and update `col3`: |
| 99 | + |
| 100 | +```sql |
| 101 | +-- Execute as newuser |
| 102 | +SELECT col1 FROM tbl; |
| 103 | +SELECT * FROM tbl; -- Error (missing SELECT column privilege for col2, col3) |
| 104 | + |
| 105 | +UPDATE tbl SET col3 = 1; |
| 106 | +UPDATE tbl SET col1 = 2; -- Error (missing UPDATE column privilege for col1) |
| 107 | + |
| 108 | +UPDATE tbl SET col3 = col1; |
| 109 | +UPDATE tbl SET col3 = col3 + 1; -- Error (missing SELECT column privilege for col3) |
| 110 | +UPDATE tbl SET col3 = col1 WHERE col1 > 0; |
| 111 | +``` |
| 112 | + |
| 113 | +## Compatibility Differences with MySQL |
| 114 | + |
| 115 | +TiDB's column-level privileges are generally compatible with MySQL. However, there are differences in the following scenarios: |
| 116 | +| Scenario | TiDB | MySQL | |
| 117 | +| :--------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 118 | +| Revoking column-level privileges not granted to a user | `REVOKE` executes successfully. | `REVOKE` throws an error. | |
| 119 | +| Execution order of column pruning and `SELECT` privilege check | `SELECT` column privileges are checked first, then column pruning is performed. For example: executing `SELECT a FROM (SELECT a, b FROM t) s` requires `SELECT` column privileges for both `t.a` and `t.b`. | Column pruning is performed first, then `SELECT` column privileges are checked. For example: executing `SELECT a FROM (SELECT a, b FROM t) s` only requires `SELECT` column privilege for `t.a`. | |
| 120 | + |
| 121 | +### Column Pruning and Privilege Checks in View Scenarios |
| 122 | + |
| 123 | +When performing `SELECT` privilege checks on views, MySQL first prunes columns in the view's internal query and then checks the column privileges of the internal tables, making the checks relatively lenient in some scenarios. TiDB does not perform column pruning before privilege checks, so additional column privileges might be required. |
| 124 | + |
| 125 | +```sql |
| 126 | +-- Prepare the environment by logging in as root |
| 127 | +DROP USER IF EXISTS 'u'@'%'; |
| 128 | +CREATE USER 'u'@'%'; |
| 129 | + |
| 130 | +DROP TABLE IF EXISTS t; |
| 131 | +CREATE TABLE t (a INT, b INT, c INT, d INT); |
| 132 | + |
| 133 | +DROP VIEW IF EXISTS v; |
| 134 | +CREATE SQL SECURITY INVOKER VIEW v AS SELECT a, b FROM t WHERE c = 0 ORDER BY d; |
| 135 | + |
| 136 | +GRANT SELECT ON v TO 'u'@'%'; |
| 137 | + |
| 138 | +-- Log in as u |
| 139 | +SELECT a FROM v; |
| 140 | +-- MySQL: Error, missing access privileges for t.a, t.c, t.d |
| 141 | +-- TiDB: Error, missing access privileges for t.a, t.b, t.c, t.d |
| 142 | + |
| 143 | +-- Log in as root |
| 144 | +GRANT SELECT(a, c, d) ON t TO 'u'@'%'; |
| 145 | + |
| 146 | +-- Log in as u |
| 147 | +SELECT a FROM v; |
| 148 | +-- MySQL: Success (internal query is pruned to `SELECT a FROM t WHERE c = 0 ORDER BY d`) |
| 149 | +-- TiDB: Error, missing access privileges for t.b |
| 150 | + |
| 151 | +SELECT * FROM v; |
| 152 | +-- MySQL: Error, missing access privileges for t.b |
| 153 | +-- TiDB: Error, missing access privileges for t.b |
| 154 | + |
| 155 | +-- Log in as root |
| 156 | +GRANT SELECT(b) ON t TO 'u'@'%'; |
| 157 | + |
| 158 | +-- Log in as u |
| 159 | +SELECT * FROM v; |
| 160 | +-- MySQL: Success |
| 161 | +-- TiDB: Success |
| 162 | +``` |
| 163 | + |
| 164 | +## See Also |
| 165 | + |
| 166 | +* [Privilege Management](/privilege-management.md) |
| 167 | +* [`GRANT <privileges>`](/sql-statements/sql-statement-grant-privileges.md) |
| 168 | +* [`REVOKE <privileges>`](/sql-statements/sql-statement-revoke-privileges.md) |
| 169 | +``` |
0 commit comments