表的截断、删除和删除之间有什么区别?什么时候选择哪个?
What is the difference between truncate, drop and delete of tables? And when to choose for which?
表格truncate
、drop
、delete
有什么区别?什么时候选择哪个?有没有人有一个快速比较?我已经看到很多关于此的信息,但还没有在清晰的概述中找到它。我希望这 post 有助于理解。
我的意思是喜欢在 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
:
当需要条件删除时使用 delete,当所有记录可能被删除时使用 truncate。当您希望能够回滚时,请使用删除。
If you want to get rid of the whole table
,包括 headers(带有设置的列)然后选择删除。
If you want to get rid of values and automatically the related objects
(而级联是在table中定义的),使用delete。 (PS:在其他方言中,即使 table 没有设计成级联,似乎也有办法实现它,但据我所知 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
表格truncate
、drop
、delete
有什么区别?什么时候选择哪个?有没有人有一个快速比较?我已经看到很多关于此的信息,但还没有在清晰的概述中找到它。我希望这 post 有助于理解。
我的意思是喜欢在 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
:
当需要条件删除时使用 delete,当所有记录可能被删除时使用 truncate。当您希望能够回滚时,请使用删除。
If you want to get rid of the whole table
,包括 headers(带有设置的列)然后选择删除。
If you want to get rid of values and automatically the related objects
(而级联是在table中定义的),使用delete。 (PS:在其他方言中,即使 table 没有设计成级联,似乎也有办法实现它,但据我所知 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