diff --git a/_includes/v19.2/sql/functions.md b/_includes/v19.2/sql/functions.md
index ea21089edd6..4525ab1946f 100644
--- a/_includes/v19.2/sql/functions.md
+++ b/_includes/v19.2/sql/functions.md
@@ -373,6 +373,10 @@ has no relationship with the commit order of concurrent transactions.
The value is based on a timestamp picked when the transaction starts
and which stays constant throughout the transaction. This timestamp
@@ -662,6 +666,8 @@ has no relationship with the commit order of concurrent transactions.
Function → Returns
Description
+
aclexplode(aclitems: string[]) → tuple{oid AS grantor, oid AS grantee, string AS privilege_type, bool AS is_grantable}
Produces a virtual table containing aclitem stuff (returns no rows as this feature is unsupported in CockroachDB)
+
crdb_internal.unary_table() → tuple
Produces a virtual table containing a single row with no values.
This function is used only by CockroachDB’s developers for testing purposes.
@@ -783,6 +789,8 @@ has no relationship with the commit order of concurrent transactions.
pg_sleep makes the current session’s process sleep until seconds seconds have elapsed. seconds is a value of type double precision, so fractional-second delays can be specified.
Calculates the number of the current row within its partition, counting from 1.
+
diff --git a/v19.2/create-table-as.md b/v19.2/create-table-as.md
index 9d86a2dbe66..4ec123553c9 100644
--- a/v19.2/create-table-as.md
+++ b/v19.2/create-table-as.md
@@ -32,8 +32,40 @@ mentions.
The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the parent database.
## Synopsis
+
+
+
+
- {% include {{ page.version.version }}/sql/diagrams/create_table_as.html %}
+
+{% include {{ page.version.version }}/sql/diagrams/create_table_as.html %}
+
+
+
+
+
+ {% include {{ page.version.version }}/sql/diagrams/create_table_as.html %}
+
+
+**create_as_col_qual_list ::=**
+
+
+ {% include {{ page.version.version }}/sql/diagrams/create_as_col_qual_list.html %}
+
+
+**family_def ::=**
+
+
+ {% include {{ page.version.version }}/sql/diagrams/family_def.html %}
+
+
+**create_as_constraint_def ::=**
+
+
+ {% include {{ page.version.version }}/sql/diagrams/create_as_constraint_def.html %}
+
+
+
## Parameters
@@ -47,7 +79,10 @@ table td:first-child {
-----------|-------------
`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.
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`.
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.
- `name` | The name of the column you want to use instead of the name of the column from `select_stmt`.
+ `column_name` | The name of the column you want to use instead of the name of the column from `select_stmt`.
+ `create_as_col_qual_list` | An optional column definition, which may include [primary key constraints](primary-key.html) and [column family assignments](column-families.html).
+ `family_def` | An optional [column family definition](column-families.html). Column family names must be unique within the table but can have the same name as columns, constraints, or indexes.
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).
+ `create_as_constraint_def` | An optional [primary key constraint](primary-key.html).
`select_stmt` | A [selection query](selection-queries.html) to provide the data.
## Limitations
@@ -66,7 +101,7 @@ For example:
> CREATE TABLE logoff (
user_id INT PRIMARY KEY,
user_email STRING UNIQUE,
- logoff_date DATE NOT NULL,
+ logoff_date DATE NOT NULL
);
~~~
@@ -80,16 +115,14 @@ For example:
> SHOW CREATE logoff_copy;
~~~
~~~
-+-------------+-----------------------------------------------------------------+
-| Table | CreateTable |
-+-------------+-----------------------------------------------------------------+
-| logoff_copy | CREATE TABLE logoff_copy ( |
-| | user_id INT NULL, |
-| | user_email STRING NULL, |
-| | logoff_date DATE NULL, |
-| | FAMILY "primary" (user_id, user_email, logoff_date, rowid) |
-| | ) |
-+-------------+-----------------------------------------------------------------+
+ table_name | create_statement
++-------------+----------------------------------------------------------------+
+ logoff_copy | CREATE TABLE logoff_copy (
+ | user_id INT8 NULL,
+ | user_email STRING NULL,
+ | logoff_date DATE NULL,
+ | FAMILY "primary" (user_id, user_email, logoff_date, rowid)
+ | )
(1 row)
~~~
@@ -112,17 +145,15 @@ For example:
> SHOW CREATE logoff_copy;
~~~
~~~
-+-------------+-----------------------------------------------------------------+
-| Table | CreateTable |
-+-------------+-----------------------------------------------------------------+
-| logoff_copy | CREATE TABLE logoff_copy ( |
-| | user_id INT NULL, |
-| | user_email STRING NULL, |
-| | logoff_date DATE NULL, |
-| | INDEX logoff_copy_id_idx (user_id ASC), |
-| | FAMILY "primary" (user_id, user_email, logoff_date, rowid) |
-| | ) |
-+-------------+-----------------------------------------------------------------+
+ table_name | create_statement
++-------------+----------------------------------------------------------------+
+ logoff_copy | CREATE TABLE logoff_copy (
+ | user_id INT8 NULL,
+ | user_email STRING NULL,
+ | logoff_date DATE NULL,
+ | INDEX logoff_copy_id_idx (user_id ASC),
+ | FAMILY "primary" (user_id, user_email, logoff_date, rowid)
+ | )
(1 row)
~~~
@@ -133,37 +164,43 @@ results.
## Examples
+{% include {{page.version.version}}/sql/movr-statements.md %}
+
### Create a table from a `SELECT` query
{% include copy-clipboard.html %}
~~~ sql
-> SELECT * FROM customers WHERE state = 'NY';
+> SELECT * FROM users WHERE city = 'new york';
~~~
~~~
-+----+---------+-------+
-| id | name | state |
-+----+---------+-------+
-| 6 | Dorotea | NY |
-| 15 | Thales | NY |
-+----+---------+-------+
+ id | city | name | address | credit_card
++--------------------------------------+----------+------------------+-----------------------------+-------------+
+ 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228
+ 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892
+ 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636
+ 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234
+ 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487
+(5 rows)
~~~
{% include copy-clipboard.html %}
~~~ sql
-> CREATE TABLE customers_ny AS SELECT * FROM customers WHERE state = 'NY';
+> CREATE TABLE users_ny AS SELECT * FROM users WHERE city = 'new york';
~~~
{% include copy-clipboard.html %}
~~~ sql
-> SELECT * FROM customers_ny;
+> SELECT * FROM users_ny;
~~~
~~~
-+----+---------+-------+
-| id | name | state |
-+----+---------+-------+
-| 6 | Dorotea | NY |
-| 15 | Thales | NY |
-+----+---------+-------+
+ id | city | name | address | credit_card
++--------------------------------------+----------+------------------+-----------------------------+-------------+
+ 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228
+ 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892
+ 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636
+ 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234
+ 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487
+(5 rows)
~~~
### Change column names
@@ -172,41 +209,41 @@ This statement creates a copy of an existing table but with changed column names
{% include copy-clipboard.html %}
~~~ sql
-> CREATE TABLE customers_ny (id, first_name) AS SELECT id, name FROM customers WHERE state = 'NY';
+> CREATE TABLE users_ny_names (id, name) AS SELECT id, name FROM users WHERE city = 'new york';
~~~
{% include copy-clipboard.html %}
~~~ sql
-> SELECT * FROM customers_ny;
+> SELECT * FROM users_ny_names;
~~~
~~~
-+----+------------+
-| id | first_name |
-+----+------------+
-| 6 | Dorotea |
-| 15 | Thales |
-+----+------------+
+ id | name
++--------------------------------------+------------------+
+ 00000000-0000-4000-8000-000000000000 | Robert Murphy
+ 051eb851-eb85-4ec0-8000-000000000001 | James Hamilton
+ 0a3d70a3-d70a-4d80-8000-000000000002 | Judy White
+ 0f5c28f5-c28f-4c00-8000-000000000003 | Devin Jordan
+ 147ae147-ae14-4b00-8000-000000000004 | Catherine Nelson
+(5 rows)
~~~
### Create a table from a `VALUES` clause
{% include copy-clipboard.html %}
~~~ sql
-> CREATE TABLE tech_states AS VALUES ('CA'), ('NY'), ('WA');
+> CREATE TABLE drivers (id, city, name) AS VALUES (gen_random_uuid(), 'new york', 'Harry Potter'), (gen_random_uuid(), 'chicago', 'Terry Oldman'), (gen_random_uuid(), 'Annika', 'John');
~~~
{% include copy-clipboard.html %}
~~~ sql
-> SELECT * FROM tech_states;
+> SELECT * FROM drivers;
~~~
~~~
-+---------+
-| column1 |
-+---------+
-| CA |
-| NY |
-| WA |
-+---------+
+ id | city | name
++--------------------------------------+----------+--------------+
+ fd7d4529-52b9-481f-8c2c-11e83ee3671f | new york | Harry Potter
+ 1b0bd35a-6741-4994-a8e2-82121efcfb27 | chicago | Terry Oldman
+ 48341b10-83ef-4b98-b40b-8f5de738201a | seattle | Annika
(3 rows)
~~~
@@ -215,26 +252,110 @@ This statement creates a copy of an existing table but with changed column names
{% include copy-clipboard.html %}
~~~ sql
-> CREATE TABLE customers_ny_copy AS TABLE customers_ny;
+> CREATE TABLE users_ny_copy AS TABLE users_ny;
~~~
{% include copy-clipboard.html %}
~~~ sql
-> SELECT * FROM customers_ny_copy;
+> SELECT * FROM users_ny_copy;
~~~
~~~
-+----+------------+
-| id | first_name |
-+----+------------+
-| 6 | Dorotea |
-| 15 | Thales |
-+----+------------+
+ id | city | name | address | credit_card
++--------------------------------------+----------+------------------+-----------------------------+-------------+
+ 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228
+ 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892
+ 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636
+ 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234
+ 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487
+(5 rows)
~~~
When a table copy is created this way, the copy is not associated to
any primary key, secondary index or constraint that was present on the
original table.
+### Change primary keys
+
+{% include copy-clipboard.html %}
+~~~ sql
+> CREATE TABLE users_ny_extra (id, city, name PRIMARY KEY) AS SELECT id, city, name FROM users WHERE city = 'new york';
+~~~
+
+{% include copy-clipboard.html %}
+~~~ sql
+> SELECT * FROM users_ny_extra;
+~~~
+~~~
+ id | city | name
++--------------------------------------+----------+------------------+
+ 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson
+ 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan
+ 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton
+ 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White
+ 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy
+(5 rows)
+~~~
+
+{% include copy-clipboard.html %}
+~~~ sql
+> SHOW CREATE TABLE users_ny_extra;
+~~~
+~~~
+ table_name | create_statement
++----------------+--------------------------------------------------+
+ users_ny_extra | CREATE TABLE users_ny_extra (
+ | id UUID NULL,
+ | city VARCHAR NULL,
+ | name VARCHAR NOT NULL,
+ | CONSTRAINT "primary" PRIMARY KEY (name ASC),
+ | FAMILY "primary" (id, city, name)
+ | )
+(1 row)
+~~~
+
+### Define column families
+
+{% include copy-clipboard.html %}
+~~~ sql
+> CREATE TABLE users_ny_alt (id PRIMARY KEY FAMILY ids, name, city FAMILY locs, address, credit_card FAMILY payments) AS SELECT id, name, city, address, credit_card FROM users WHERE city = 'new york';
+~~~
+
+{% include copy-clipboard.html %}
+~~~ sql
+> SELECT * FROM users_ny_alt;
+~~~
+~~~
+ id | name | city | address | credit_card
++--------------------------------------+------------------+----------+-----------------------------+-------------+
+ 00000000-0000-4000-8000-000000000000 | Robert Murphy | new york | 99176 Anderson Mills | 8885705228
+ 051eb851-eb85-4ec0-8000-000000000001 | James Hamilton | new york | 73488 Sydney Ports Suite 57 | 8340905892
+ 0a3d70a3-d70a-4d80-8000-000000000002 | Judy White | new york | 18580 Rosario Ville Apt. 61 | 2597958636
+ 0f5c28f5-c28f-4c00-8000-000000000003 | Devin Jordan | new york | 81127 Angela Ferry Apt. 8 | 5614075234
+ 147ae147-ae14-4b00-8000-000000000004 | Catherine Nelson | new york | 1149 Lee Alley | 0792553487
+(5 rows)
+~~~
+
+{% include copy-clipboard.html %}
+~~~ sql
+> SHOW CREATE TABLE users_ny_alt;
+~~~
+~~~
+ table_name | create_statement
++--------------+------------------------------------------------+
+ users_ny_alt | CREATE TABLE users_ny_alt (
+ | id UUID NOT NULL,
+ | name VARCHAR NULL,
+ | city VARCHAR NULL,
+ | address VARCHAR NULL,
+ | credit_card VARCHAR NULL,
+ | CONSTRAINT "primary" PRIMARY KEY (id ASC),
+ | FAMILY ids (id, name, address),
+ | FAMILY locs (city),
+ | FAMILY payments (credit_card)
+ | )
+(1 row)
+~~~
+
## See also
- [Selection Queries](selection-queries.html)
diff --git a/v19.2/create-table.md b/v19.2/create-table.md
index 7d2dc8553ba..1662cca5b5a 100644
--- a/v19.2/create-table.md
+++ b/v19.2/create-table.md
@@ -421,7 +421,7 @@ In this example, we create a table and [define partitions by list](partitioning.
{% include copy-clipboard.html %}
~~~ sql
-> CREATE TABLE TABLE rides (
+> CREATE TABLE rides (
id UUID NOT NULL,
city STRING NOT NULL,
vehicle_city STRING,