diff --git a/docs/MatrixOne/Develop/schema-design/create-temporary-table.md b/docs/MatrixOne/Develop/schema-design/create-temporary-table.md index e275ec4c72..21b933fccf 100644 --- a/docs/MatrixOne/Develop/schema-design/create-temporary-table.md +++ b/docs/MatrixOne/Develop/schema-design/create-temporary-table.md @@ -36,15 +36,16 @@ 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); ``` -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..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; @@ -257,5 +263,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..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 @@ -114,11 +114,15 @@ 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: -The creating session can perform any operation on the table, such as `DROP TABLE`, `INSERT`, `UPDATE`, or `SELECT`. +- **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. + +For more information, see [CREATE TEMPORARY TABLE](create-temporary-table.md). #### COMMENT @@ -140,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; ``` @@ -198,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; @@ -224,7 +231,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: @@ -246,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), @@ -346,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'); @@ -360,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; @@ -367,15 +378,16 @@ 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; @@ -383,15 +395,16 @@ 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' | +-------+----------------------------------------------------------------------------------------------------------+ ``` - 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; @@ -399,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; @@ -414,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, @@ -435,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; @@ -450,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, @@ -470,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, @@ -491,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, @@ -512,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, @@ -530,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, @@ -549,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, @@ -569,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; @@ -584,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) @@ -597,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)); 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..fd637f791b --- /dev/null +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-temporary-table.md @@ -0,0 +1,87 @@ +# **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_t_unique_1 (a INT, b VARCHAR(10)); +INSERT INTO temp_t_unique_1 VALUES (1, 'abc'); + +mysql> SELECT * FROM temp_t_unique_1; ++------+------+ +| a | b | ++------+------+ +| 1 | abc | ++------+------+ + +-- The table will be automatically dropped when the session ends. +``` + +- **Example 2: Shadowing a regular table** + +Note: The following behavior (shadowing) depends on the session-level mapping. + +``` +-- Regular table +CREATE TABLE t1_shadow_test (a INT); +INSERT INTO t1_shadow_test VALUES (1); + +-- Temporary table with same name +CREATE TEMPORARY TABLE t1_shadow_test (b INT); +INSERT INTO t1_shadow_test VALUES (2); + +mysql> SELECT * FROM t1_shadow_test; ++------+ +| b | ++------+ +| 2 | ++------+ +-- The temporary table shadows the regular table. + +mysql> DESC t1_shadow_test; ++-------+---------+------+------+---------+-------+---------+ +| 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