-
Notifications
You must be signed in to change notification settings - Fork 466
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
ON UPDATE expressions #12035
ON UPDATE expressions #12035
Changes from 1 commit
9e16a68
d85b583
a8ce763
3a9c18b
f564e2b
4da219e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,7 +4,7 @@ summary: Use the ADD COLUMN statement to add columns to tables. | |||||
toc: true | ||||||
--- | ||||||
|
||||||
The `ADD COLUMN` [statement](sql-statements.html) is part of `ALTER TABLE` and adds columns to tables. | ||||||
`ADD COLUMN` is a subcommand of [`ALTER TABLE`](alter-table.html). Use `ADD COLUMN` to add columns to existing tables. | ||||||
|
||||||
{% include {{ page.version.version }}/sql/combine-alter-table-commands.md %} | ||||||
|
||||||
|
@@ -26,7 +26,27 @@ The user must have the `CREATE` [privilege](authorization.html#assign-privileges | |||||
`table_name` | The name of the table to which you want to add the column. | ||||||
`column_name` | The name of the column you want to add. The column name must follow these [identifier rules](keywords-and-identifiers.html#identifiers) and must be unique within the table but can have the same name as indexes or constraints. | ||||||
`typename` | The [data type](data-types.html) of the new column. | ||||||
`col_qualification` | An optional list of column definitions, which may include [column-level constraints](constraints.html), [collation](collate.html), or [column family assignments](column-families.html).<br><br>If the column family is not specified, the column will be added to the first column family. For more information about how column families are assigned, see [Column Families](column-families.html#assign-column-families-when-adding-columns). | ||||||
`col_qualification` | An optional list of [column qualifications](#column-qualifications). | ||||||
|
||||||
## Column qualifications | ||||||
|
||||||
CockroachDB supports the following column qualifications: | ||||||
|
||||||
- [Column-level constraints](constraints.html) | ||||||
- [Collations](collate.html) | ||||||
- [Column family assignments](column-families.html) | ||||||
- <span class="version-tag">New in v21.2</span>: [`ON UPDATE` expressions](#on-update-expressions) | ||||||
|
||||||
### ON UPDATE expressions | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
|
||||||
<span class="version-tag">New in v21.2</span>: `ON UPDATE` expressions set a row value for a particular column when any other value in the row is updated. | ||||||
|
||||||
Note the following limitations of `ON UPDATE` expressions: | ||||||
|
||||||
- You cannot add a [foreign key constraint](foreign-key.html) and an `ON UPDATE` expression to the same column. | ||||||
- Values populated by [`DEFAULT` constraints](default-value.html) do not trigger an `ON UPDATE` change. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Updated this line |
||||||
|
||||||
For an example of `ON UPDATE`, see [Add a column with an `ON UPDATE` expression](#add-a-column-with-an-on-update-expression). | ||||||
|
||||||
## Viewing schema changes | ||||||
|
||||||
|
@@ -225,7 +245,7 @@ $ cockroach demo bank | |||||
|
||||||
{% include copy-clipboard.html %} | ||||||
~~~ sql | ||||||
> SHOW CREATE TABLE FROM bank; | ||||||
> SHOW CREATE TABLE bank; | ||||||
~~~ | ||||||
~~~ | ||||||
table_name | create_statement | ||||||
|
@@ -260,7 +280,7 @@ $ cockroach demo bank | |||||
|
||||||
{% include copy-clipboard.html %} | ||||||
~~~ sql | ||||||
> SHOW CREATE TABLE FROM bank; | ||||||
> SHOW CREATE TABLE bank; | ||||||
~~~ | ||||||
~~~ | ||||||
table_name | create_statement | ||||||
|
@@ -296,7 +316,7 @@ $ cockroach demo bank | |||||
|
||||||
{% include copy-clipboard.html %} | ||||||
~~~ sql | ||||||
> SHOW CREATE TABLE FROM bank; | ||||||
> SHOW CREATE TABLE bank; | ||||||
~~~ | ||||||
~~~ | ||||||
table_name | create_statement | ||||||
|
@@ -325,6 +345,56 @@ $ cockroach demo bank | |||||
(1 row) | ||||||
~~~ | ||||||
|
||||||
### Add a column with an `ON UPDATE` expression | ||||||
|
||||||
<span class="version-tag">New in v21.2</span>: `ON UPDATE` expressions set the value for a column when other values in a row are updated. | ||||||
|
||||||
For example, suppose you add a new column to the `bank` table: | ||||||
|
||||||
{% include copy-clipboard.html %} | ||||||
~~~ sql | ||||||
> ALTER TABLE bank ADD COLUMN last_updated TIMESTAMPTZ DEFAULT now() ON UPDATE now(); | ||||||
~~~ | ||||||
|
||||||
{% include copy-clipboard.html %} | ||||||
~~~ sql | ||||||
> SELECT id, balance, last_updated FROM bank LIMIT 5; | ||||||
~~~ | ||||||
|
||||||
~~~ | ||||||
id | balance | last_updated | ||||||
-----+---------+-------------------------------- | ||||||
0 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
1 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
2 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
3 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
4 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
(5 rows) | ||||||
~~~ | ||||||
|
||||||
When any value in any row of the `bank` table is updated, CockroachDB re-evaluates the `ON UPDATE` expression and updates the `last_updated` column with the result. | ||||||
|
||||||
{% include copy-clipboard.html %} | ||||||
~~~ sql | ||||||
> UPDATE bank SET balance = 500 WHERE id = 0; | ||||||
~~~ | ||||||
|
||||||
{% include copy-clipboard.html %} | ||||||
~~~ sql | ||||||
> SELECT id, balance, last_updated FROM bank LIMIT 5; | ||||||
~~~ | ||||||
|
||||||
~~~ | ||||||
id | balance | last_updated | ||||||
-----+---------+-------------------------------- | ||||||
0 | 500 | 2021-10-21 17:06:42.211261+00 | ||||||
1 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
2 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
3 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
4 | 0 | 2021-10-21 17:03:41.213557+00 | ||||||
(5 rows) | ||||||
~~~ | ||||||
|
||||||
## See also | ||||||
|
||||||
- [`ALTER TABLE`](alter-table.html) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similar to my other comment, i don't thing
DEFAULT
should be included under constraints. instead,DEFAULT
should be documented as basically identically toON UPDATE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
(but see my comment below)