close[x]


MySQL

MySQL-Home MySQL-Environment setup MySQL- Workbench MySQL-Basic syntax MySQL-Operator MySQL-Data type MySQL-Comments MySQL-Create DB MySQL-Drop DB MySQL-Select DB MySQL-Create Table MySQL-Drop table MySQL-Truncate MySQL-Primary Key MySQL-Foreign Key MySQL-Null MySQL-Increment MySQL-Having MySQL-Top MySQL-Insert Statement MySQL-Select Statement MySQL-Alter Statement MySQL-Where MySQL-And & Or MySQL-Default values MySQL-Exists MySQL-Order by MySQL-View MySQL-Update Statement MySQL-Delete Statement MySQL-Like MySQL-Sort MySQL-Limit MySQL-Min MySQL-Max MySQL-Group MySQL-In MySQL-Between MySQL-Union MySQL-Count MySQL-Average MySQL-Sum MySQL-Date & Time MySQL-Import MySQL-Export MySQL-Index MySQL-Temporary MySQL-Join MySQL-Full Join MySQL-Inner Join MySQL-Left Join MySQL-Right Join MySQL-Store Procedure MySQL-Injection MySQL-PHP connection



learncodehere.com




MySQL - Temporary Table

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


Syntax : Creating Temporary Table


CREATE TEMPORARY TABLE table_name (  
column_1, column_2, ..., table_constraints  
);  

Let us understand how we can create a temporary table in MySQL.


Example : Creating Temporary Table


 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:

mysql temporary table


Next, we need to insert values in the temporary table:


Example : Insert and Select Temporary Table Data


 INSERT INTO StudentsTempo(ROLL_NO,SUBJECT)
 VALUES (12,'MySQL'), (34,'SQL');             
 SELECT * FROM StudentsTempo;          
  

Now, after executing the above MySQL statement,

Table : StudentsTempo

ROLL_NO SUBJECT
12 MySQL
34 SQL

Drop Temporary Table

MySQL allows us to remove the temporary table using the DROP TABLE statement


Syntax : Drop Temporary Table


DROP TEMPORARY TABLE table_name;