diff --git a/_includes/sql/diagrams/rename_column.html b/_includes/sql/diagrams/rename_column.html index a85b0d60771..56e8ae79973 100644 --- a/_includes/sql/diagrams/rename_column.html +++ b/_includes/sql/diagrams/rename_column.html @@ -1,4 +1,4 @@ - + @@ -14,33 +14,31 @@ EXISTS - - - - relation_expr - - - - RENAME - - - - opt_column - - - - - name - - - - TO + + + + table_name + + + + RENAME + + + COLUMN + + + + current_name + + + + TO - - - name + + + name - - - + + + \ No newline at end of file diff --git a/generate/main.go b/generate/main.go index 6754e19ae37..967c165669e 100644 --- a/generate/main.go +++ b/generate/main.go @@ -249,7 +249,7 @@ func main() { }, {name: "iso_level"}, {name: "release_savepoint", stmt: "release_stmt", inline: []string{"savepoint_name"}}, - {name: "rename_column", stmt: "rename_stmt", match: []*regexp.Regexp{regexp.MustCompile("'ALTER' 'TABLE' .* 'RENAME' opt_column")}}, + {name: "rename_column", stmt: "rename_stmt", inline: []string{"opt_column"}, match: []*regexp.Regexp{regexp.MustCompile("'ALTER' 'TABLE' .* 'RENAME' ('COLUMN'|name)")}, replace: map[string]string{"relation_expr": "table_name", "name 'TO'": "current_name 'TO'"}, unlink: []string{"table_name", "current_name"}}, {name: "rename_database", stmt: "rename_stmt", match: []*regexp.Regexp{regexp.MustCompile("'ALTER' 'DATABASE'")}}, {name: "rename_index", stmt: "rename_stmt", match: []*regexp.Regexp{regexp.MustCompile("'ALTER' 'INDEX'")}}, {name: "rename_table", stmt: "rename_stmt", match: []*regexp.Regexp{regexp.MustCompile("'ALTER' 'TABLE' .* 'RENAME' 'TO'")}}, diff --git a/rename-column.md b/rename-column.md index cedd56638b5..c9627c8fefc 100644 --- a/rename-column.md +++ b/rename-column.md @@ -20,4 +20,43 @@ The user must have the `CREATE` [privilege](privileges.html) on the table. | Parameter | Description | |-----------|-------------| -| | | +| `IF EXISTS` | Rename the column only if a column of `current_name` exists; if one does not exist, do not return an error. | +| `table_name` | The name of the table with the column you want to use. | +| `current_name` | The current name of the column. | +| `name` | The [`name`](sql-grammar.html#name) you want to use for the column, which must be unique to its table and follow these [identifier rules](keywords-and-identifiers.html#identifiers). | + +## Example + +### Rename a Column + +~~~ sql +> SELECT * FROM users; +~~~ +~~~ ++----+-------+-------+ +| id | name | title | ++----+-------+-------+ +| 1 | Tom | cat | +| 2 | Jerry | rat | ++----+-------+-------+ +~~~ +~~~ sql +> ALTER TABLE users RENAME COLUMN title TO species; +~~~ +~~~ sql +> SELECT * FROM users; +~~~ +~~~ ++----+-------+---------+ +| id | name | species | ++----+-------+---------+ +| 1 | Tom | cat | +| 2 | Jerry | rat | ++----+-------+---------+ +~~~ + +## See Also + +- [`RENAME DATABASE`](rename-database.html) +- [`RENAME TABLE`](rename-table.html) +- [`ALTER TABLE`](alter-table.html)