Beginners Guide to SQL – SQL Temporary Tables

(SQL Tutorials for Citizen Data Scientist)

SQL Temporary Tables

In this tutorial you will learn how to create temporary tables using SQL.

Creating Temporary Tables

A temporary table is a table that is visible only to the current session, and is dropped automatically when the session in which it was created is closed.

Since temporary tables are not stored in the database on a permanent basis, therefore, it would be useful in a situation where you need a table only for a short time to perform or test something, after which you want it to disappear automatically.

Syntax

The CREATE TEMPORARY TABLE statement is used to create a temporary table.

CREATE TEMPORARY TABLE table_name (column definitions);

If you want to create a temporary table from scratch, you can use the TEMPORARY keyword when creating a table, i.e. use CREATE TEMPORARY TABLE instead of CREATE TABLE statement. See the create table chapter for complete syntax and examples.


Creating a Temporary Copy of an Existing Table

Temporary tables can be useful in situations when you just want to test the SQL queries without affecting the database. Let’s create a temporary copy of an existing table in MySQL database.

Type the following command at the MySQL command prompt and press enter:

mysql> CREATE TEMPORARY TABLE persons SELECT * FROM persons;

The above statement creates a temporary table named persons on the fly from the result set of an existing base table persons. Also, since it is a temporary copy of the persons table, therefore you can perform any operation like INSERTUPDATE or DELETE without worrying about affecting the original persons base table by mistake.

Tip: A temporary table can have the same name as a permanent base table. If you specify the name of a temporary table same as the existing base table, then the permanent base table is hidden until the temporary table is dropped.

Note: Since temporary tables are session-specific, so two different sessions can use the same temporary table name without conflicting with each other.


Dropping Temporary Tables

Temporary tables are dropped automatically when the database connection or session in which they are created is closed. However, if want to delete them without closing the current session, you can use the DROP TEMPORARY TABLE statement, as follow:

mysql> DROP TEMPORARY TABLE persons;

The above statement will delete the temporary table persons from the database. After that, the original persons base table becomes visible.

 

MySQL Tutorials for Business Analyst: MySQL Create Database, Tables, Data Types

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!