Home » SQL

Temporary tables in SQL



In this article, we are going to learn about the temporary tables and the process to use it to store data temporarily.
Submitted by Manu Jemini, on March 05, 2018

Whenever you need to have a temporary table from your original table all you need to do is to create a temporary table with your desired fields. In the example below, we have used this operation on our dummy data.

Now the first thing we need to do is to, find what we will need in our temporary table then execute a SQL query telling database to create a similar temporary table.

This is very useful when we need a temporary storage space in which we can store our data and can also retrieve it whenever we want to.

To use this kind ability first we need to tell the database to create a temporary table and then it will behave like a real table. When we have our temporary table ready we will insert data into it. Inserting data into it is same as of normal tables.

This data that we have here can be used later as we will need it later on. There are moments when we want to store data in a particular data but we also need other data. At this point we will need to have a storage space where we can store it for some time. This is like saving your data in a variable and then using it later.

Dummy data:

SQL Temporary tables example

Syntax:

CREATE TEMPORARY TABLE OUREMPLOYEES (
	employee_name VARCHAR(50) NOT NULL, 
	address DECIMAL(name VARCHAR(50) NOT NULL, 
	total_salary INT UNSIGNED NOT NULL DEFAULT 0 
);
SQL Temporary tables Output 1

Example 1:

INSERT INTO OUREMPLOYEES (employee_name, address, total_salary) 
VALUES 
(Manu, Gwalior, 500000);
SQL Temporary tables Output 2

Example 2:

SELECT * FROM SALESSUMMARY;
SQL Temporary tables Output 3






Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.