表格的truncate
,drop
和delete
有什麼區別?何時選擇哪個?有沒有人有一個快速的比較?我已經看到了很多關於這方面的信息,但還沒有在清晰的概述中找到它。我希望這篇文章有助於理解。表的截斷,刪除和刪除有什麼區別?何時選擇哪個?
我的意思是像在T-SQL這些語句中使用:
truncate table TableX
drop table TableX
delete table_name
表格的truncate
,drop
和delete
有什麼區別?何時選擇哪個?有沒有人有一個快速的比較?我已經看到了很多關於這方面的信息,但還沒有在清晰的概述中找到它。我希望這篇文章有助於理解。表的截斷,刪除和刪除有什麼區別?何時選擇哪個?
我的意思是像在T-SQL這些語句中使用:
truncate table TableX
drop table TableX
delete table_name
基於由@Michal here答案,多一些搜索我做了下面的語句下面的對比(在T-SQL ):truncate table TableX
,drop table TableX
和delete table_name
。
Truncate Drop Delete
Speed [Fast] Slow Slowest
Rolback possibility No No [Yes]
Specifiable conditions No No [Yes]
Scope All records All record+Headers Some records/All records
=whole table
Cascading effects No* No* [Yes]**
**For example: in a Table_1 there is a PK, in Table_2 there is a FK that relates with
the PK of Table_1, other words there is referential integrity. If the PK has `'ON DELETE CASCADE'`
and `delete Table_1` is ordered, then the data in Table_2 will be deleted too,
automatically. For more info about ON DELETE CASCADE and ON ALTER CASCADE, see:
https://technet.microsoft.com/en-us/library/ms186973%28v=sql.105%29.aspx.
Cascading does automatic alterations and deletes of depending objects such as foreign keys (FK),
views, and triggers. Sometimes very useful, sometimes very dangerous..
*The drop and truncate statements of a Table_1 (with PK and FK in Table_2, as decribed
in **) can't be executed, because the ssdms prohibits that. To accomplish the truncation
or dropping of a Table_1: first get rid of the FK in Table_2, by altering the table design, or
by dropping table2.
看到比較基礎的決定時所使用的語句...
作爲一個拇指:
If you want to get rid of only records
:當需要一個條件刪除 使用刪除,使用截斷當所有記錄可能會被刪除。當你想能夠回滾然後使用刪除。
If you want to get rid of the whole table
,包括標題(帶有設置的列),然後選擇放置。
If you want to get rid of values and automatically the related objects
(並且表中定義了級聯),使用delete。 (PS:在其他方言中,即使當表沒有設計級聯時,似乎也有辦法實現它,但據我所知t-sql/msss中沒有;但如果我錯了,請糾正我)
PS:如果你想alter or delete
的preferences of a column
,然後(在T-SQL方言)使用方法:
阿爾特:
alter table tableX
alter columnX datatypeX
刪除:
alter table tableX
drop column columnX
--And here's some code to play with
--table to truncate, drop or delete
create table TableX(
[Name] [nchar](25) null,
[ID_Number] [int] not null)
--tables with PK and FK
create table Table_1(
[Name] [nchar](25) null,
[ID_Number] [int] not null primary key)
create table Table_2(
[ID_Number] int not null foreign key references Table_1(ID_Number) on delete cascade,
[Buys] [int] null)
--the on delete cascade make it happen that when a ID_Number is Table_1 is deleted, that row
is automatically deleted in Table_2 too. But not the other way around,
therefor alter the design of Table_1.
insert into Table_1 (Name,ID_Number) values ('A',1),('B',2),('C',3);
insert into Table_2 (ID_Number,Buys) values (1,10),(2,20),(3,30);
select * from Table_1
select * from Table_2
truncate table table_2
truncate table table_1
drop table table_2
drop table table_1
delete Table_1
delete from dbo.table_1 where name='A'
delete from Table_1 where name like '%'
delete from dbo.table_2 where ID_Number=2
如果您添加級聯效果(在所有3種情況下),我會upvote! – jarlh
@jarlh:我會,如果我可以......對於當前的比較表,它是沒有意義的,因爲當添加一個主鍵'刪除級聯'下降和截斷不會工作。但讓我考慮一下。添加一些關於自動刪除子數據的可能性的信息不是一個壞主意! – cybork
依賴對象,如視圖,觸發器,外鍵(包括隱式索引) – jarlh