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

ON UPDATE expressions #12035

Merged
merged 6 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions v20.2/add-column.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ $ cockroach demo bank

{% include copy-clipboard.html %}
~~~ sql
> SHOW CREATE TABLE FROM bank;
> SHOW CREATE TABLE bank;
~~~
~~~
table_name | create_statement
Expand Down Expand Up @@ -259,7 +259,7 @@ $ cockroach demo bank

{% include copy-clipboard.html %}
~~~ sql
> SHOW CREATE TABLE FROM bank;
> SHOW CREATE TABLE bank;
~~~
~~~
table_name | create_statement
Expand Down Expand Up @@ -295,7 +295,7 @@ $ cockroach demo bank

{% include copy-clipboard.html %}
~~~ sql
> SHOW CREATE TABLE FROM bank;
> SHOW CREATE TABLE bank;
~~~
~~~
table_name | create_statement
Expand Down
6 changes: 3 additions & 3 deletions v21.1/add-column.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ $ cockroach demo bank

{% include copy-clipboard.html %}
~~~ sql
> SHOW CREATE TABLE FROM bank;
> SHOW CREATE TABLE bank;
~~~
~~~
table_name | create_statement
Expand Down Expand Up @@ -259,7 +259,7 @@ $ cockroach demo bank

{% include copy-clipboard.html %}
~~~ sql
> SHOW CREATE TABLE FROM bank;
> SHOW CREATE TABLE bank;
~~~
~~~
table_name | create_statement
Expand Down Expand Up @@ -295,7 +295,7 @@ $ cockroach demo bank

{% include copy-clipboard.html %}
~~~ sql
> SHOW CREATE TABLE FROM bank;
> SHOW CREATE TABLE bank;
~~~
~~~
table_name | create_statement
Expand Down
80 changes: 75 additions & 5 deletions v21.2/add-column.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand All @@ -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)
Copy link
Contributor

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 to ON UPDATE

Copy link
Contributor Author

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)


### ON UPDATE expressions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### ON UPDATE expressions
### `ON UPDATE` expressions

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Values populated by DEFAULT constraints do not trigger an ON UPDATE change.

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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion v21.2/alter-column.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ summary: Use the ALTER COLUMN statement to set, change, or drop a column's DEFAU
toc: true
---

The `ALTER COLUMN` [statement](sql-statements.html) is part of `ALTER TABLE` and can be used to:
`ALTER COLUMN` is a subcommand of [`ALTER TABLE`](alter-table.html). You can use `ALTER COLUMN` to do the following:

- Set, change, or drop a column's [`DEFAULT` constraint](default-value.html).
- Set or drop a column's [`NOT NULL` constraint](not-null.html).
- <span class="version-tag">New in v21.2</span>: Set, change, or drop an [`ON UPDATE` expression](add-column.html#on-update-expressions).
- Change a column's [data type](data-types.html).

{{site.data.alerts.callout_info}}
Expand Down
2 changes: 1 addition & 1 deletion v21.2/create-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Parameter | Description
`opt_persistence_temp_table` | Defines the table as a session-scoped temporary table. For more information, see [Temporary Tables](temporary-tables.html).<br><br>Note that the `LOCAL`, `GLOBAL`, and `UNLOGGED` options are no-ops, allowed by the parser for PostgresSQL compatibility.<br><br>**Support for temporary tables is [experimental](experimental-features.html#temporary-objects)**.
`IF NOT EXISTS` | Create a new table only if a table of the same name does not already exist in the database; if one does exist, do not return an error.<br><br>Note that `IF NOT EXISTS` checks the table name only; it does not check if an existing table has the same columns, indexes, constraints, etc., of the new table.
`table_name` | The name of the table to create, which must be unique within its database and follow these [identifier rules](keywords-and-identifiers.html#identifiers). When the parent database is not set as the default, the name must be formatted as `database.name`.<br><br>The [`UPSERT`](upsert.html) and [`INSERT ON CONFLICT`](insert.html) statements use a temporary table called `excluded` to handle uniqueness conflicts during execution. It's therefore not recommended to use the name `excluded` for any of your tables.
`column_def` | A comma-separated list of column definitions. Each column requires a [name/identifier](keywords-and-identifiers.html#identifiers) and [data type](data-types.html); optionally, a [column-level constraint](constraints.html) or other column qualification (e.g., [computed columns](computed-columns.html)) can be specified. Column names must be unique within the table but can have the same name as indexes or constraints.<br><br>Any `PRIMARY KEY`, `UNIQUE`, and `CHECK` [constraints](constraints.html) defined at the column level are moved to the table-level as part of the table's creation. Use the [`SHOW CREATE`](show-create.html) statement to view them at the table level.
`column_def` | A comma-separated list of column definitions. Each column requires a [name/identifier](keywords-and-identifiers.html#identifiers) and [data type](data-types.html). Column names must be unique within the table but can have the same name as indexes or constraints.<br><br>You can optionally specify a [column qualification](add-column.html#column-constraints) (e.g., a [column-level constraint](constraints.html)). Any `PRIMARY KEY`, `UNIQUE`, and `CHECK` [constraints](constraints.html) defined at the column level are moved to the table-level as part of the table's creation. Use the [`SHOW CREATE`](show-create.html) statement to view them at the table level.
`index_def` | An optional, comma-separated list of [index definitions](indexes.html). For each index, the column(s) to index must be specified; optionally, a name can be specified. Index names must be unique within the table and follow these [identifier rules](keywords-and-identifiers.html#identifiers). See the [Create a Table with Secondary Indexes and Inverted Indexes](#create-a-table-with-secondary-and-inverted-indexes) example below.<br><br> To enable [hash-sharded indexes](hash-sharded-indexes.html), set the `experimental_enable_hash_sharded_indexes` [session variable](set-vars.html) to `on`. For examples, see [Create a table with hash-sharded indexes](#create-a-table-with-a-hash-sharded-primary-index) below.<br><br>The [`CREATE INDEX`](create-index.html) statement can be used to create an index separate from table creation.
`family_def` | An optional, comma-separated list of [column family definitions](column-families.html). Column family names must be unique within the table but can have the same name as columns, constraints, or indexes.<br><br>A column family is a group of columns that are stored as a single key-value pair in the underlying key-value store. CockroachDB automatically groups columns into families to ensure efficient storage and performance. However, there are cases when you may want to manually assign columns to families. For more details, see [Column Families](column-families.html).
`table_constraint` | An optional, comma-separated list of [table-level constraints](constraints.html). Constraint names must be unique within the table but can have the same name as columns, column families, or indexes.
Expand Down