Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reference/sql/statements: First Batch of Statement Reference #1067

Merged
merged 6 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
76 changes: 69 additions & 7 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,75 @@
- [Supported Client Drivers](sql/connection-and-APIs.md)
- [MySQL Compatibility](sql/mysql-compatibility.md)
+ SQL Statements
- [Data Definition Statements](sql/ddl.md)
- [Data Manipulation Statements](sql/dml.md)
- [Transactions](sql/transaction.md)
- [Database Administration Statements](sql/admin.md)
- [Prepared SQL Statement Syntax](sql/prepare.md)
- [Utility Statements](sql/util.md)
- [TiDB SQL Syntax Diagram](https://pingcap.github.io/sqlgram/)
- [`ADD COLUMN`](dev/reference/sql/statements/add-column.md)
- [`ADD INDEX`](dev/reference/sql/statements/add-index.md)
- [`ALTER TABLE`](dev/reference/sql/statements/alter-table.md)
- [`ALTER USER`](dev/reference/sql/statements/alter-user.md)
- [`ANALYZE TABLE`](dev/reference/sql/statements/analyze-table.md)
- [`BEGIN`](dev/reference/sql/statements/begin.md)
- [`COMMIT`](dev/reference/sql/statements/commit.md)
- [`CREATE DATABASE`](dev/reference/sql/statements/create-database.md)
- [`CREATE INDEX`](dev/reference/sql/statements/create-index.md)
- [`CREATE TABLE LIKE`](dev/reference/sql/statements/create-table-like.md)
- [`CREATE TABLE`](dev/reference/sql/statements/create-table.md)
- [`CREATE USER`](dev/reference/sql/statements/create-user.md)
- [`CREATE VIEW`](dev/reference/sql/statements/create-view.md)
- [`DEALLOCATE`](dev/reference/sql/statements/deallocate.md)
- [`DELETE`](dev/reference/sql/statements/delete.md)
- [`DESC`](dev/reference/sql/statements/desc.md)
- [`DESCRIBE`](dev/reference/sql/statements/describe.md)
- [`DO`](dev/reference/sql/statements/do.md)
- [`DROP COLUMN`](dev/reference/sql/statements/drop-column.md)
- [`DROP DATABASE`](dev/reference/sql/statements/drop-database.md)
- [`DROP INDEX`](dev/reference/sql/statements/drop-index.md)
- [`DROP TABLE`](dev/reference/sql/statements/drop-table.md)
- [`DROP USER`](dev/reference/sql/statements/drop-user.md)
- [`DROP VIEW`](dev/reference/sql/statements/drop-view.md)
- [`EXECUTE`](dev/reference/sql/statements/execute.md)
- [`EXPLAIN ANALYZE`](dev/reference/sql/statements/explain-analyze.md)
- [`EXPLAIN`](dev/reference/sql/statements/explain.md)
- [`FLUSH PRIVILEGES`](dev/reference/sql/statements/flush-privileges.md)
- [`FLUSH STATUS`](dev/reference/sql/statements/flush-status.md)
- [`FLUSH TABLES`](dev/reference/sql/statements/flush-tables.md)
- [`GRANT <privileges>`](dev/reference/sql/statements/grant-privileges.md)
- [`INSERT`](dev/reference/sql/statements/insert.md)
- [`KILL [TIDB]`](dev/reference/sql/statements/kill.md)
- [`LOAD DATA`](dev/reference/sql/statements/load-data.md)
- [`MODIFY COLUMN`](dev/reference/sql/statements/modify-column.md)
- [`PREPARE`](dev/reference/sql/statements/prepare.md)
- [`RENAME INDEX`](dev/reference/sql/statements/rename-index.md)
- [`RENAME TABLE`](dev/reference/sql/statements/rename-table.md)
- [`REPLACE`](dev/reference/sql/statements/replace.md)
- [`REVOKE <privileges>`](dev/reference/sql/statements/revoke-privileges.md)
- [`ROLLBACK`](dev/reference/sql/statements/rollback.md)
- [`SELECT`](dev/reference/sql/statements/select.md)
- [`SET TRANSACTION`](dev/reference/sql/statements/set-transaction.md)
- [`SET [GLOBAL|SESSION] <variable>`](dev/reference/sql/statements/set-variable.md)
- [`SHOW CHARACTER SET`](dev/reference/sql/statements/show-character-set.md)
- [`SHOW COLLATION`](dev/reference/sql/statements/show-collation.md)
- [`SHOW [FULL] COLUMNS FROM`](dev/reference/sql/statements/show-columns-from.md)
- [`SHOW CREATE TABLE`](dev/reference/sql/statements/show-create-table.md)
- [`SHOW CREATE USER`](dev/reference/sql/statements/show-create-user.md)
- [`SHOW DATABASES`](dev/reference/sql/statements/show-databases.md)
- [`SHOW ENGINES`](dev/reference/sql/statements/show-engines.md)
- [`SHOW ERRORS`](dev/reference/sql/statements/show-errors.md)
- [`SHOW [FULL] FIELDS FROM`](dev/reference/sql/statements/show-fields-from.md)
- [`SHOW GRANTS`](dev/reference/sql/statements/show-grants.md)
- [`SHOW INDEXES [FROM|IN]`](dev/reference/sql/statements/show-indexes.md)
- [`SHOW INDEX [FROM|IN]`](dev/reference/sql/statements/show-index.md)
- [`SHOW KEYS [FROM|IN]`](dev/reference/sql/statements/show-keys.md)
- [`SHOW PRIVILEGES`](dev/reference/sql/statements/show-privileges.md)
- [`SHOW [FULL] PROCESSSLIST`](dev/reference/sql/statements/show-processlist.md)
- [`SHOW SCHEMAS`](dev/reference/sql/statements/show-schemas.md)
- [`SHOW [FULL] TABLES`](dev/reference/sql/statements/show-tables.md)
- [`SHOW TABLE STATUS`](dev/reference/sql/statements/show-table-status.md)
- [`SHOW [GLOBAL|SESSION] VARIABLES`](dev/reference/sql/statements/show-variables.md)
- [`SHOW WARNINGS`](dev/reference/sql/statements/show-warnings.md)
- [`START TRANSACTION`](dev/reference/sql/statements/start-transaction.md)
- [`TRACE`](dev/reference/sql/statements/trace.md)
- [`TRUNCATE`](dev/reference/sql/statements/truncate.md)
- [`UPDATE`](dev/reference/sql/statements/update.md)
- [`USE`](dev/reference/sql/statements/use.md)
+ Data Types
- [Numeric Types](sql/datatype.md#numeric-types)
- [Date and Time Types](sql/datatype.md#date-and-time-types)
Expand Down
82 changes: 82 additions & 0 deletions dev/reference/sql/statements/add-column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: ADD COLUMN | TiDB SQL Statement Reference
summary: An overview of the usage of ADD COLUMN for the TiDB database.
category: reference
---

# ADD COLUMN

The `ALTER TABLE.. ADD COLUMN` statement adds a column to an existing table. This operation is online in TiDB, which means that neither reads or writes to the table are blocked by adding a column.

## Synopsis

**AlterTableStmt:**

![AlterTableStmt](/media/sqlgram/AlterTableStmt.png)

**AlterTableSpec:**

![AlterTableSpec](/media/sqlgram/AlterTableSpec.png)

**ColumnKeywordOpt:**

![ColumnKeywordOpt](/media/sqlgram/ColumnKeywordOpt.png)

**ColumnDef:**

![ColumnDef](/media/sqlgram/ColumnDef.png)

**ColumnPosition:**

![ColumnPosition](/media/sqlgram/ColumnPosition.png)

## Examples

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

mysql> INSERT INTO t1 VALUES (NULL);
Query OK, 1 row affected (0.02 sec)

mysql> SELECT * FROM t1;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)

mysql> ALTER TABLE t1 ADD COLUMN c1 INT NOT NULL;
Query OK, 0 rows affected (0.28 sec)

mysql> SELECT * FROM t1;
+----+----+
| id | c1 |
+----+----+
| 1 | 0 |
+----+----+
1 row in set (0.00 sec)

mysql> ALTER TABLE t1 ADD c2 INT NOT NULL AFTER c1;
Query OK, 0 rows affected (0.28 sec)

mysql> SELECT * FROM t1;
+----+----+----+
| id | c1 | c2 |
+----+----+----+
| 1 | 0 | 0 |
+----+----+----+
1 row in set (0.00 sec)
```

## MySQL compatibility

* Adding multiple columns at the same time is currently not supported.
* Adding a new column and setting it to the `PRIMARY KEY` is not supported.
* Adding a new column and setting it to `AUTO_INCREMENT` is not supported.

## See also

* [ADD INDEX](add-index.md)
morgo marked this conversation as resolved.
Show resolved Hide resolved
* [CREATE TABLE](create-table.md)
80 changes: 80 additions & 0 deletions dev/reference/sql/statements/add-index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: ADD INDEX | TiDB SQL Statement Reference
summary: An overview of the usage of ADD INDEX for the TiDB database.
category: reference
---

# ADD INDEX

The `ALTER TABLE.. ADD INDEX` statement adds an index to an existing table. This operation is online in TiDB, which means that neither reads or writes to the table are blocked by adding an index.

## Synopsis

**AlterTableStmt:**

![AlterTableStmt](/media/sqlgram/AlterTableStmt.png)

**AlterTableSpec:**

![AlterTableSpec](/media/sqlgram/AlterTableSpec.png)

**ColumnKeywordOpt:**

![ColumnKeywordOpt](/media/sqlgram/ColumnKeywordOpt.png)

**ColumnDef:**

![ColumnDef](/media/sqlgram/ColumnDef.png)

**ColumnPosition:**

![ColumnPosition](/media/sqlgram/ColumnPosition.png)

## Examples

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

mysql> INSERT INTO t1 (c1) VALUES (1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> EXPLAIN SELECT * FROM t1 WHERE c1 = 3;
+---------------------+----------+------+-------------------------------------------------------------+
| id | count | task | operator info |
+---------------------+----------+------+-------------------------------------------------------------+
| TableReader_7 | 10.00 | root | data:Selection_6 |
| └─Selection_6 | 10.00 | cop | eq(test.t1.c1, 3) |
| └─TableScan_5 | 10000.00 | cop | table:t1, range:[-inf,+inf], keep order:false, stats:pseudo |
+---------------------+----------+------+-------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql> ALTER TABLE t1 ADD INDEX (c1);
Query OK, 0 rows affected (0.30 sec)

mysql> EXPLAIN SELECT * FROM t1 WHERE c1 = 3;
+-------------------+-------+------+-----------------------------------------------------------------+
| id | count | task | operator info |
+-------------------+-------+------+-----------------------------------------------------------------+
| IndexReader_6 | 10.00 | root | index:IndexScan_5 |
| └─IndexScan_5 | 10.00 | cop | table:t1, index:c1, range:[3,3], keep order:false, stats:pseudo |
+-------------------+-------+------+-----------------------------------------------------------------+
2 rows in set (0.00 sec)
```

## MySQL compatibility

* `FULLTEXT`, `HASH` and `SPATIAL` indexes are not supported.
* Descending indexes are not supported (similar to MySQL 5.7).
* Adding multiple indexes at the same time is currently not supported.
* It is not possible to add a `PRIMARY KEY` to a table.

## See also

* [CREATE INDEX](create-index.md)
* [DROP INDEX](drop-index.md)
* [RENAME INDEX](rename-index.md)
* [ADD COLUMN](add-column.md)
* [CREATE TABLE](create-table.md)
* [EXPLAIN](explain.md)
76 changes: 76 additions & 0 deletions dev/reference/sql/statements/alter-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
title: ALTER TABLE | TiDB SQL Statement Reference
summary: An overview of the usage of ALTER TABLE for the TiDB database.
category: reference
---

# ALTER TABLE

This statement modifies an existing table to conform to a new table structure. The statement `ALTER TABLE` can be used to:

* [`ADD`](add-index.md), [`DROP`](drop-index.md), or [`RENAME`](rename-index.md) indexes
* [`ADD`](add-column.md), [`DROP`](drop-column.md), [`MODIFY`](modify-column.md) or [`CHANGE`](change-column.md) columns

## Synopsis

**AlterTableStmt:**

![AlterTableStmt](/media/sqlgram/AlterTableStmt.png)

**TableName:**

![TableName](/media/sqlgram/TableName.png)

**AlterTableSpec:**

![AlterTableSpec](/media/sqlgram/AlterTableSpec.png)

## Examples

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

mysql> INSERT INTO t1 (c1) VALUES (1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> EXPLAIN SELECT * FROM t1 WHERE c1 = 3;
+---------------------+----------+------+-------------------------------------------------------------+
| id | count | task | operator info |
+---------------------+----------+------+-------------------------------------------------------------+
| TableReader_7 | 10.00 | root | data:Selection_6 |
| └─Selection_6 | 10.00 | cop | eq(test.t1.c1, 3) |
| └─TableScan_5 | 10000.00 | cop | table:t1, range:[-inf,+inf], keep order:false, stats:pseudo |
+---------------------+----------+------+-------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql> ALTER TABLE t1 ADD INDEX (c1);
Query OK, 0 rows affected (0.30 sec)

mysql> EXPLAIN SELECT * FROM t1 WHERE c1 = 3;
+-------------------+-------+------+-----------------------------------------------------------------+
| id | count | task | operator info |
+-------------------+-------+------+-----------------------------------------------------------------+
| IndexReader_6 | 10.00 | root | index:IndexScan_5 |
| └─IndexScan_5 | 10.00 | cop | table:t1, index:c1, range:[3,3], keep order:false, stats:pseudo |
+-------------------+-------+------+-----------------------------------------------------------------+
2 rows in set (0.00 sec)
```

## MySQL compatibility

* All of the data types except spatial types are supported.
* `FULLTEXT`, `HASH` and `SPATIAL` indexes are not supported.

## See also

* [ADD COLUMN](add-column.md)
* [DROP COLUMN](drop-column.md)
* [RENAME COLUMN](rename-column.md)
* [ADD INDEX](add-index.md)
* [DROP INDEX](drop-index.md)
* [RENAME INDEX](rename-index.md)
* [CREATE TABLE](create-table.md)
* [DROP TABLE](drop-table.md)
* [SHOW CREATE TABLE](show-create-table.md)
60 changes: 60 additions & 0 deletions dev/reference/sql/statements/alter-user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: ALTER USER | TiDB SQL Statement Reference
summary: An overview of the usage of ALTER USER for the TiDB database.
category: reference
---

# ALTER USER

This statement changes an existing user inside the TiDB privilege system. In the MySQL privilege system, a user is the combination of a username and the host from which they are connecting from. Thus, it is possible to create a user `'newuser2'@'192.168.1.1'` who is only able to connect from the IP address `192.168.1.1`. It is also possible to have two users have the same user-portion, and different permissions as they login from different hosts.

## Synopsis

**AlterUserStmt:**

![AlterUserStmt](/media/sqlgram/AlterUserStmt.png)

**UserSpecList:**

![UserSpecList](/media/sqlgram/UserSpecList.png)

**UserSpec:**

![UserSpec](/media/sqlgram/UserSpec.png)

## Examples

```sql
mysql> CREATE USER 'newuser' IDENTIFIED BY 'newuserpassword';
Query OK, 1 row affected (0.01 sec)

mysql> SHOW CREATE USER 'newuser';
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE USER for newuser@% |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE USER 'newuser'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*5806E04BBEE79E1899964C6A04D68BCA69B1A879' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> ALTER USER 'newuser' IDENTIFIED BY 'newnewpassword';
Query OK, 0 rows affected (0.02 sec)

mysql> SHOW CREATE USER 'newuser';
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE USER for newuser@% |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE USER 'newuser'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*FB8A1EA1353E8775CA836233E367FBDFCB37BE73' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

## MySQL compatibility

* In MySQL this statement is used to change attributes such as to expire a password. This functionality is not yet supported by TiDB.

## See also

* [Security Compatibility with MySQL](/sql/security-compatibility.md)
* [CREATE USER](create-user.md)
* [DROP USER](drop-user.md)
* [SHOW CREATE USER](show-create-user.md)
Loading