TEMPORARY TABLE temporary table in SQL
TEMPORARY TABLE, also known as a temporary table, is a special form of table that is temporarily stored and handles intermediate results on SQL using the same options as selecting, updating, and matching as you can use. with typical tables in SQL Server.
However, you should note that Temporary Table can be deleted when the current Client Session ends.
Temporary tables are available from MySQL version 3.23 onwards. If you use an older version, you cannot use this feature, you can use the Heap Table instead.
As mentioned, the temporary table will only exist when the session is available. If you run the code in PHP script, the temporary table will be canceled automatically when the script finishes executing. If you are connected to MySQL Database through the MySQL Client program, the temporary table will exist until you close the Client or cancel the table.
The example illustrates how to use a temporary table
mysql> CREATE TEMPORARY TABLE DOANHTHU (
-> ten_sanpham VARCHAR(50) NOT NULL
-> , tong_doanhthu DECIMAL(12,2) NOT NULL DEFAULT 0.00
-> , gia_trungbinh DECIMAL(7,2) NOT NULL DEFAULT 0.00
-> , tong_soluong INT UNSIGNED NOT NULL DEFAULT 0 );
Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO DOANHTHU -> (ten_sanpham, tong_doanhthu, gia_trungbinh, tong_soluong) -> VALUES -> ('galaxys10', 100.25, 90, 2); mysql> SELECT * FROM DOANHTHU; +--------------+---------------+----------------+------------------+ | ten_sanpham | tong_doanhthu | gia_trungbinh | tong_soluong | +--------------+---------------+----------------+------------------+ | galaxys10 | 100.25| 90.00 | 2 | +--------------+---------------+----------------+------------------+ 1 row in set (0.00 sec)
When you execute the SHOW TABLES command, your temp table will not be listed. Now, if you log out of the MySQL session and then execute a SELECT command, you will not find the data in the database and then your temporary table will no longer exist.
Delete temporary tables in SQL
By default, all temporary tables in MySQL will be deleted when your database connection ends. However, if you still want to delete them without disconnecting, you can use the DROP TABLE command.
The following is an example of deleting a temporary table.
mysql> CREATE TEMPORARY TABLE DOANHTHU (
-> ten_sanpham VARCHAR(50) NOT NULL
-> , tong_doanhthu DECIMAL(12,2) NOT NULL DEFAULT 0.00
-> , gia_trungbinh DECIMAL(7,2) NOT NULL DEFAULT 0.00
-> , tong_soluong INT UNSIGNED NOT NULL DEFAULT 0 );
Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO DOANHTHU -> (ten_sanpham, tong_doanhthu, gia_trungbinh, tong_soluong) -> VALUES -> ('galaxys10', 100.25, 90, 2); mysql> SELECT * FROM DOANHTHU; +--------------+---------------+----------------+------------------+ | ten_sanpham | tong_doanhthu | gia_trungbinh | tong_soluong | +--------------+---------------+----------------+------------------+ | galaxys10 | 100.25| 90.00 | 2 | +--------------+---------------+----------------+------------------+ 1 row in set (0.00 sec) mysql> DROP TABLE DOANHTHU; mysql> SELECT * FROM DOANHTHU; ERROR 1146: Table 'QTM.DOANHTHU' doesn't exist
In the next section, we will learn about the Clone Table in SQL , so keep in mind.
Previous article: Function handling DATE / TIME in SQL - Part 2
Next article: CLONE TABLE in SQL
You should read it
- Create a temporary name using ALIAS in SQL
- What is Temporary File? How to delete temporary files on windows computers
- Steps to temporarily view Excel files when working together
- CLONE TABLE in SQL
- Install frames and temporary images on Facebook
- Free up space on Windows 10 by deleting temporary files
- Steps to remove temporary files stored on Kaspersky
- 35 tools, scripts and plugins to build HTML Table
May be interested
- Function handles DATE / TIME in SQL - Part 2this article will show you in detail how to use all functions to handle date / time in sql with syntax and specific examples to make it easier to visualize and capture functions.
- CLONE TABLE in SQLthis article will show you in detail how to use clone table in sql with a specific example to make it easier to visualize and capture.
- Handling copy - HANDLING DUPLICATE in SQLthis article will show you in detail how to handle copy - handling duplicate with specific examples to make it easier to visualize and capture.
- Subquery - SUBQUERY in SQLsubquery is a query inside another sql query and is embedded in the where clause.
- SEQUENCE in SQLsequence is often used because meeting the requirements of many applications is to require each row in a table to contain a unique value similar to the primary key.
- Integrated terminal in SQL Operations Studio (preview)in sql operations studio (preview), you can open an integrated terminal, starting from the root of the current workspace.