Does MySQL support defining a table inside a session without appending it to the database?
-
13-03-2021 - |
Question
As per stated in the title. Does MySQL support defining locally a table? (as a 'local variable' perhaps?)
I understand (according to 'Source') that MySQL has 'two types of variables': (1) local and (2) user variables, nevertheless neither can be a whole table.
Source:
EDIT: I think that using the 'temporary' flag in 'create' is the answer
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
from the docs: https://dev.mysql.com/doc/refman/8.0/en/create-table.html
Solution
You are right it is a like a local table to the connection.
According to the link you have under the subheading Temporary Tables
, it says:
A TEMPORARY table is visible only within the current session, and is dropped automatically when the session is closed
Simply put, once the DB Connection terminates (normally or abnormally), the table disappears.
Also, please be aware that only the DB Connection that created the temporary table can access it. If 10 different DB Connections perform CREATE TEMPORARY TABLE
, each DB Connection would have its own table. No other DB Connection can access it.