From bc0b0b27ad6eef93d1c1b380538ce88133ee923f Mon Sep 17 00:00:00 2001 From: iamlinjunhong <1030420200@qq.com> Date: Tue, 6 Jan 2026 17:06:41 +0800 Subject: [PATCH 1/6] doc of new tmp table --- .../schema-design/create-temporary-table.md | 7 +- .../Data-Definition-Language/alter-table.md | 2 +- .../Data-Definition-Language/create-table.md | 9 +- .../create-temporary-table.md | 83 +++++++++++++++++++ mkdocs.yml | 1 + 5 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md diff --git a/docs/MatrixOne/Develop/schema-design/create-temporary-table.md b/docs/MatrixOne/Develop/schema-design/create-temporary-table.md index e275ec4c72..5cbadb4ba6 100644 --- a/docs/MatrixOne/Develop/schema-design/create-temporary-table.md +++ b/docs/MatrixOne/Develop/schema-design/create-temporary-table.md @@ -40,11 +40,12 @@ The syntax for using a temporary table is the same as for a regular table, excep CREATE TEMPORARY TABLE temp_table_name (column_list); ``` -You can use the same table name for temporary and regular tables without conflicts because they are in different namespaces. However, two temporary tables cannot share the same name in the session. +You can use the same table name for temporary and regular tables. If you do so, the temporary table will shadow (override) the regular table for almost all operations within the current session until the temporary table is dropped. However, two temporary tables cannot share the same name in the same session. !!! note - 1. Even though temporary tables can have the same name as permanent tables, it is not recommended as this may result in unexpected data loss. For example, if the connection to the database server is lost and you automatically reconnect to the server, you cannot distinguish between temporary and permanent tables. Then, you issue a `DROP TABLE` statement. This time, the permanent table may be deleted instead of the temporary table. This result is unpredictable. - 2. When you use the 'SHOW TABLES' command to display a list of data tables, you cannot see a list of temporary tables either. + 1. When a temporary table and a regular table have the same name, the temporary table takes precedence. Operations such as `SELECT`, `INSERT`, `UPDATE`, `DESC`, and `SHOW CREATE TABLE` will interact with the temporary table. + 2. When using the `SHOW TABLES` command, temporary tables are not displayed in the result list. + 3. Temporary tables currently only support index-related `ALTER TABLE` operations, such as adding or dropping an index. Other structural modifications via `ALTER TABLE` are not yet supported. ## Example diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md index 4e3c18741a..d16a168077 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md @@ -257,5 +257,5 @@ mysql> show tables; ## Limitations 1. The following clauses: `CHANGE [COLUMN]`, `MODIFY [COLUMN]`, `RENAME COLUMN`, `ADD [CONSTRAINT [symbol]] PRIMARY KEY`, `DROP PRIMARY KEY`, and `ALTER COLUMN ORDER BY` can be freely combined in an `ALTER TABLE` statement but are currently not supported with other clauses. -2. Temporary tables do not currently support structural modifications via `ALTER TABLE`. +2. Temporary tables only support index-related operations (e.g., `ADD INDEX`, `DROP INDEX`) via `ALTER TABLE`. Other structural modifications are not yet supported. 3. Tables created with `CREATE TABLE ... CLUSTER BY...` cannot be modified using `ALTER TABLE`. \ No newline at end of file diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md index caf33ccc95..d883ce2d82 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md @@ -114,11 +114,14 @@ For more detailed syntax explanations, see the following content. #### Temporary Tables -You can use the `TEMPORARY` keyword when creating a table. A `TEMPORARY` table is visible only within the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.) +You can use the `TEMPORARY` keyword when creating a table. A `TEMPORARY` table is visible only within the current session, and is dropped automatically when the session is closed. -Dropping a database does automatically drop any `TEMPORARY` tables created within that database. +Key behaviors of temporary tables: +- **Shadowing**: If a temporary table has the same name as a regular table, the temporary table takes precedence for DML and inspection commands (like `DESC`). +- **Persistence**: Data in temporary tables is stored in memory and is not persistent. +- **ALTER support**: Only index-related `ALTER` operations are supported. -The creating session can perform any operation on the table, such as `DROP TABLE`, `INSERT`, `UPDATE`, or `SELECT`. +For more information, see [CREATE TEMPORARY TABLE](create-temporary-table.md). #### COMMENT diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md new file mode 100644 index 0000000000..f904c67ada --- /dev/null +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md @@ -0,0 +1,83 @@ +# **CREATE TEMPORARY TABLE** + +## **Description** + +Creates a new temporary table. A temporary table is a special type of table that is visible only within the current session and is dropped automatically when the session is closed. + +## **Syntax** + +```sql +CREATE TEMPORARY TABLE [IF NOT EXISTS] tbl_name + (create_definition,...) + [table_options] + [partition_options] + +create_definition: { + col_name column_definition + | [CONSTRAINT [symbol]] PRIMARY KEY + [index_type] (key_part,...) + [index_option] ... +} + +column_definition: { + data_type [NOT NULL | NULL] [DEFAULT {literal | (expr)} ] + [AUTO_INCREMENT] [UNIQUE [KEY]] [[PRIMARY] KEY] + [COMMENT 'string'] +} +``` + +## **Explanations** + +- **Visibility**: A temporary table is visible only within the current session. Different sessions can use the same temporary table name without conflict. +- **Shadowing**: If a temporary table has the same name as an existing regular table, the regular table is "shadowed" or hidden by the temporary table within that session. Operations like `SELECT`, `INSERT`, `SHOW CREATE TABLE`, `SHOW COLUMNS`, and `DESC` will target the temporary table rather than the regular one. +- **Lifecycle**: Temporary tables are automatically dropped when the session ends. You can also explicitly drop them using `DROP TABLE` or `DROP TEMPORARY TABLE`. +- **Database Scope**: Dropping a database automatically drops any temporary tables created within that database in the current session. +- **Limited ALTER Support**: Currently, temporary tables only support index-related `ALTER TABLE` operations (e.g., `ADD INDEX`, `DROP INDEX`). Other structural modifications are not yet supported. + +## **Examples** + +- **Example 1: Create and use a temporary table** + +```sql +CREATE TEMPORARY TABLE temp_t1 (a INT, b VARCHAR(10)); +INSERT INTO temp_t1 VALUES (1, 'abc'); + +mysql> SELECT * FROM temp_t1; ++------+------+ +| a | b | ++------+------+ +| 1 | abc | ++------+------+ + +-- The table will be automatically dropped when the session ends. +``` + +- **Example 2: Shadowing a regular table** + +```sql +CREATE TABLE t1 (a INT); -- Regular table +INSERT INTO t1 VALUES (1); + +CREATE TEMPORARY TABLE t1 (b INT); -- Temporary table with same name +INSERT INTO t1 VALUES (2); + +mysql> SELECT * FROM t1; ++------+ +| b | ++------+ +| 2 | ++------+ +-- The temporary table shadows the regular table. + +mysql> DESC t1; ++-------+---------+------+------+---------+-------+---------+ +| Field | Type | Null | Key | Default | Extra | Comment | ++-------+---------+------+------+---------+-------+---------+ +| b | INT(32) | YES | | NULL | | | ++-------+---------+------+------+---------+-------+---------+ +``` + +## **Constraints** + +1. Temporary tables currently do not support `FOREIGN KEY` constraints. +2. Except for index-related operations, other `ALTER TABLE` operations (like changing columns or renaming the table) are not supported for temporary tables. diff --git a/mkdocs.yml b/mkdocs.yml index 077fc203e6..c88962e8db 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -347,6 +347,7 @@ nav: - CREATE INDEX...USING HNSW: MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-index-hnsw.md - CREATE FULLTEXT INDEX: MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-fulltext-index.md - CREATE TABLE: MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md + - CREATE TEMPORARY TABLE: MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md - CREATE TABLE AS SELECT: MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table-as-select.md - CREATE TABLE ... LIKE: MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table-like.md - CREATE EXTERNAL TABLE: MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-external-table.md From d1371560014840aaa8dba3ec6fcc6535e915b07c Mon Sep 17 00:00:00 2001 From: iamlinjunhong <1030420200@qq.com> Date: Tue, 6 Jan 2026 17:12:02 +0800 Subject: [PATCH 2/6] fix --- .../Data-Definition-Language/create-table.md | 1 + .../Data-Definition-Language/create-temporary-table.md | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md index d883ce2d82..3556ad1130 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md @@ -117,6 +117,7 @@ For more detailed syntax explanations, see the following content. You can use the `TEMPORARY` keyword when creating a table. A `TEMPORARY` table is visible only within the current session, and is dropped automatically when the session is closed. Key behaviors of temporary tables: + - **Shadowing**: If a temporary table has the same name as a regular table, the temporary table takes precedence for DML and inspection commands (like `DESC`). - **Persistence**: Data in temporary tables is stored in memory and is not persistent. - **ALTER support**: Only index-related `ALTER` operations are supported. diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md index f904c67ada..cc6784eed9 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md @@ -36,7 +36,7 @@ column_definition: { ## **Examples** -- **Example 1: Create and use a temporary table** +- **Example 1: Create and use a temporary table** ```sql CREATE TEMPORARY TABLE temp_t1 (a INT, b VARCHAR(10)); @@ -52,7 +52,7 @@ mysql> SELECT * FROM temp_t1; -- The table will be automatically dropped when the session ends. ``` -- **Example 2: Shadowing a regular table** +- **Example 2: Shadowing a regular table** ```sql CREATE TABLE t1 (a INT); -- Regular table @@ -79,5 +79,5 @@ mysql> DESC t1; ## **Constraints** -1. Temporary tables currently do not support `FOREIGN KEY` constraints. -2. Except for index-related operations, other `ALTER TABLE` operations (like changing columns or renaming the table) are not supported for temporary tables. +1. Temporary tables currently do not support `FOREIGN KEY` constraints. +2. Except for index-related operations, other `ALTER TABLE` operations (like changing columns or renaming the table) are not supported for temporary tables. From 36925a784dfca588c1735286f7ca134df107c297 Mon Sep 17 00:00:00 2001 From: iamlinjunhong <1030420200@qq.com> Date: Tue, 6 Jan 2026 17:13:29 +0800 Subject: [PATCH 3/6] fix --- .../Develop/schema-design/create-temporary-table.md | 2 +- .../Data-Definition-Language/create-temporary-table.md | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/MatrixOne/Develop/schema-design/create-temporary-table.md b/docs/MatrixOne/Develop/schema-design/create-temporary-table.md index 5cbadb4ba6..21b933fccf 100644 --- a/docs/MatrixOne/Develop/schema-design/create-temporary-table.md +++ b/docs/MatrixOne/Develop/schema-design/create-temporary-table.md @@ -36,7 +36,7 @@ Before reading this document, make sure that the following tasks are completed: The syntax for using a temporary table is the same as for a regular table, except that the TEMPORARY keyword is added before the statement that creates the table: -```sql +``` CREATE TEMPORARY TABLE temp_table_name (column_list); ``` diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md index cc6784eed9..ffa8657b16 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md @@ -55,10 +55,12 @@ mysql> SELECT * FROM temp_t1; - **Example 2: Shadowing a regular table** ```sql -CREATE TABLE t1 (a INT); -- Regular table +-- Regular table +CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1); -CREATE TEMPORARY TABLE t1 (b INT); -- Temporary table with same name +-- Temporary table with same name +CREATE TEMPORARY TABLE t1 (b INT); INSERT INTO t1 VALUES (2); mysql> SELECT * FROM t1; From 98488e37a613f07e1d3a2404b99205ab1f49b2aa Mon Sep 17 00:00:00 2001 From: iamlinjunhong <1030420200@qq.com> Date: Tue, 6 Jan 2026 17:58:30 +0800 Subject: [PATCH 4/6] fix --- .../Data-Definition-Language/alter-table.md | 3 +++ .../Data-Definition-Language/create-table.md | 6 +++++ .../create-temporary-table.md | 22 ++++++++++--------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md index d16a168077..92863a0391 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md @@ -82,8 +82,10 @@ Below are explanations for each parameter: ```sql -- Create table f1 with two integer columns: fa (PRIMARY KEY) and fb (UNIQUE KEY) +drop table if exists f1; CREATE TABLE f1(fa INT PRIMARY KEY, fb INT UNIQUE KEY); -- Create table c1 with two integer columns: ca and cb +drop table if exists c1; CREATE TABLE c1 (ca INT, cb INT); -- Add a FOREIGN KEY constraint named ffa to c1, linking column ca to f1.fa ALTER TABLE c1 ADD CONSTRAINT ffa FOREIGN KEY (ca) REFERENCES f1(fa); @@ -120,6 +122,7 @@ mysql> select ca, cb from c1 order by ca; ```sql -- Create table t1 with columns a (INTEGER), b (CHAR(10)), c (DATE), d (DECIMAL(7,2)), and a UNIQUE KEY on (a, b) +drop table if exists t1; CREATE TABLE t1(a INTEGER, b CHAR(10), c DATE, d DECIMAL(7,2), UNIQUE KEY(a, b)); -- View the structure of t1 diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md index 3556ad1130..c9a730815a 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md @@ -202,6 +202,7 @@ The following is an example to illustrate the association of parent and child ta First, create a parent table with field a as the primary key: ```sql +drop table if exists t1; create table t1(a int primary key,b varchar(5)); insert into t1 values(101,'abc'),(102,'def'); mysql> select * from t1; @@ -217,6 +218,7 @@ mysql> select * from t1; Then create a child table with field c as the foreign key, associated with parent table field a: ```sql +drop table if exists t2; create table t2(a int ,b varchar(5),c int, foreign key(c) references t1(a)); insert into t2 values(1,'zs1',101),(2,'zs2',102); insert into t2 values(3,'xyz',null); @@ -250,11 +252,13 @@ See the example below: Suppose there are two tables, `Orders` and `Customers`, where the `Orders` table has a foreign key column `customer_id` referencing the `id` column in the `Customers` table. If, when a customer is deleted from the `Customers` table, you also want to delete the associated order data, you can use `ON DELETE CASCADE`. ```sql +drop table if exists Customers; CREATE TABLE Customers ( id INT PRIMARY KEY, name VARCHAR(50) ); +drop table if exists Orders; CREATE TABLE Orders ( id INT PRIMARY KEY, order_number VARCHAR(10), @@ -364,6 +368,7 @@ mysql> SELECT * FROM test; - Example 2: Add comments when creating a table ```sql +drop table if exists t2; create table t2 (a int, b int) comment = "fact table"; mysql> show create table t2; @@ -380,6 +385,7 @@ mysql> show create table t2; - Example 3: Add comments to columns when creating tables ```sql +drop table if exists t3; create table t3 (a int comment 'Column comment', b int) comment = "table"; mysql> SHOW CREATE TABLE t3; diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md index ffa8657b16..fd637f791b 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md @@ -39,10 +39,10 @@ column_definition: { - **Example 1: Create and use a temporary table** ```sql -CREATE TEMPORARY TABLE temp_t1 (a INT, b VARCHAR(10)); -INSERT INTO temp_t1 VALUES (1, 'abc'); +CREATE TEMPORARY TABLE temp_t_unique_1 (a INT, b VARCHAR(10)); +INSERT INTO temp_t_unique_1 VALUES (1, 'abc'); -mysql> SELECT * FROM temp_t1; +mysql> SELECT * FROM temp_t_unique_1; +------+------+ | a | b | +------+------+ @@ -54,16 +54,18 @@ mysql> SELECT * FROM temp_t1; - **Example 2: Shadowing a regular table** -```sql +Note: The following behavior (shadowing) depends on the session-level mapping. + +``` -- Regular table -CREATE TABLE t1 (a INT); -INSERT INTO t1 VALUES (1); +CREATE TABLE t1_shadow_test (a INT); +INSERT INTO t1_shadow_test VALUES (1); -- Temporary table with same name -CREATE TEMPORARY TABLE t1 (b INT); -INSERT INTO t1 VALUES (2); +CREATE TEMPORARY TABLE t1_shadow_test (b INT); +INSERT INTO t1_shadow_test VALUES (2); -mysql> SELECT * FROM t1; +mysql> SELECT * FROM t1_shadow_test; +------+ | b | +------+ @@ -71,7 +73,7 @@ mysql> SELECT * FROM t1; +------+ -- The temporary table shadows the regular table. -mysql> DESC t1; +mysql> DESC t1_shadow_test; +-------+---------+------+------+---------+-------+---------+ | Field | Type | Null | Key | Default | Extra | Comment | +-------+---------+------+------+---------+-------+---------+ From 61d9cc2731ebe3494c7b62c2cdee0dd1eeb0355f Mon Sep 17 00:00:00 2001 From: iamlinjunhong <1030420200@qq.com> Date: Tue, 6 Jan 2026 18:56:06 +0800 Subject: [PATCH 5/6] fix --- .../Data-Definition-Language/alter-table.md | 3 --- .../Data-Definition-Language/create-table.md | 20 +++++++------------ 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md index 92863a0391..d16a168077 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md @@ -82,10 +82,8 @@ Below are explanations for each parameter: ```sql -- Create table f1 with two integer columns: fa (PRIMARY KEY) and fb (UNIQUE KEY) -drop table if exists f1; CREATE TABLE f1(fa INT PRIMARY KEY, fb INT UNIQUE KEY); -- Create table c1 with two integer columns: ca and cb -drop table if exists c1; CREATE TABLE c1 (ca INT, cb INT); -- Add a FOREIGN KEY constraint named ffa to c1, linking column ca to f1.fa ALTER TABLE c1 ADD CONSTRAINT ffa FOREIGN KEY (ca) REFERENCES f1(fa); @@ -122,7 +120,6 @@ mysql> select ca, cb from c1 order by ca; ```sql -- Create table t1 with columns a (INTEGER), b (CHAR(10)), c (DATE), d (DECIMAL(7,2)), and a UNIQUE KEY on (a, b) -drop table if exists t1; CREATE TABLE t1(a INTEGER, b CHAR(10), c DATE, d DECIMAL(7,2), UNIQUE KEY(a, b)); -- View the structure of t1 diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md index c9a730815a..6e203e654c 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md @@ -202,7 +202,6 @@ The following is an example to illustrate the association of parent and child ta First, create a parent table with field a as the primary key: ```sql -drop table if exists t1; create table t1(a int primary key,b varchar(5)); insert into t1 values(101,'abc'),(102,'def'); mysql> select * from t1; @@ -218,7 +217,6 @@ mysql> select * from t1; Then create a child table with field c as the foreign key, associated with parent table field a: ```sql -drop table if exists t2; create table t2(a int ,b varchar(5),c int, foreign key(c) references t1(a)); insert into t2 values(1,'zs1',101),(2,'zs2',102); insert into t2 values(3,'xyz',null); @@ -230,7 +228,7 @@ mysql> select * from t2; | 2 | zs2 | 102 | | 3 | xyz | NULL | +------+------+------+ -3 rows in set (0.00 sec) +2 rows in set (0.00 sec) ``` In addition, `[ON DELETE reference_option]` and `[ON UPDATE reference_option]` are used when defining a foreign key relationship to specify actions to be taken when records in the parent table are deleted or updated. These two parameters are primarily used to maintain data integrity and consistency: @@ -252,13 +250,11 @@ See the example below: Suppose there are two tables, `Orders` and `Customers`, where the `Orders` table has a foreign key column `customer_id` referencing the `id` column in the `Customers` table. If, when a customer is deleted from the `Customers` table, you also want to delete the associated order data, you can use `ON DELETE CASCADE`. ```sql -drop table if exists Customers; CREATE TABLE Customers ( id INT PRIMARY KEY, name VARCHAR(50) ); -drop table if exists Orders; CREATE TABLE Orders ( id INT PRIMARY KEY, order_number VARCHAR(10), @@ -368,7 +364,6 @@ mysql> SELECT * FROM test; - Example 2: Add comments when creating a table ```sql -drop table if exists t2; create table t2 (a int, b int) comment = "fact table"; mysql> show create table t2; @@ -376,16 +371,15 @@ mysql> show create table t2; | Table | Create Table | +-------+---------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( -`a` INT DEFAULT NULL, -`b` INT DEFAULT NULL -) COMMENT='fact table', | + `a` int DEFAULT NULL, + `b` int DEFAULT NULL +) COMMENT='fact table' | +-------+---------------------------------------------------------------------------------------+ ``` - Example 3: Add comments to columns when creating tables ```sql -drop table if exists t3; create table t3 (a int comment 'Column comment', b int) comment = "table"; mysql> SHOW CREATE TABLE t3; @@ -393,9 +387,9 @@ mysql> SHOW CREATE TABLE t3; | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------+ | t3 | CREATE TABLE `t3` ( -`a` INT DEFAULT NULL COMMENT 'Column comment', -`b` INT DEFAULT NULL -) COMMENT='table', | + `a` int DEFAULT NULL COMMENT 'Column comment', + `b` int DEFAULT NULL +) COMMENT='table' | +-------+----------------------------------------------------------------------------------------------------------+ ``` From 015ccbf96cd6e3f1bf5aaceac487b3db702c7ab9 Mon Sep 17 00:00:00 2001 From: iamlinjunhong <1030420200@qq.com> Date: Wed, 7 Jan 2026 16:09:53 +0800 Subject: [PATCH 6/6] fix --- .../Data-Definition-Language/alter-table.md | 10 ++- .../Data-Definition-Language/create-table.md | 90 +++++++++++-------- 2 files changed, 63 insertions(+), 37 deletions(-) diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md index d16a168077..36df0d5e4a 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-table.md @@ -82,15 +82,16 @@ Below are explanations for each parameter: ```sql -- Create table f1 with two integer columns: fa (PRIMARY KEY) and fb (UNIQUE KEY) +drop table if exists c1; +drop table if exists f1; CREATE TABLE f1(fa INT PRIMARY KEY, fb INT UNIQUE KEY); -- Create table c1 with two integer columns: ca and cb +drop table if exists c1; CREATE TABLE c1 (ca INT, cb INT); -- Add a FOREIGN KEY constraint named ffa to c1, linking column ca to f1.fa ALTER TABLE c1 ADD CONSTRAINT ffa FOREIGN KEY (ca) REFERENCES f1(fa); -- Insert a record into f1: (2, 2) INSERT INTO f1 VALUES (2, 2); --- Insert a record into c1: (1, 1) -INSERT INTO c1 VALUES (1, 1); -- Insert a record into c1: (2, 2) INSERT INTO c1 VALUES (2, 2); -- Select all records from c1, ordered by ca @@ -120,6 +121,7 @@ mysql> select ca, cb from c1 order by ca; ```sql -- Create table t1 with columns a (INTEGER), b (CHAR(10)), c (DATE), d (DECIMAL(7,2)), and a UNIQUE KEY on (a, b) +drop table if exists t1; CREATE TABLE t1(a INTEGER, b CHAR(10), c DATE, d DECIMAL(7,2), UNIQUE KEY(a, b)); -- View the structure of t1 @@ -181,6 +183,8 @@ mysql> select * from t1; - Example 3: Renaming a Column ```sql +drop table if exists t2; +drop table if exists t1; CREATE TABLE t1 (a INTEGER PRIMARY KEY, b CHAR(10)); mysql> desc t1; +-------+----------+------+------+---------+-------+---------+ @@ -232,6 +236,8 @@ mysql> select * from t1; - Example 4: Renaming a Table ```sql +drop table if exists t2; +drop table if exists t1; CREATE TABLE t1 (a INTEGER PRIMARY KEY, b CHAR(10)); mysql> show tables; diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md index 6e203e654c..f6bae18507 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-table.md @@ -144,6 +144,7 @@ For example, to create a table and define an auto-increment column with a starti ```sql -- set up +drop table if exists t1; create table t1(a int auto_increment primary key) auto_increment = 10; ``` @@ -202,6 +203,8 @@ The following is an example to illustrate the association of parent and child ta First, create a parent table with field a as the primary key: ```sql +drop table if exists t2; +drop table if exists t1; create table t1(a int primary key,b varchar(5)); insert into t1 values(101,'abc'),(102,'def'); mysql> select * from t1; @@ -250,11 +253,13 @@ See the example below: Suppose there are two tables, `Orders` and `Customers`, where the `Orders` table has a foreign key column `customer_id` referencing the `id` column in the `Customers` table. If, when a customer is deleted from the `Customers` table, you also want to delete the associated order data, you can use `ON DELETE CASCADE`. ```sql +drop table if exists Customers; CREATE TABLE Customers ( id INT PRIMARY KEY, name VARCHAR(50) ); +drop table if exists Orders; CREATE TABLE Orders ( id INT PRIMARY KEY, order_number VARCHAR(10), @@ -350,6 +355,7 @@ The number of partitions may optionally be specified with a PARTITIONS num claus - Example 1: Create a common table ```sql +drop table if exists test; CREATE TABLE test(a int, b varchar(10)); INSERT INTO test values(123, 'abc'); @@ -364,6 +370,7 @@ mysql> SELECT * FROM test; - Example 2: Add comments when creating a table ```sql +drop table if exists t2; create table t2 (a int, b int) comment = "fact table"; mysql> show create table t2; @@ -380,6 +387,7 @@ mysql> show create table t2; - Example 3: Add comments to columns when creating tables ```sql +drop table if exists t3; create table t3 (a int comment 'Column comment', b int) comment = "table"; mysql> SHOW CREATE TABLE t3; @@ -396,6 +404,7 @@ mysql> SHOW CREATE TABLE t3; - Example 4: Create a common partitioned table ```sql +drop table if exists tp1; CREATE TABLE tp1 (col1 INT, col2 CHAR(5), col3 DATE) PARTITION BY KEY(col3) PARTITIONS 4; mysql> SHOW CREATE TABLE tp1; @@ -403,14 +412,15 @@ mysql> SHOW CREATE TABLE tp1; | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | tp1 | CREATE TABLE `tp1` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL, -`col3` DATE DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL, + `col3` date DEFAULT NULL ) partition by key algorithm = 2 (col3) partitions 4 | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) -- do not specify the number of partitions +drop table if exists tp2; CREATE TABLE tp2 (col1 INT, col2 CHAR(5), col3 DATE) PARTITION BY KEY(col3); mysql> SHOW CREATE TABLE tp2; @@ -418,14 +428,15 @@ mysql> SHOW CREATE TABLE tp2; | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------------------------------+ | tp2 | CREATE TABLE `tp2` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL, -`col3` DATE DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL, + `col3` date DEFAULT NULL ) partition by key algorithm = 2 (col3) | +-------+---------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) -- Specify partition algorithm +drop table if exists tp3; CREATE TABLE tp3 ( col1 INT, @@ -439,14 +450,15 @@ mysql> show create table tp3; | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------------------------------+ | tp3 | CREATE TABLE `tp3` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL, -`col3` DATE DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL, + `col3` date DEFAULT NULL ) partition by key algorithm = 1 (col3) | +-------+---------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) -- Specify partition algorithm and the number of partitions +drop table if exists tp4; CREATE TABLE tp4 (col1 INT, col2 CHAR(5), col3 DATE) PARTITION BY LINEAR KEY ALGORITHM = 1 (col3) PARTITIONS 5; mysql> SHOW CREATE TABLE tp4; @@ -454,14 +466,15 @@ mysql> SHOW CREATE TABLE tp4; | Table | Create Table | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | tp4 | CREATE TABLE `tp4` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL, -`col3` DATE DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL, + `col3` date DEFAULT NULL ) partition by linear key algorithm = 1 (col3) partitions 5 | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) -- Multi-column partition +drop table if exists tp5; CREATE TABLE tp5 ( col1 INT, @@ -474,14 +487,15 @@ mysql> SHOW CREATE TABLE tp5; | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | tp5 | CREATE TABLE `tp5` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL, -`col3` DATE DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL, + `col3` date DEFAULT NULL ) partition by key algorithm = 2 (col1, col2) partitions 4 | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) -- Create a primary key column partition +drop table if exists tp6; CREATE TABLE tp6 ( col1 INT NOT NULL PRIMARY KEY, @@ -495,16 +509,17 @@ mysql> SHOW CREATE TABLE tp6; | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | tp6 | CREATE TABLE `tp6` ( -`col1` INT NOT NULL, -`col2` DATE NOT NULL, -`col3` INT NOT NULL, -`col4` INT NOT NULL, -PRIMARY KEY (`col1`) + `col1` int NOT NULL, + `col2` date NOT NULL, + `col3` int NOT NULL, + `col4` int NOT NULL, + PRIMARY KEY (`col1`) ) partition by key algorithm = 2 (col1) partitions 4 | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) -- Create HASH partition +drop table if exists tp7; CREATE TABLE tp7 ( col1 INT, @@ -516,13 +531,14 @@ mysql> SHOW CREATE TABLE tp7; | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------+ | tp7 | CREATE TABLE `tp7` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL ) partition by hash (col1) | +-------+------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) -- Specifies the number of HASH partitions when creating hash partition +drop table if exists tp8; CREATE TABLE tp8 ( col1 INT, @@ -534,13 +550,14 @@ mysql> SHOW CREATE TABLE tp8; | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------+ | tp8 | CREATE TABLE `tp8` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL ) partition by hash (col1) partitions 4 | +-------+-------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) -- specify the partition granularity when creating a partition +drop table if exists tp9; CREATE TABLE tp9 ( col1 INT, @@ -553,14 +570,15 @@ mysql> SHOW CREATE TABLE tp9; | Table | Create Table | +-------+------------------------------------------------------------------------------------------------------------------------------------------+ | tp9 | CREATE TABLE `tp9` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL, -`col3` DATETIME DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL, + `col3` datetime DEFAULT NULL ) partition by hash (year(col3)) | +-------+------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) -- specify the partition granularity and number of partitions when creating a partition +drop table if exists tp10; CREATE TABLE tp10 ( col1 INT, @@ -573,14 +591,15 @@ mysql> SHOW CREATE TABLE tp10; | Table | Create Table | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+ | tp10 | CREATE TABLE `tp10` ( -`col1` INT DEFAULT NULL, -`col2` CHAR(5) DEFAULT NULL, -`col3` DATE DEFAULT NULL + `col1` int DEFAULT NULL, + `col2` char(5) DEFAULT NULL, + `col3` date DEFAULT NULL ) partition by linear hash (year(col3)) partitions 6 | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) -- Use the primary key column as the HASH partition when creating a partition +drop table if exists tp12; CREATE TABLE tp12 (col1 INT NOT NULL PRIMARY KEY, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL) PARTITION BY HASH(col1) PARTITIONS 4; mysql> SHOW CREATE TABLE tp12; @@ -588,11 +607,11 @@ mysql> SHOW CREATE TABLE tp12; | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | tp12 | CREATE TABLE `tp12` ( -`col1` INT NOT NULL, -`col2` DATE NOT NULL, -`col3` INT NOT NULL, -`col4` INT NOT NULL, -PRIMARY KEY (`col1`) + `col1` int NOT NULL, + `col2` date NOT NULL, + `col3` int NOT NULL, + `col4` int NOT NULL, + PRIMARY KEY (`col1`) ) partition by hash (col1) partitions 4 | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) @@ -601,6 +620,7 @@ PRIMARY KEY (`col1`) - Example 5: Primary key auto increment ```sql +drop table if exists t2; drop table if exists t1; create table t1(a bigint primary key auto_increment, b varchar(10));