postgresql之 drop & delete & truncate

官网:https://www.postgresql.org/docs/8.1/sql-droptable.html

Name

 DROP TABLE -- remove a table

Synopsis

DROP TABLE name [, ...] [ CASCADE | RESTRICT ]

Description

DROP TABLE removes tables from the database. 
Only its owner may destroy a table.
To empty a table of rows, without destroying the table, use DELETE.


DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table.

However, to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified.
(CASCADE will remove a dependent view entirely, but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely.)



Name

DELETE -- delete rows of a table


Synopsis

DELETE FROM [ ONLY ] table
    [ USING usinglist ]
    [ WHERE condition ]

Description

DELETE deletes rows that satisfy the WHERE clause from the specified table.

If the WHERE clause is absent, the effect is to delete all rows in the table.

The result is a valid, but empty table.

Tip: TRUNCATE is a PostgreSQL extension that provides a faster mechanism to remove all rows from a table.

By default, DELETE will delete rows in the specified table and all its child tables.

If you wish to delete only from the specific table mentioned, you must use the ONLY clause.

There are two ways to delete rows in a table using information contained in other tables in the database: using sub-selects, or specifying additional tables in the USING clause. Which technique is more appropriate depends on the specific circumstances.

You must have the DELETE privilege on the table to delete from it, as well as the SELECT privilege for any table in the USING clause or whose values are read in the condition.

Parameters

ONLY

If specified, delete rows from the named table only. When not specified, any tables inheriting from the named table are also processed.

table

The name (optionally schema-qualified) of an existing table.

usinglist

A list of table expressions, allowing columns from other tables to appear in the WHERE condition. This is similar to the list of tables that can be specified in the FROM Clause of a SELECT statement; for example, an alias for the table name can be specified. Do not repeat the target table in the usinglist, unless you wish to set up a self-join.

condition

An expression returning a value of type boolean, which determines the rows that are to be deleted.

Notes

PostgreSQL lets you reference columns of other tables in the WHERE condition by specifying the other tables in the USING clause. For example, to delete all films produced by a given producer, one might do

DELETE FROM films USING producers
  WHERE producer_id = producers.id AND producers.name = 'foo';

What is essentially happening here is a join between films and producers, with all successfully joined films rows being marked for deletion. This syntax is not standard. A more standard way to do it is

DELETE FROM films
  WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');

 In some cases the join style is easier to write or faster to execute than the sub-select style.

Examples

Delete all films but musicals:

DELETE FROM films WHERE kind <> 'Musical';

Clear the table films:

DELETE FROM films;
 
 

Name

TRUNCATE -- empty a table or set of tables

Synopsis

TRUNCATE [ TABLE ] name [, ...]

Description

TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE on each table, but since it does not actually scan the tables it is faster.

This is most useful on large tables.

Parameters

name

The name (optionally schema-qualified) of a table to be truncated.

Notes

Only the owner of a table may TRUNCATE it.

TRUNCATE cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command. Checking validity in such cases would require table scans, and the whole point is not to do one.

TRUNCATE will not run any user-defined ON DELETE triggers that might exist for the tables.

Examples

Truncate the tables bigtable and fattable:

TRUNCATE TABLE bigtable, fattable;

猜你喜欢

转载自www.cnblogs.com/panpanwelcome/p/11643711.html