036. TiDB feature_temporary table

global temporary table

CREATE GLOBAL TEMPORARY TABLE;
  • Table definitions for global temporary tables are persistent and visible to all sessions
  • The data of the global temporary table is only visible in the current transaction
  • When the transaction ends, the data is automatically cleared
  • A global temporary table cannot have the same name as a permanent table
  • present in information_schema.tables

# commit delete rows 一定带上这个才能创建
tidb> create global temporary table test.temp2 (id bigint) on
commit delete rows;
Query OK, 0 rows affected (0.57 sec)

# 开启事务,关闭自动提交
tidb> set @@autocommit = 'OFF';
tidb> insert into test.temp2 values (10);
tidb> select * from test.temp2;
+------+
| id |
+------+
| 10 |
+------+
1 row in set (0.00 sec)
tidb> commit;

# 事务完成后,这张表虽然存在,但里面的数据清掉
tidb> select * from test.temp2;
Empty set (0.00 sec)

Users can create a global temporary table through the CREATE GLOBAL TEMPORARY TABLE statement, and add ON COMMIT DELETE ROWS at the end of the statement. A global temporary table can be dropped with the DROP TABLE or DROP GLOBAL TEMPORARY TABLE statement.

local temporary table

  • CREATE TEMPORARY TABLE:

    • Exists only during a client session
    • Visible only to the session that created it
    • Override permanent table definition
    • will not appear in information_schema.tables
  • The semantics of a local temporary table is consistent with that of a MySQL temporary table, and it has the following characteristics:

    • The table definition of the local temporary table is not persistent, it is only visible in the session that created the table, and other sessions cannot access the local temporary table
    • Different sessions can create local temporary tables with the same name, and each session will only read and write the local temporary tables created in the session
    • The data of the local temporary table is visible to all transactions within the session
    • After the session ends, the local temporary table created by the session will be automatically deleted
    • A local temporary table can have the same name as an ordinary table, and at this time, in DDL and DML statements, the ordinary table is hidden until the local temporary table is deleted

• Users can create a local temporary table through the CREATE TEMPORARY TABLE statement, and delete the local temporary table through the DROP TABLE or DROP TEMPORARY TABLE statement.

tidb> create table test.temp1 (id int);
Query OK, 0 rows affected (0.51 sec)

tidb> create temporary table test.temp1 (name char(10));
Query OK, 0 rows affected (0.00 sec)

tidb> desc test.temp1;
+-------+----------+------+------+---------+-------+
| Field | Type     | Null | Key  | Default | Extra |
+-------+----------+------+------+---------+-------+
| name  | char(10) | YES  |      | NULL    |       |
+-------+----------+------+------+---------+-------+
1 row in set (0.00 sec)

tidb> drop table test.temp1;
Query OK, 0 rows affected (0.00 sec)

tidb> desc test.temp1;
+-------+---------+------+------+---------+-------+
| Field | Type    | Null | Key  | Default | Extra |
+-------+---------+------+------+---------+-------+
| id    | int(11) | YES  |      | NULL    |       |
+-------+---------+------+------+---------+-------+
1 row in set (0.00 sec)

Guess you like

Origin blog.csdn.net/wangzhicheng987/article/details/130776850