MySQL has a feature to create a special table called a Temporary Table.
Temporary Table allows us to keep temporary data
We can reuse this table several times in a particular session
A temporary table provides a very useful and flexible feature that allows us to achieve complex tasks quickly, such as when we query data that requires a single SELECT statement with JOIN clause
CREATE TEMPORARY TABLE table_name ( column_1, column_2, ..., table_constraints );
Let us understand how we can create a temporary table in MySQL.
CREATE TEMPORARY TABLE StudentsTempo(ROLL_NO int(3) NOT NULL, SUBJECT varchar(20) NOT NULL, PRIMARY KEY(ROLL_NO));
The empty "StudentsTempo" table will now look like this:
Next, we need to insert values in the temporary table:
INSERT INTO StudentsTempo(ROLL_NO,SUBJECT) VALUES (12,'MySQL'), (34,'SQL'); SELECT * FROM StudentsTempo;
Now, after executing the above MySQL statement,
MySQL allows us to remove the temporary table using the DROP TABLE statement
DROP TEMPORARY TABLE table_name;