Monday, 16 November 2020

SQL: Temporary table

 

Most of the RDBMS systems has the capability to define temporary table.

 

How long the data in temporary table is available?

In general, temporary table is dropped when the transaction completes or when the database session is closed.

 

How to create temporary table?

Using 'CREATE TEMPORARY TABLE' statement, you can create a temporary table.

 

CREATE TEMPORARY TABLE log_messages(

         id int AUTO_INCREMENT,

         message text,

         PRIMARY KEY(id)

)

 

Insert data into temporary table

INSERT INTO log_messages(id, message) VALUES (1, "Started execution");

INSERT INTO log_messages(id, message) VALUES (2, "Execution in process");

INSERT INTO log_messages(id, message) VALUES (3, "Done with the execution");

 

Get the data from log_messages

mysql> SELECT * FROM log_messages;
+----+-------------------------+
| id | message                 |
+----+-------------------------+
|  1 | Started execution       |
|  2 | Execution in process    |
|  3 | Done with the execution |
+----+-------------------------+
3 rows in set (0.00 sec)

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment