|
9 | 9 | parent: 'commands'
|
10 | 10 | ---
|
11 | 11 |
|
12 |
| -`CREATE TABLE` defines a table that is persisted in durable storage and can be |
13 |
| -written to, updated and seamlessly joined with other tables, views or sources. |
| 12 | +`CREATE TABLE` defines a table that is persisted in durable storage. In |
| 13 | +Materialize, you can create: |
14 | 14 |
|
15 |
| -Tables in Materialize are similar to tables in standard relational databases: |
16 |
| -they consist of rows and columns where the columns are fixed when the table is |
17 |
| -created but rows can be added to at will via [`INSERT`](../insert) statements. |
| 15 | +- User-populated tables. User-populated tables can be written to (i.e., |
| 16 | + [`INSERT`]/[`UPDATE`]/[`DELETE`]) by the user. |
18 | 17 |
|
19 |
| -{{< warning >}} |
20 |
| -At the moment, tables have many [known limitations](#known-limitations). In most |
21 |
| -situations, you should use [sources](/sql/create-source) instead. |
22 |
| -{{< /warning >}} |
| 18 | +- [Source-populated](/concepts/sources/) tables. Source-populated tables cannot |
| 19 | + be written to by the user; they are populated through data ingestion from a |
| 20 | + source. |
23 | 21 |
|
24 |
| -[//]: # "TODO(morsapaes) Bring back When to use a table? once there's more |
25 |
| -clarity around best practices." |
| 22 | +Tables can be joined with other tables, materialized views, and views. Tables in |
| 23 | +Materialize are similar to tables in standard relational databases: they consist |
| 24 | +of rows and columns where the columns are fixed when the table is created. |
26 | 25 |
|
27 | 26 | ## Syntax
|
28 | 27 |
|
29 |
| -{{< diagram "create-table.svg" >}} |
| 28 | +{{< tabs >}} |
30 | 29 |
|
31 |
| -### `col_option` |
| 30 | +{{< tab "User-populated tables" >}} |
32 | 31 |
|
33 |
| -{{< diagram "col-option.svg" >}} |
| 32 | +To create a table that users can write to (i.e., perform |
| 33 | +[`INSERT`](/sql/insert/)/[`UPDATE`](/sql/update/)/[`DELETE`](/sql/delete/) |
| 34 | +operations): |
34 | 35 |
|
35 |
| -### `with_options` |
| 36 | +```mzsql |
| 37 | +CREATE [TEMP|TEMPORARY] TABLE <table_name> ( |
| 38 | + <column_name> <column_type> [NOT NULL][DEFAULT <default_expr>] |
| 39 | + [, ...] |
| 40 | +) |
| 41 | +[WITH ( |
| 42 | + RETAIN HISTORY [=] FOR <duration> | PARTITION BY (<column_name> [, ...]) |
| 43 | + [, ...] |
| 44 | +)] |
| 45 | +; |
| 46 | +``` |
| 47 | + |
| 48 | +{{% yaml-table data="syntax_options/create_table_options_user_populated" %}} |
| 49 | + |
| 50 | +{{</ tab >}} |
| 51 | + |
| 52 | +{{< tab "Source-populated tables (DB source)" >}} |
36 | 53 |
|
37 |
| -{{< diagram "with-options-retain-history.svg" >}} |
| 54 | +To create a table from a [source](/sql/create-source/), where the source maps to |
| 55 | +an external database system: |
38 | 56 |
|
39 |
| -Field | Use |
40 |
| -------|----- |
41 |
| -**TEMP** / **TEMPORARY** | Mark the table as [temporary](#temporary-tables). |
42 |
| -_table_name_ | A name for the table. |
43 |
| -_col_name_ | The name of the column to be created in the table. |
44 |
| -_col_type_ | The data type of the column indicated by _col_name_. |
45 |
| -**NOT NULL** | Do not allow the column to contain _NULL_ values. Columns without this constraint can contain _NULL_ values. |
46 |
| -*default_expr* | A default value to use for the column in an [`INSERT`](/sql/insert) statement if an explicit value is not provided. If not specified, `NULL` is assumed. |
47 |
| -_retention_period_ | ***Private preview.** This option has known performance or stability issues and is under active development.* Duration for which Materialize retains historical data, which is useful to implement [durable subscriptions](/transform-data/patterns/durable-subscriptions/#history-retention-period). Accepts positive [interval](/sql/types/interval/) values (e.g. `'1hr'`). Default: `1s`. |
| 57 | +{{< note >}} |
| 58 | + |
| 59 | +Users cannot write to source-populated tables; i.e., users cannot perform |
| 60 | +[`INSERT`](/sql/insert/)/[`UPDATE`](/sql/update/)/[`DELETE`](/sql/delete/) |
| 61 | +operations on source-populated tables. |
| 62 | + |
| 63 | +{{</ note >}} |
| 64 | + |
| 65 | +```mzsql |
| 66 | +CREATE TABLE <table_name> FROM SOURCE <source_name> (REFERENCE <ref_object>) |
| 67 | +[WITH ( |
| 68 | + TEXT COLUMNS (<fq_column_name> [, ...]) |
| 69 | + | EXCLUDE COLUMNS (<fq_column_name> [, ...]) |
| 70 | + | PARTITION BY (<fq_column_name> [, ...]) |
| 71 | + [, ...] |
| 72 | +)] |
| 73 | +; |
| 74 | +``` |
| 75 | + |
| 76 | +{{% yaml-table data="syntax_options/create_table_options_source_populated_db" %}} |
| 77 | + |
| 78 | +{{</ tab >}} |
| 79 | + |
| 80 | + |
| 81 | +{{</ tabs >}} |
48 | 82 |
|
49 | 83 | ## Details
|
50 | 84 |
|
| 85 | +### Table names and column names |
| 86 | + |
| 87 | +Names for tables and column(s) must follow the [naming |
| 88 | +guidelines](/sql/identifiers/#naming-restrictions). |
| 89 | + |
51 | 90 | ### Known limitations
|
52 | 91 |
|
53 | 92 | Tables do not currently support:
|
@@ -104,4 +143,9 @@ The privileges required to execute this statement are:
|
104 | 143 | ## Related pages
|
105 | 144 |
|
106 | 145 | - [`INSERT`](../insert)
|
| 146 | +- [`CREATE SOURCE`](/sql/create-source/) |
107 | 147 | - [`DROP TABLE`](../drop-table)
|
| 148 | + |
| 149 | +[`INSERT`]: /sql/insert/ |
| 150 | +[`UPDATE`]: /sql/update/ |
| 151 | +[`DELETE`]: /sql/delete/ |
0 commit comments