|
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 |
| -Field | Use |
36 |
| -------|----- |
37 |
| -**TEMP** / **TEMPORARY** | Mark the table as [temporary](#temporary-tables). |
38 |
| -_table_name_ | A name for the table. |
39 |
| -_col_name_ | The name of the column to be created in the table. |
40 |
| -_col_type_ | The data type of the column indicated by _col_name_. |
41 |
| -**NOT NULL** | Do not allow the column to contain _NULL_ values. Columns without this constraint can contain _NULL_ values. |
42 |
| -*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. |
| 36 | +```mzsql |
| 37 | +CREATE [TEMP|TEMPORARY] TABLE <table_name> ( |
| 38 | + <column_name> <column_type> [NOT NULL][DEFAULT <default_expr>] |
| 39 | + [, ...] |
| 40 | +) |
| 41 | +[WITH ( |
| 42 | + PARTITION BY (<column_name> [, ...]) | |
| 43 | + RETAIN HISTORY [=] FOR <duration> |
| 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)" >}} |
| 53 | + |
| 54 | +To create a table from a [source](/sql/create-source/), where the source maps to |
| 55 | +an external database system: |
43 | 56 |
|
44 |
| -### `with_options` |
| 57 | +{{< note >}} |
45 | 58 |
|
46 |
| -{{< diagram "with-options.svg" >}} |
| 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. |
47 | 62 |
|
48 |
| -| Field | Value | Description | |
49 |
| -|------------------------------------------|---------------------| ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
50 |
| -| **PARTITION BY** _columns_ | `(ident [, ident]*)` | The key by which Materialize should internally partition this durable collection. See the [partitioning guide](/transform-data/patterns/partition-by/) for restrictions on valid values and other details. |
51 |
| -| **RETAIN HISTORY FOR** _retention_period_ | `interval` | ***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`. |
| 63 | +{{</ note >}} |
52 | 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 (<column_name> [, ...]) |
| 71 | + [, ...] |
| 72 | +)] |
| 73 | +; |
| 74 | +``` |
| 75 | + |
| 76 | +{{% yaml-table data="syntax_options/create_table_options_source_populated_db" %}} |
| 77 | + |
| 78 | +<a name="supported-db-source-types" ></a> |
| 79 | + |
| 80 | +{{< tabs >}} |
| 81 | +{{< tab "Supported MySQL types">}} |
| 82 | + |
| 83 | +Materialize natively supports the following MySQL types: |
| 84 | + |
| 85 | +<ul style="column-count: 3"> |
| 86 | +<li><code>bigint</code></li> |
| 87 | +<li><code>binary</code></li> |
| 88 | +<li><code>bit</code></li> |
| 89 | +<li><code>blob</code></li> |
| 90 | +<li><code>boolean</code></li> |
| 91 | +<li><code>char</code></li> |
| 92 | +<li><code>date</code></li> |
| 93 | +<li><code>datetime</code></li> |
| 94 | +<li><code>decimal</code></li> |
| 95 | +<li><code>double</code></li> |
| 96 | +<li><code>float</code></li> |
| 97 | +<li><code>int</code></li> |
| 98 | +<li><code>json</code></li> |
| 99 | +<li><code>longblob</code></li> |
| 100 | +<li><code>longtext</code></li> |
| 101 | +<li><code>mediumblob</code></li> |
| 102 | +<li><code>mediumint</code></li> |
| 103 | +<li><code>mediumtext</code></li> |
| 104 | +<li><code>numeric</code></li> |
| 105 | +<li><code>real</code></li> |
| 106 | +<li><code>smallint</code></li> |
| 107 | +<li><code>text</code></li> |
| 108 | +<li><code>time</code></li> |
| 109 | +<li><code>timestamp</code></li> |
| 110 | +<li><code>tinyblob</code></li> |
| 111 | +<li><code>tinyint</code></li> |
| 112 | +<li><code>tinytext</code></li> |
| 113 | +<li><code>varbinary</code></li> |
| 114 | +<li><code>varchar</code></li> |
| 115 | +</ul> |
| 116 | + |
| 117 | +Replicating tables that contain **unsupported data types** is |
| 118 | +possible via the [`TEXT COLUMNS` option](#text-columns) for the |
| 119 | +following types: |
| 120 | + |
| 121 | +<ul style="column-count: 1"> |
| 122 | +<li><code>enum</code></li> |
| 123 | +<li><code>year</code></li> |
| 124 | +</ul> |
| 125 | + |
| 126 | +The specified columns will be treated as `text`, and will thus not offer the |
| 127 | +expected MySQL type features. For any unsupported data types not listed above, |
| 128 | +use the [`EXCLUDE COLUMNS`](#exclude-columns) option. |
| 129 | + |
| 130 | +{{</ tab >}} |
| 131 | + |
| 132 | +{{< tab "Supported PostgreSQL types">}} |
| 133 | +Materialize natively supports the following PostgreSQL types (including the |
| 134 | +array type for each of the types): |
| 135 | + |
| 136 | +<ul style="column-count: 3"> |
| 137 | +<li><code>bool</code></li> |
| 138 | +<li><code>bpchar</code></li> |
| 139 | +<li><code>bytea</code></li> |
| 140 | +<li><code>char</code></li> |
| 141 | +<li><code>date</code></li> |
| 142 | +<li><code>daterange</code></li> |
| 143 | +<li><code>float4</code></li> |
| 144 | +<li><code>float8</code></li> |
| 145 | +<li><code>int2</code></li> |
| 146 | +<li><code>int2vector</code></li> |
| 147 | +<li><code>int4</code></li> |
| 148 | +<li><code>int4range</code></li> |
| 149 | +<li><code>int8</code></li> |
| 150 | +<li><code>int8range</code></li> |
| 151 | +<li><code>interval</code></li> |
| 152 | +<li><code>json</code></li> |
| 153 | +<li><code>jsonb</code></li> |
| 154 | +<li><code>numeric</code></li> |
| 155 | +<li><code>numrange</code></li> |
| 156 | +<li><code>oid</code></li> |
| 157 | +<li><code>text</code></li> |
| 158 | +<li><code>time</code></li> |
| 159 | +<li><code>timestamp</code></li> |
| 160 | +<li><code>timestamptz</code></li> |
| 161 | +<li><code>tsrange</code></li> |
| 162 | +<li><code>tstzrange</code></li> |
| 163 | +<li><code>uuid</code></li> |
| 164 | +<li><code>varchar</code></li> |
| 165 | +</ul> |
| 166 | + |
| 167 | +Replicating tables that contain **unsupported data types** is possible via the |
| 168 | +[`TEXT COLUMNS` option](#text-columns). When decoded as `text`, the specified |
| 169 | +columns will not have the expected PostgreSQL type features. For example: |
| 170 | + |
| 171 | +* [`enum`]: When decoded as `text`, the resulting `text` values will |
| 172 | + not observe the implicit ordering of the original PostgreSQL `enum`; instead, |
| 173 | + Materialize will sort the values as `text`. |
| 174 | + |
| 175 | +* [`money`]: When decoded as `text`, the resulting `text` value |
| 176 | + cannot be cast back to `numeric` since PostgreSQL adds typical currency |
| 177 | + formatting to the output. |
| 178 | + |
| 179 | +[`enum`]: https://www.postgresql.org/docs/current/datatype-enum.html |
| 180 | +[`money`]: https://www.postgresql.org/docs/current/datatype-money.html |
| 181 | + |
| 182 | +{{</ tab >}} |
| 183 | +{{</ tabs >}} |
| 184 | + |
| 185 | +See also [Materialize SQL data types](/sql/types/). |
| 186 | + |
| 187 | +{{</ tab >}} |
| 188 | + |
| 189 | + |
| 190 | +{{</ tabs >}} |
53 | 191 |
|
54 | 192 | ## Details
|
55 | 193 |
|
| 194 | +### Table names and column names |
| 195 | + |
| 196 | +Names for tables and column(s) must follow the [naming |
| 197 | +guidelines](/sql/identifiers/#naming-restrictions). |
| 198 | + |
56 | 199 | ### Known limitations
|
57 | 200 |
|
58 | 201 | Tables do not currently support:
|
@@ -109,4 +252,9 @@ The privileges required to execute this statement are:
|
109 | 252 | ## Related pages
|
110 | 253 |
|
111 | 254 | - [`INSERT`](../insert)
|
| 255 | +- [`CREATE SOURCE`](/sql/create-source/) |
112 | 256 | - [`DROP TABLE`](../drop-table)
|
| 257 | + |
| 258 | +[`INSERT`]: /sql/insert/ |
| 259 | +[`UPDATE`]: /sql/update/ |
| 260 | +[`DELETE`]: /sql/delete/ |
0 commit comments