Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
+-------+----------+------+------+---------+-------+---------+
Expand Down Expand Up @@ -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;
Expand All @@ -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`.
Loading
Loading