(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.
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:
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 INSERT
, UPDATE
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:
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
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
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.