Gre*_*ill 105
使用该TRUNCATE TABLE命令.
vit*_*tfo 89
PostgreSQL数据库中表/表的内容可以通过多种方式删除.
使用sql删除表内容:
删除一个表的内容:
TRUNCATE table_name;
DELETE FROM table_name;
Run Code Online (Sandbox Code Playgroud)
删除所有命名表的内容:
TRUNCATE table_a, table_b, …, table_z;
Run Code Online (Sandbox Code Playgroud)
删除引用它们的命名表和表的内容(我将在本答案后面详细解释):
TRUNCATE table_a, table_b CASCADE;
Run Code Online (Sandbox Code Playgroud)
使用pgAdmin删除表内容:
删除一个表的内容:
Right click on the table -> Truncate
Run Code Online (Sandbox Code Playgroud)
删除引用它的表和表的内容:
Right click on the table -> Truncate Cascaded
Run Code Online (Sandbox Code Playgroud)
删除和截断之间的区别:
从文档:
DELETE从指定的表中删除满足WHERE子句的行.如果不存在WHERE子句,则效果是删除表中的所有行. http://www.postgresql.org/docs/9.3/static/sql-delete.html
TRUNCATE是一个PostgreSQL扩展,它提供了一种更快的机制来从表中删除所有行.TRUNCATE快速删除一组表中的所有行.它与每个表上的非限定DELETE具有相同的效果,但由于它实际上不扫描表,因此速度更快.此外,它立即回收磁盘空间,而不是需要后续的VACUUM操作.这对大型表最有用. http://www.postgresql.org/docs/9.1/static/sql-truncate.html
使用从其他表引用的表:
当您拥有包含多个表的数据库时,表可能具有关系.例如,有三个表:
create table customers (
customer_id int not null,
name varchar(20),
surname varchar(30),
constraint pk_customer primary key (customer_id)
);
create table orders (
order_id int not null,
number int not null,
customer_id int not null,
constraint pk_order primary key (order_id),
constraint fk_customer foreign key (customer_id) references customers(customer_id)
);
create table loyalty_cards (
card_id int not null,
card_number varchar(10) not null,
customer_id int not null,
constraint pk_card primary key (card_id),
constraint fk_customer foreign key (customer_id) references customers(customer_id)
);
Run Code Online (Sandbox Code Playgroud)
并为这些表准备了一些数据:
insert into customers values (1, 'John', 'Smith');
insert into orders values
(10, 1000, 1),
(11, 1009, 1),
(12, 1010, 1);
insert into loyalty_cards values (100, 'A123456789', 1);
Run Code Online (Sandbox Code Playgroud)
表命令引用表客户和表loyalty_cards引用表客户.当您尝试TRUNCATE/DELETE FROM其他表引用的表(其他表/ s具有指定表的外键约束)时,您会收到错误.要删除所有三个表中的内容,您必须为所有这些表命名(顺序并不重要)
TRUNCATE customers, loyalty_cards, orders;
Run Code Online (Sandbox Code Playgroud)
或者只是用CASCADE关键字引用的表(你可以命名更多的表而不仅仅是一个)
TRUNCATE customers CASCADE;
Run Code Online (Sandbox Code Playgroud)
这同样适用于pgAdmin.右键单击customers表并选择Truncate Cascaded.
Erw*_*ter 26
对于小型表DELETE通常更快并且需要更少的激进锁定(对于大量并发负载):
DELETE FROM tbl;
Run Code Online (Sandbox Code Playgroud)
没有WHERE条件.
对于中型或大型表,请TRUNCATE tbl像@Greg一样发布.
小智 9
我为每个可能使用DBeaver这样的工具的人找到了一种非常简单快捷的方法:您只需选择要截断(SHIFT + click或CTRL + click)的所有表即可right click
如果您有外键,请选择面板CASCADE上的选项Settings。Start仅此而已!
| 归档时间: |
|
| 查看次数: |
108540 次 |
| 最近记录: |