-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev/reference/sql: add sql statements (#1425)
* dev/reference/sql: add sql statements * dev/reference/sql: fix aliases in sql statements * Apply suggestions from code review Co-Authored-By: bb7133 <bb7133@gmail.com> * dev/reference/sql: improve wording * Apply suggestions from code review Co-Authored-By: Calvin Weng <wenghao@pingcap.com> * dev/reference/sql: improve wording based on Calvin's suggestions * dev/reference/sql: fix wording * dev/reference/sql: fix alias format * dev/reference/sql: address comments & fix format * dev/reference/sql: add SELECT syntax description * Apply suggestions from code review Co-Authored-By: Lilian Lee <lilin@pingcap.com> * dev/reference/sql: fix links * Update dev/reference/sql/statements/begin.md Co-Authored-By: Lilian Lee <lilin@pingcap.com> * Update dev/reference/sql/statements/kill.md Co-Authored-By: Lilian Lee <lilin@pingcap.com> * Update TOC
- Loading branch information
Showing
443 changed files
with
5,039 additions
and
1,087 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: ADD COLUMN | ||
summary: TiDB 数据库中 ADD COLUMN 的使用概况。 | ||
category: reference | ||
--- | ||
|
||
# ADD COLUMN | ||
|
||
`ALTER TABLE.. ADD COLUMN` 语句用于在已有表中添加列。在 TiDB 中,`ADD COLUMN` 为在线操作,不会阻塞表中的数据读写。 | ||
|
||
## 语法图 | ||
|
||
**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) | ||
|
||
## 示例 | ||
|
||
```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 兼容性 | ||
|
||
* 不支持同时添加多列。 | ||
* 不支持将新添加的列设为 `PRIMARY KEY`。 | ||
* 不支持将新添加的列设为 `AUTO_INCREMENT`。 | ||
|
||
## 另请参阅 | ||
|
||
* [ADD INDEX](/dev/reference/sql/statements/add-index.md) | ||
* [CREATE TABLE](/dev/reference/sql/statements/create-table.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: ADD INDEX | ||
summary: TiDB 数据库中 ADD INDEX 的使用概况。 | ||
category: reference | ||
--- | ||
|
||
# ADD INDEX | ||
|
||
`ALTER TABLE.. ADD INDEX` 语句用于在已有表中添加一个索引。在 TiDB 中,`ADD INDEX` 为在线操作,不会阻塞表中的数据读写。 | ||
|
||
## 语法图 | ||
|
||
**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) | ||
|
||
## 示例 | ||
|
||
```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 兼容性 | ||
|
||
* 不支持 `FULLTEXT`,`HASH` 和 `SPATIAL` 索引。 | ||
* 不支持降序索引(类似于 MySQL 5.7)。 | ||
* 目前尚不支持同时添加多个索引。 | ||
* 无法向表中添加 `PRIMARY KEY`。 | ||
|
||
## 另请参阅 | ||
|
||
* [CREATE INDEX](/dev/reference/sql/statements/create-index.md) | ||
* [DROP INDEX](/dev/reference/sql/statements/drop-index.md) | ||
* [RENAME INDEX](/dev/reference/sql/statements/rename-index.md) | ||
* [ADD COLUMN](/dev/reference/sql/statements/add-column.md) | ||
* [CREATE TABLE](/dev/reference/sql/statements/create-table.md) | ||
* [EXPLAIN](/dev/reference/sql/statements/explain.md) |
Oops, something went wrong.