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

5 ★ | 1 Vote

May be interested

  • Should a table drill be used?Should a table drill be used?
    table drilling machines provide the most accurate drilling holes, meeting the demand for industrial and continuous drilling on wood and metal.
  • Steps to remove temporary files stored on KasperskySteps to remove temporary files stored on Kaspersky
    like other programs and antivirus applications, in the process of using kaspersky internet security software, kaspersky will store temporary files in a folder hidden deep in the system drive.
  • How to use the temporary permission feature of Android 11 on any phone?How to use the temporary permission feature of Android 11 on any phone?
    not everyone uses pixel phones. that means you are not likely to experience the latest android 11 features. the following article will tell you how to get temporary, one-time permissions on android 11 without rooting.
  • How to Get a Temporary Phone Number in the USHow to Get a Temporary Phone Number in the US
    a temporary phone number is a good way to protect privacy in situations such as a 'blind date' (a date between two strangers) or a meeting set up on an online classified site. you can use temporary phone numbers through applications that connect using mobile minutes or simple wifi networks. prepaid cell phones and sim cards are also simple options for getting a temporary phone number. skype and google voice are popular options for getting an additional phone number for short-term use (or you can keep it long-term if you want).
  • How to Rid Your Computer of Temporary FilesHow to Rid Your Computer of Temporary Files
    temporary files are created as safeguards against errors, accidental shut-downs, and other interruptions amidst running programs. while these files help you quickly recover your information, they also take up random access memory (ram),...
  • Table operations in WordTable operations in Word
    introduce table operations in word. table operations in word 2013 include: 1. draw cells in a table. - creating a table can not ignore merge cell operation, for example, if you want the column of the school day to have 2 values ​​of even and odd dates, make 2 columns into one.
  • How to fix table errors in Word overflowHow to fix table errors in Word overflow
    error word overflow table can be corrected in 2 different ways and very simple to perform, helping to bring the table in word aligned correctly.
  • How to Clear Temp Files in Windows 10How to Clear Temp Files in Windows 10
    windows 10 creates temporary files while you use certain apps (such as microsoft word) to improve your experience. however, your pc can create hundreds of temporary files in a day, which may fill up your hard drive. if you have a document...
  • Instructions for naming Excel tablesInstructions for naming Excel tables
    tables in excel are named table 1, table2, table 3,... but if you keep the names like this, it will be difficult to handle the table. therefore, users should change and name the excel table.
  • Instructions for inserting table captions in WordInstructions for inserting table captions in Word
    similar to other content in word, you can insert a caption for the table to explain the content of the table or simply enter a name for the table to make it easier for people to follow.