MySQL刪除表的三種方式(小結(jié))
drop table
drop 是直接刪除表信息,速度最快,但是無(wú)法找回?cái)?shù)據(jù)
例如刪除 user 表:
drop table user;
truncate (table)
truncate 是刪除表數(shù)據(jù),不刪除表的結(jié)構(gòu),速度排第二,但不能與where一起使用
例如刪除 user 表:
truncate table user;
delete from
delete 是刪除表中的數(shù)據(jù),不刪除表結(jié)構(gòu),速度最慢,但可以與where連用,可以刪除指定的行
例如刪除user表的所有數(shù)據(jù)
delete from user;
刪除user表的指定記錄
delete from user where user_id = 1;
三種方式的區(qū)別
相同點(diǎn)
truncate和不帶where子句的delete,drop都會(huì)刪除表內(nèi)的數(shù)據(jù); drop,truncate都是DDL語(yǔ)句(數(shù)據(jù)定義語(yǔ)言),執(zhí)行后會(huì)自動(dòng)提交;不同點(diǎn)
語(yǔ)句類型:delete語(yǔ)句是數(shù)據(jù)庫(kù)操作語(yǔ)言(DML),truncate,drop是數(shù)據(jù)庫(kù)定義語(yǔ)言(DDL); 效率:一般來(lái)說(shuō) drop > truncate> delete; 是否刪除表結(jié)構(gòu):truncate和delete 只刪除數(shù)據(jù)不刪除表結(jié)構(gòu),truncate 刪除后將重建索引(新插入數(shù)據(jù)后id從0開(kāi)始記起),而 delete不會(huì)刪除索引 (新插入的數(shù)據(jù)將在刪除數(shù)據(jù)的索引后繼續(xù)增加),drop語(yǔ)句將刪除表的結(jié)構(gòu)包括依賴的約束,觸發(fā)器,索引等; 安全性:drop和truncate刪除時(shí)不記錄MySQL日志,不能回滾,delete刪除會(huì)記錄MySQL日志,可以回滾; 返回值:delete 操作后返回刪除的記錄數(shù),而 truncate 返回的是0或者-1(成功則返回0,失敗返回-1);小知識(shí)
delete 與 delete from 區(qū)別
如果只針對(duì)一張表進(jìn)行刪除,則效果一樣;如果需要聯(lián)合其他表,則需要使用from
delete tb1 from tb1 m where id in (select id from tb2);
用法總結(jié)
希望刪除表結(jié)構(gòu)時(shí),用 drop; 希望保留表結(jié)構(gòu),但要?jiǎng)h除所有記錄時(shí), 用 truncate; 希望保留表結(jié)構(gòu),但要?jiǎng)h除部分記錄時(shí), 用 delete。到此這篇關(guān)于MySQL刪除表的三種方式(小結(jié))的文章就介紹到這了,更多相關(guān)MySQL 刪除表內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. MySQL全文搜索之布爾搜索2. Oracle 9i數(shù)據(jù)庫(kù)的用戶創(chuàng)建以及權(quán)限分配3. MySQL Community Server 5.1.494. 啟動(dòng)MYSQL出錯(cuò) Manager of pid-file quit without updating file.5. Oracle數(shù)據(jù)字典的應(yīng)用實(shí)例6. MySQL中 concat函數(shù)的使用7. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的備份與恢復(fù)8. MYSQL(電話號(hào)碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)9. oracle觸發(fā)器介紹10. 用最簡(jiǎn)單的方法復(fù)制或遷移Oracle數(shù)據(jù)庫(kù)
