Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 26 additions & 28 deletions _includes/sql/diagrams/rename_column.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<svg width="708" height="134">
<svg width="684" height="134">

<polygon points="9 17 1 13 1 21"></polygon>
<polygon points="17 17 9 13 9 21"></polygon>
Expand All @@ -14,33 +14,31 @@
<rect x="269" y="35" width="68" height="32" rx="10"></rect>
<rect x="267" y="33" width="68" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="277" y="53">EXISTS</text>
<a xlink:href="sql-grammar.html#relation_expr" xlink:title="relation_expr">
<rect x="377" y="3" width="104" height="32"></rect>
<rect x="375" y="1" width="104" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="385" y="21">relation_expr</text>
</a>
<rect x="501" y="3" width="74" height="32" rx="10"></rect>
<rect x="499" y="1" width="74" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="509" y="21">RENAME</text>
<a xlink:href="sql-grammar.html#opt_column" xlink:title="opt_column">
<rect x="595" y="3" width="92" height="32"></rect>
<rect x="593" y="1" width="92" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="603" y="21">opt_column</text>
</a>
<a xlink:href="sql-grammar.html#name" xlink:title="name">
<rect x="495" y="101" width="54" height="32"></rect>
<rect x="493" y="99" width="54" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="503" y="119">name</text>
</a>
<rect x="569" y="101" width="38" height="32" rx="10"></rect>
<rect x="567" y="99" width="38" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="577" y="119">TO</text>

<rect x="377" y="3" width="94" height="32"></rect>
<rect x="375" y="1" width="94" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="385" y="21">table_name</text>

<rect x="491" y="3" width="74" height="32" rx="10"></rect>
<rect x="489" y="1" width="74" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="499" y="21">RENAME</text>
<rect x="585" y="3" width="78" height="32" rx="10"></rect>
<rect x="583" y="1" width="78" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="593" y="21">COLUMN</text>

<rect x="417" y="101" width="108" height="32"></rect>
<rect x="415" y="99" width="108" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="425" y="119">current_name</text>

<rect x="545" y="101" width="38" height="32" rx="10"></rect>
<rect x="543" y="99" width="38" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="553" y="119">TO</text>
<a xlink:href="sql-grammar.html#name" xlink:title="name">
<rect x="627" y="101" width="54" height="32"></rect>
<rect x="625" y="99" width="54" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="635" y="119">name</text>
<rect x="603" y="101" width="54" height="32"></rect>
<rect x="601" y="99" width="54" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="611" y="119">name</text>
</a>
<path class="line" d="m17 17 h2 m0 0 h10 m62 0 h10 m0 0 h10 m62 0 h10 m20 0 h10 m0 0 h132 m-162 0 h20 m142 0 h20 m-182 0 q10 0 10 10 m162 0 q0 -10 10 -10 m-172 10 v12 m162 0 v-12 m-162 12 q0 10 10 10 m142 0 q10 0 10 -10 m-152 10 h10 m34 0 h10 m0 0 h10 m68 0 h10 m20 -32 h10 m104 0 h10 m0 0 h10 m74 0 h10 m0 0 h10 m92 0 h10 m2 0 l2 0 m2 0 l2 0 m2 0 l2 0 m-236 98 l2 0 m2 0 l2 0 m2 0 l2 0 m2 0 h10 m54 0 h10 m0 0 h10 m38 0 h10 m0 0 h10 m54 0 h10 m3 0 h-3"></path>
<polygon points="699 115 707 111 707 119"></polygon>
<polygon points="699 115 691 111 691 119"></polygon>
<path class="line" d="m17 17 h2 m0 0 h10 m62 0 h10 m0 0 h10 m62 0 h10 m20 0 h10 m0 0 h132 m-162 0 h20 m142 0 h20 m-182 0 q10 0 10 10 m162 0 q0 -10 10 -10 m-172 10 v12 m162 0 v-12 m-162 12 q0 10 10 10 m142 0 q10 0 10 -10 m-152 10 h10 m34 0 h10 m0 0 h10 m68 0 h10 m20 -32 h10 m94 0 h10 m0 0 h10 m74 0 h10 m0 0 h10 m78 0 h10 m2 0 l2 0 m2 0 l2 0 m2 0 l2 0 m-290 98 l2 0 m2 0 l2 0 m2 0 l2 0 m2 0 h10 m108 0 h10 m0 0 h10 m38 0 h10 m0 0 h10 m54 0 h10 m3 0 h-3"></path>
<polygon points="675 115 683 111 683 119"></polygon>
<polygon points="675 115 667 111 667 119"></polygon>
</svg>
2 changes: 1 addition & 1 deletion generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'")}},
Expand Down
41 changes: 40 additions & 1 deletion rename-column.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)