Skip to content

Commit 1327b64

Browse files
committed
docs: create table update syntax (part 1)
1 parent 9fb7a6e commit 1327b64

File tree

5 files changed

+198
-57
lines changed

5 files changed

+198
-57
lines changed

doc/user/assets/sass/_content.scss

+5-6
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,7 @@ p+p {
749749
padding: 0;
750750
border-bottom: 1px solid #9c86e0;
751751
display: flex;
752-
overflow-x: scroll;
753-
754-
padding-bottom: var(--xx-small);
752+
overflow-x: auto;
755753

756754
@media(max-width: 850px) {}
757755

@@ -760,13 +758,15 @@ p+p {
760758
margin: 0 rem(0.1);
761759
padding: 0;
762760
position: relative;
763-
bottom: -1px;
761+
764762
background: var(--gray-lightest);
763+
border-radius: 8px 8px 0 0;
764+
765765

766766
a {
767767
color: var(--body);
768768
display: block;
769-
padding: rem(0.8) rem(1.6);
769+
padding: rem(0.8) rem(1.5);
770770
font-size: rem(1.4);
771771
text-decoration: none;
772772
font-weight: 500;
@@ -787,7 +787,6 @@ p+p {
787787

788788
&.active {
789789
background: var(--bg);
790-
border-radius: 2px 2px 0 0;
791790
border: 1px solid #9c86e0;
792791
border-bottom-color: var(--bg);
793792

doc/user/content/sql/create-table.md

+67-25
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,82 @@ menu:
99
parent: 'commands'
1010
---
1111

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:
1414

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.
1817

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.
2321

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.
2625

2726
## Syntax
2827

29-
{{< diagram "create-table.svg" >}}
28+
{{< tabs >}}
3029

31-
### `col_option`
30+
{{< tab "User-populated tables" >}}
3231

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):
3435

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>
43+
)]
44+
;
45+
```
46+
47+
{{% yaml-table data="syntax_options/create_table_options_user_populated" %}}
48+
49+
{{</ tab >}}
50+
51+
{{< tab "Source-populated tables (DB source)" >}}
3652

37-
{{< diagram "with-options-retain-history.svg" >}}
53+
To create a table from a [source](/sql/create-source/), where the source maps to
54+
an external database system:
3855

39-
Field | Use
40-
------|-----
41-
**TEMP** / **TEMPORARY** | Mark the table as [temporary](#temporary-tables).
42-
_table&lowbar;name_ | A name for the table.
43-
_col&lowbar;name_ | The name of the column to be created in the table.
44-
_col&lowbar;type_ | The data type of the column indicated by _col&lowbar;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`.
56+
{{< note >}}
57+
58+
Users cannot write to source-populated tables; i.e., users cannot perform
59+
[`INSERT`](/sql/insert/)/[`UPDATE`](/sql/update/)/[`DELETE`](/sql/delete/)
60+
operations on source-populated tables.
61+
62+
{{</ note >}}
63+
64+
```mzsql
65+
CREATE TABLE <table_name> FROM SOURCE <source_name> (REFERENCE <ref_object>)
66+
[WITH (
67+
TEXT COLUMNS (<fq_column_name> [, ...])
68+
| EXCLUDE COLUMNS (<fq_column_name> [, ...])
69+
[, ...]
70+
)]
71+
;
72+
```
73+
74+
{{% yaml-table data="syntax_options/create_table_options_source_populated_db" %}}
75+
76+
{{</ tab >}}
77+
78+
79+
{{</ tabs >}}
4880

4981
## Details
5082

83+
### Table names and column names
84+
85+
Names for tables and column(s) must follow the [naming
86+
guidelines](/sql/identifiers/#naming-restrictions).
87+
5188
### Known limitations
5289

5390
Tables do not currently support:
@@ -104,4 +141,9 @@ The privileges required to execute this statement are:
104141
## Related pages
105142

106143
- [`INSERT`](../insert)
144+
- [`CREATE SOURCE`](/sql/create-source/)
107145
- [`DROP TABLE`](../drop-table)
146+
147+
[`INSERT`]: /sql/insert/
148+
[`UPDATE`]: /sql/update/
149+
[`DELETE`]: /sql/delete/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
columns:
2+
- column: "Parameter"
3+
- column: "Description"
4+
rows:
5+
- "Parameter": "`<table_name>`"
6+
"Description": |
7+
8+
The name of the table to create. Names for tables must follow the [naming
9+
guidelines](/sql/identifiers/#naming-restrictions).
10+
11+
- "Parameter": "`<source_name>`"
12+
"Description": |
13+
14+
The name of the [source](/sql/create-source/) associated with the
15+
reference object from which to create the table.
16+
17+
- "Parameter": "**(REFERENCE <ref_object>)**"
18+
"Description": |
19+
20+
The name of the reference object from which to create the table. Reference
21+
objects are the names of the tables in the upstream database. You can
22+
create multiple tables from the same reference object.
23+
24+
To find the reference objects available in your
25+
[source](/sql/create-source/), you can use the following query,
26+
substituting your source name for `<source_name>`:
27+
28+
<br>
29+
30+
```mzsql
31+
SELECT refs.*
32+
FROM mz_internal.mz_source_references refs, mz_sources s
33+
WHERE s.name = '<source_name>' -- substitute with your source name
34+
AND refs.source_id = s.id;
35+
```
36+
37+
- "Parameter": "**WITH (<with_option>[,...])**"
38+
"Description": |
39+
The following `<with_option>`s are supported:
40+
41+
| Option | Description |
42+
|--------|-------------|
43+
| `TEXT COLUMNS (<fq_column_name> [, ...])` | *Optional.* If specified, decode data as `text` for the listed column(s). Use fully qualified column names. |
44+
| `EXCLUDE COLUMNS (<fq_column_name> [, ...])` | *Optional.* If specified, exclude the listed column(s) from the table. Use fully qualified column names. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
columns:
2+
- column: "Parameter"
3+
- column: "Description"
4+
rows:
5+
- "Parameter": "**TEMP** / **TEMPORARY**"
6+
"Description": |
7+
*Optional.* If specified, mark the table as [temporary](#temporary-tables). Temporary
8+
tables are automatically dropped at the end of the SQL session and are not
9+
visible to other connections. See [temporary tables](#temporary-tables)
10+
for more details.
11+
- "Parameter": "`<table_name>`"
12+
"Description": |
13+
14+
The name of the table to create. Names for tables must follow the [naming
15+
guidelines](/sql/identifiers/#naming-restrictions).
16+
17+
- "Parameter": "`<column_name>`"
18+
"Description": |
19+
20+
The name of a column to be created in the new table. Names for columns
21+
must follow the [naming guidelines](/sql/identifiers/#naming-restrictions).
22+
23+
- "Parameter": "`<column_type>`"
24+
"Description": |
25+
26+
The type of the column. For supported types, see [SQL data types](/sql/types/).
27+
28+
- "Parameter": "**NOT NULL**"
29+
"Description": |
30+
*Optional.* If specified, disallow _NULL_ values for the column. Columns without this constraint can contain _NULL_ values.
31+
- "Parameter": "**DEFAULT <default_expr>**"
32+
"Description": |
33+
*Optional.* If specified, use the `<default_expr>` as the default value for the column. If not specified, `NULL` is used as the default value.
34+
- "Parameter": "**WITH (<with_option>[,...])**"
35+
"Description": |
36+
37+
The following `<with_option>`s are supported:
38+
39+
| Option | Description |
40+
|--------|-------------|
41+
| `RETAIN HISTORY <duration>` | *Optional.* ***Private preview.** This option has known performance or stability issues and is under active development.* <br>If specified, Materialize retains historical data for the specified duration, which is useful to implement [durable subscriptions](/transform-data/patterns/durable-subscriptions/#history-retention-period).<br>Accepts positive [interval](/sql/types/interval/) values (e.g., `'1hr'`).|

doc/user/layouts/partials/head.html

+41-26
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,50 @@
115115

116116
{{/* Tabs */}}
117117
<script>
118-
$(document).ready(function () {
119-
// make nav-tab lists from tab-panes
120-
$(".tab-content").each(function (idx, tab) {
121-
$(tab)
122-
.find(".tab-pane")
123-
.each(function (item) {
124-
var navTabs = $(this).closest(".code-tabs").find(".nav-tabs"),
125-
title = $(this).attr("title"),
126-
id = title
127-
.toLowerCase()
128-
.replace(/ /g, "-")
129-
.replace(/[^\w-]+/g, "");
130-
navTabs.append(`<li><a href="#${id}-t${idx}">${title}</a></li>`);
131-
});
132-
});
118+
document.addEventListener("DOMContentLoaded", function () {
119+
document.querySelectorAll(".code-tabs").forEach(function (tabGroup, groupIndex) {
120+
const navTabs = tabGroup.querySelector(".nav-tabs");
121+
const tabContent = tabGroup.querySelector(".tab-content");
122+
const tabPanes = Array.from(tabContent.children).filter(child =>
123+
child.classList.contains("tab-pane")
124+
);
125+
126+
// Create tab headers from panes
127+
tabPanes.forEach(function (pane, tabIndex) {
128+
const title = pane.getAttribute("title") || `Tab ${tabIndex + 1}`;
129+
const id = `tab-${groupIndex}-${tabIndex}`;
130+
pane.setAttribute("id", id);
133131

134-
// handle click events
135-
$(".nav-tabs a").click(function (e) {
136-
var tab = $(this).parent(),
137-
tabIndex = tab.index(),
138-
tabPanel = $(this).closest(".code-tabs"),
139-
tabPane = tabPanel.find(".tab-pane").eq(tabIndex);
140-
tabPanel.find(".active").removeClass("active");
141-
tab.addClass("active");
142-
tabPane.addClass("active");
132+
const tabItem = document.createElement("li");
133+
const link = document.createElement("a");
134+
link.setAttribute("href", `#${id}`);
135+
link.textContent = title;
136+
tabItem.appendChild(link);
137+
navTabs.appendChild(tabItem);
143138
});
144139

145-
// activate first tab
146-
$(".nav-tabs li:first-child a").click();
140+
// Handle tab click events
141+
navTabs.querySelectorAll("a").forEach(function (link, index) {
142+
link.addEventListener("click", function (e) {
143+
e.preventDefault();
144+
145+
// Deactivate all tabs and panes
146+
navTabs.querySelectorAll("li").forEach(li => li.classList.remove("active"));
147+
tabPanes.forEach(pane => pane.classList.remove("active"));
147148

149+
// Activate clicked tab and corresponding pane
150+
link.parentElement.classList.add("active");
151+
const targetPane = tabContent.querySelector(link.getAttribute("href"));
152+
if (targetPane) {
153+
targetPane.classList.add("active");
154+
}
155+
});
156+
});
157+
158+
// Activate the first tab by default
159+
const firstLink = navTabs.querySelector("a");
160+
if (firstLink) firstLink.click();
148161
});
162+
});
163+
149164
</script>

0 commit comments

Comments
 (0)