A very important feature in any database is the so called temporary tables. With them, you can create a table apart of the others in the database – so when you finalize your connection, then it is automatically deleted.
Widely used by DBAs and developers on several occasions, a practical example of using this feature is when it is necessary to insert multiple records at once in a table from the SELECT command. These data often need to be treated before being effectively stored and therefore a temporary table is used. The manipulation from them is more rapid and really works.
A temporary table is almost equal to a normal table, taking up disk space and memory while the session is open. Therefore, it should be used very carefully, since each open session, a new table is created.
In procedures, the temporary table must be used with caution and should be evaluated the possibility of using other features like views and transactions.
Temporary tables in MySQL are created using the following syntax:
CREATE TEMPORARY TABLE [Table Name]
To illustrate, we will create a basic product table:
CREATE TEMPORARY TABLE tbl_Product_Temp
cd_Prod INTEGER NOT NULL,
Name VARCHAR(100) NOT NULL
As with a normal table, the SELECT, INSERT, UPDATE, and DELETE commands will work exactly the same for temporary tables.
SELECT * FROM tbl_Product_Temp