How MariaDB administrates temporary tables?

余生颓废 提交于 2020-03-05 00:27:29

问题


I would like to understand how MariaDB administrates temporary tables. For example, how MariaDB cope with a temporary and non-temporary table if they have both the same name.

In the example below I created a temporary and non-temporary table with the same name (step A) and did an update of the table. Now, which one was updated (B)?

When I drop the non-temporary table (C1) the table has no content but still exists (C1). Only after a second dropping table will be dropped.

The same happens if I drop the temporary table (C2).

It seems like that if there are a temporary and a non-temporary table DROP TEMPORARY TABLE x0 and DROP TABLE x0 deletes the content of the table but the table still exists. After a second dropping of the non-temporary table the table is dropped.

But if I drop twice the temporary table (C3) the second DROPrealizes that there is no temporary table.

Obviously, in the presence of a temporary table dropping the non-temporary table does only delete the table (and not dropping). After the second dropping the table is dropped.

Is there a way to rationale this behaviour? The Tutorial gives some hints:

Note − Temporary tables are permitted to have the same name as an existing non-temporary table because MariaDB views it as a difference reference.

But this does not confirm what is shown below.

-- ****************************************************
-- (A) Create table
-- ****************************************************
DROP TABLE IF EXISTS x0;
DROP TEMPORARY TABLE IF EXISTS x0;

CREATE TABLE x0 (
  id     INTEGER
, v      FLOAT
);

CREATE TEMPORARY TABLE x0 (
  id     INTEGER
, v      FLOAT
);


INSERT INTO x0 VALUES
  (1,1)
, (2,1)
;

SELECT * FROM x0;

-- ****************************************************
-- (B) Update
-- ****************************************************

UPDATE x0 SET v = 2 WHERE id = 1;
SELECT * FROM x0;

/*
+----+---+
| id | v |
+----+---+
|  1 | 2 |
|  2 | 1 |
+----+---+
*/

-- ****************************************************
-- (C1) Dropping non-temporary table (A->B->C1)
-- ****************************************************
DROP TABLE x0;
SELECT * FROM x0; 
/*
+----+---+
| id | v |
+----+---+
*/


-- ****************************************************
-- (C2) Dropping temporary table (A->B->C2)
-- ****************************************************    
DROP TEMPORARY TABLE x0;
SELECT * FROM x0;
/*
+----+---+
| id | v |
+----+---+
*/

DROP TABLE x0;
SELECT * FROM x0;    
/* SQL Fehler (1146): Table 'test0.x0' doesn't exist */

-- ****************************************************
-- (C3) Dropping temporary table (A->B->C3)
-- ****************************************************    
DROP TEMPORARY TABLE x0;
SELECT * FROM x0;
/*
+----+---+
| id | v |
+----+---+
*/

DROP TEMPORARY TABLE x0;
/* SQL Fehler (1051): Unknown table 'test0.x0' */
SELECT * FROM x0;    
/*
+----+---+
| id | v |
+----+---+
*/

DROP TABLE x0;    
SELECT * FROM x0;
/* SQL Fehler (1146): Table 'test0.x0' doesn't exist */

回答1:


Thanks to Georg Richter who answered a similar question here I can explain what happened. The crucial sentence of his answer is:

In case a temporary table has the same name as an existing non temporary table the temporary table will shadow the name of a non temporary table.

So, as long a temporary table exists, all select, insert, update will be performed on the temporary table. Even drop table will drop first the temporary table whereas drop temporary table drops only temporary tables and gives an error when there is no temporary table.

All this explains what happened with my script. When I made the insert into the table after creating persistent and temporary table only temporary table got the records. After first dropping, the temporary table was dropped and select shows the empty persistent table.



来源:https://stackoverflow.com/questions/59372342/how-mariadb-administrates-temporary-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!