Postgresql 中文操作指南
6.3. Deleting Data #
到目前为止,我们已经解释了如何向表中添加数据以及如何更改数据。接下来,就是讨论如何删除不再需要的数据。正如添加数据只能以整行为单位地添加那样,你只能删除表中的整个行。在上一部分中,我们解释了 SQL 没有提供直接寻址各个行的办法。因此,删除行只能通过指定要删除的行必须匹配的条件。如果表中有主键,那么你可以指定确切的行。但是,你也可以删除与某个条件相匹配的行组,或者一次删除表中的所有行。
So far we have explained how to add data to tables and how to change data. What remains is to discuss how to remove data that is no longer needed. Just as adding data is only possible in whole rows, you can only remove entire rows from a table. In the previous section we explained that SQL does not provide a way to directly address individual rows. Therefore, removing rows can only be done by specifying conditions that the rows to be removed have to match. If you have a primary key in the table then you can specify the exact row. But you can also remove groups of rows matching a condition, or you can remove all rows in the table at once.
You use the DELETE command to remove rows; the syntax is very similar to the UPDATE command. For instance, to remove all rows from the products table that have a price of 10, use:
DELETE FROM products WHERE price = 10;
如果你仅编写:
If you simply write:
DELETE FROM products;
那么表中的所有行都将被删除!小心程序员。
then all rows in the table will be deleted! Caveat programmer.