成免费的crm,久久国产精品新农夫导航新妓网,恋夜秀场全部视频安卓手机,女校花强奷在线播放A级

MySQL 臨時(shí)表

mysql 臨時(shí)表

mysql 臨時(shí)表在我們需要保存一些臨時(shí)數(shù)據(jù)時(shí)是非常有用,比如查詢(xún)語(yǔ)句非常復(fù)雜,可以用臨時(shí)表充當(dāng)一個(gè)變量角色。臨時(shí)表只在當(dāng)前連接可見(jiàn),當(dāng)關(guān)閉連接時(shí),mysql會(huì)自動(dòng)刪除表并釋放所有空間。

mysql臨時(shí)表只在當(dāng)前連接可見(jiàn),如果你使用php腳本來(lái)創(chuàng)建mysql臨時(shí)表,那每當(dāng)php腳本執(zhí)行完成后,該臨時(shí)表也會(huì)自動(dòng)銷(xiāo)毀。

如果你使用了其他mysql客戶(hù)端程序連接mysql數(shù)據(jù)庫(kù)服務(wù)器來(lái)創(chuàng)建臨時(shí)表,那么只有在關(guān)閉客戶(hù)端程序時(shí)才會(huì)銷(xiāo)毀臨時(shí)表,當(dāng)然你也可以手動(dòng)銷(xiāo)毀。

 

1. 創(chuàng)建臨時(shí)表

使用 create temporary table tablename 命令創(chuàng)建臨時(shí)表。創(chuàng)建臨時(shí)表的語(yǔ)法和 create table tablename 是一樣的,只是多了一個(gè) temporary 關(guān)鍵字。

以下展示了使用mysql 臨時(shí)表的簡(jiǎn)單范例,以下的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í),你將無(wú)法看到 salessummary表。

如果你退出當(dāng)前mysql會(huì)話(huà),再使用 select命令來(lái)讀取原先創(chuàng)建的臨時(shí)表數(shù)據(jù),那你會(huì)發(fā)現(xiàn)數(shù)據(jù)庫(kù)中沒(méi)有該表的存在,因?yàn)樵谀阃顺鰰r(shí)該臨時(shí)表已經(jīng)被銷(xiāo)毀了。

 

2. 刪除 mysql 臨時(shí)表

默認(rèn)情況下,當(dāng)你斷開(kāi)與數(shù)據(jù)庫(kù)的連接后,臨時(shí)表就會(huì)自動(dòng)被銷(xiāo)毀。當(dāng)然你也可以在當(dāng)前mysql會(huì)話(huà)使用 drop table 命令來(lái)手動(dòng)刪除臨時(shí)表。

以下是手動(dòng)刪除臨時(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 'salessummary' doesn't exist

下一節(jié):mysql 復(fù)制表

mysql 教程

相關(guān)文章