|
| 1 | +- name: "create-table" |
| 2 | + description: | |
| 3 | + To create new **read-only** tables from a source, in the `CREATE TABLE FROM |
| 4 | + SOURCE` statement, specify the table in the publication using the |
| 5 | + `REFERENCE` field. |
| 6 | +
|
| 7 | + The following example creates **read-only** tables `items` and `orders` that |
| 8 | + are populated from the corresponding `items` and `orders` tables from the |
| 9 | + MySQL source `mz_source`. |
| 10 | +
|
| 11 | + {{< note >}} |
| 12 | +
|
| 13 | + - Although the example creates the tables with the same names as the |
| 14 | + upstream tables, the tables in Materialize can have names that differ from |
| 15 | + the referenced table names. |
| 16 | +
|
| 17 | + - For supported MySQL data types, refer to [supported |
| 18 | + types](#upstream-sources-and-supported-data-types). |
| 19 | +
|
| 20 | + - You can create multiple tables that reference the same upstream table. |
| 21 | +
|
| 22 | + {{< /note >}} |
| 23 | +
|
| 24 | + code: | |
| 25 | + /* This example assumes: |
| 26 | + - In the upstream MySQL, you have: |
| 27 | + - Enabled GTID-based binlog replication |
| 28 | + - Defined a replication user and password with the appropriate access. |
| 29 | + - In Materialize, |
| 30 | + - You have defined the connection to the upstream MySQL. |
| 31 | + - You have used the connection to create a source; e.g., |
| 32 | + CREATE SECRET mysql_password AS '<replication user password>'; -- substitute |
| 33 | + CREATE CONNECTION mysql_connection TO MYSQL ( |
| 34 | + HOST '<hostname>', -- substitute |
| 35 | + USER <replication user>, -- substitute |
| 36 | + PASSWORD SECRET mysql_password |
| 37 | + -- [, <network security configuration> ] |
| 38 | + ); |
| 39 | +
|
| 40 | + CREATE SOURCE mz_source |
| 41 | + FROM MYSQL CONNECTION mysql_connection; |
| 42 | + */ |
| 43 | +
|
| 44 | + CREATE TABLE items |
| 45 | + FROM SOURCE mz_source(REFERENCE public.items) |
| 46 | + ; |
| 47 | + CREATE TABLE orders |
| 48 | + FROM SOURCE mz_source(REFERENCE public.orders) |
| 49 | + ; |
| 50 | +
|
| 51 | +- name: "show-tables" |
| 52 | + description: | |
| 53 | + To verify that the table has been created, you can run [`SHOW |
| 54 | + TABLES`](/sql/show-tables/) to list all tables in the current [schema](/sql/namespaces/#namespace-hierarchy): |
| 55 | + code: | |
| 56 | + SHOW TABLES; |
| 57 | + results: | |
| 58 | + The results should include the tables `items` and `orders`: |
| 59 | +
|
| 60 | + ```hc {hl_lines="3-4"} |
| 61 | + | name | comment | |
| 62 | + | ----------- | ------- | |
| 63 | + | items | | |
| 64 | + | orders | | |
| 65 | + ``` |
| 66 | +
|
| 67 | +- name: "show-columns" |
| 68 | + description: | |
| 69 | + Inspect the table columns using the [`SHOW COLUMNS`](/sql/show-columns/) command: |
| 70 | + code: | |
| 71 | + SHOW COLUMNS FROM items; |
| 72 | + SHOW COLUMNS FROM orders; |
| 73 | + results: | |
| 74 | + The results should display information on the table columns. The types |
| 75 | + should match those from the upstream table. |
| 76 | +
|
| 77 | + For the list of supported PostgreSQL data types, refer to [supported |
| 78 | + types](#upstream-sources-and-supported-data-types). |
| 79 | +
|
| 80 | +- name: "read-from-table" |
| 81 | + code: | |
| 82 | + SELECT * FROM items; |
| 83 | +
|
| 84 | +- name: "create-view-from-tables" |
| 85 | + code: | |
| 86 | + CREATE VIEW orders_view AS |
| 87 | + SELECT o.*,i.price,o.quantity * i.price as subtotal |
| 88 | + FROM orders as o |
| 89 | + JOIN items as i |
| 90 | + ON o.item = i.item; |
0 commit comments