《MySQL 臨時(shí)表》要點(diǎn):
本文介紹了MySQL 臨時(shí)表,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
MySQL 臨時(shí)表在我們需要保留一些臨時(shí)數(shù)據(jù)時(shí)是非常有用的.臨時(shí)表只在當(dāng)前連接可見,當(dāng)關(guān)閉連接時(shí),Mysql會(huì)自動(dòng)刪除表并釋放所有空間.
臨時(shí)表在MySQL 3.23版本中添加,如果你的MySQL版本低于 3.23版本就無法使用MySQL的臨時(shí)表.不外現(xiàn)在一般很少有再使用這么低版本的MySQL數(shù)據(jù)庫(kù)服務(wù)了.
MySQL臨時(shí)表只在當(dāng)前連接可見,如果你使用PHP腳原來創(chuàng)建MySQL臨時(shí)表,那沒當(dāng)PHP腳本執(zhí)行完成后,該臨時(shí)表也會(huì)自動(dòng)銷毀.
如果你使用了其他MySQL客戶端法式連接MySQL數(shù)據(jù)庫(kù)服務(wù)器來創(chuàng)建臨時(shí)表,那么只有在關(guān)閉客戶端法式時(shí)才會(huì)銷毀臨時(shí)表,當(dāng)然你也可以手動(dòng)銷毀.
實(shí)例
以下展示了使用MySQL 臨時(shí)表的簡(jiǎn)單實(shí)例,以下的SQL代碼可以適用于PHP劇本的mysql_query()函數(shù).
mysql> CREATE TEMPORARY TABLE SalesSummary (
-> product_name VARCHAR(50) NOT NULL
-> , total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00
-> , avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00
-> , total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
);
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO SalesSummary
-> (product_name, total_sales, avg_unit_price, total_units_sold)
-> VALUES
-> ('cucumber', 100.25, 90, 2);
mysql> SELECT * FROM SalesSummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber | 100.25 | 90.00 | 2 |
+--------------+-------------+----------------+------------------+
1 row in set (0.00 sec)
當(dāng)你使用 SHOW TABLES命令顯示數(shù)據(jù)表列表時(shí),你將無法看到 SalesSummary表.
如果你退出當(dāng)前MySQL會(huì)話,再使用 SELECT命令來讀取原先創(chuàng)立的臨時(shí)表數(shù)據(jù),那你會(huì)發(fā)現(xiàn)數(shù)據(jù)庫(kù)中沒有該表的存在,因?yàn)樵谀阃顺鰰r(shí)該臨時(shí)表已經(jīng)被銷毀了.
刪除MySQL 暫時(shí)表
默認(rèn)環(huán)境下,當(dāng)你斷開與數(shù)據(jù)庫(kù)的連接后,臨時(shí)表就會(huì)自動(dòng)被銷毀.當(dāng)然你也可以在當(dāng)前MySQL會(huì)話使用 DROP TABLE 命令來手動(dòng)刪除臨時(shí)表.
以下是手動(dòng)刪除暫時(shí)表的實(shí)例:
mysql> CREATE TEMPORARY TABLE SalesSummary (
-> product_name VARCHAR(50) NOT NULL
-> , total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00
-> , avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00
-> , total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
);
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO SalesSummary
-> (product_name, total_sales, avg_unit_price, total_units_sold)
-> VALUES
-> ('cucumber', 100.25, 90, 2);
mysql> SELECT * FROM SalesSummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber | 100.25 | 90.00 | 2 |
+--------------+-------------+----------------+------------------+
1 row in set (0.00 sec)
mysql> DROP TABLE SalesSummary;
mysql> SELECT * FROM SalesSummary;
ERROR 1146: Table 'RUNOOB.SalesSummary' doesn't exist
如您還有不明確的可以在下面與我留言或是與我探討QQ群308855039,我們一起飛!
《MySQL 臨時(shí)表》是否對(duì)您有啟發(fā),歡迎查看更多與《MySQL 臨時(shí)表》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/7140.html