Postgresql delete multiple rows from multiple tables

fawzib

Consider 2 or more tables:

users (id, firstname, lastname)
orders (orderid, userid, orderdate, total)

I wish to delete all users and their orders that match first name 'Sam'. In mysql, I usually do left join. In this example userid is unknown to us.

What is the correct format of the query?

Juan Carlos Oropeza

http://www.postgresql.org/docs/current/static/sql-delete.html

DELETE 
FROM orders o
USING users u
WHERE o.userid = u.id
  and u.firstname = 'Sam';

DELETE 
FROM users u
WHERE u.firstname = 'Sam';

You can also create the table with ON delete cascade

http://www.postgresql.org/docs/current/static/ddl-constraints.html

CREATE TABLE order_items (
    product_no integer REFERENCES products ON DELETE RESTRICT,
    order_id integer REFERENCES orders ON DELETE CASCADE,
    quantity integer,
    PRIMARY KEY (product_no, order_id)
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete rows from multiple tables as one query

From Dev

Delete rows from multiple tables in a database

From Dev

DELETE rows from multiple tables with JOIN in Mysql

From Dev

Delete multiple rows from multiple tables in mysql using single value

From Dev

How to delete multiple rows from multiple tables using Where clause?

From Dev

MySQL delete multiple rows in multiple tables

From Dev

Stored procedure: delete rows from multiple tables with output for every row

From Dev

SQL Select rows and delete/update from dynamic multiple tables

From Dev

Delete from multiple tables with MyISAM

From Dev

Delete from multiple tables, if has

From Dev

Querying rows from multiple tables

From Dev

How to delete multiple rows in diferent tables?

From Dev

How to delete rows in multiple tables using pdo

From Dev

Combine multiple rows from multiple tables in MySQL

From Dev

SQL Server 2008 R2: Delete duplicate rows from multiple tables and keep original one

From Dev

MySQL - Delete from multiple tables using a UNION?

From Dev

Delete the same set of values from multiple tables

From Dev

Query to delete from multiple tables in Access

From Dev

LINQ Join to delete contents from multiple tables

From Dev

Query to delete from multiple tables in Access

From Dev

Delete from multiple related tables on one id?

From Dev

Delete records from multiple Access tables

From Dev

Delete from multiple tables with SQL in PHP

From Dev

Fetch data from multiple tables in postgresql

From Dev

MySQL combine rows from multiple tables into on row

From Dev

COUNT rows from multiple joined tables - MSSQL

From Dev

Deleting rows from multiple tables with jQuery

From Dev

Dynamic rows to columns from multiple tables with mysql

From Dev

SQL Count Rows From Multiple tables

Related Related

  1. 1

    Delete rows from multiple tables as one query

  2. 2

    Delete rows from multiple tables in a database

  3. 3

    DELETE rows from multiple tables with JOIN in Mysql

  4. 4

    Delete multiple rows from multiple tables in mysql using single value

  5. 5

    How to delete multiple rows from multiple tables using Where clause?

  6. 6

    MySQL delete multiple rows in multiple tables

  7. 7

    Stored procedure: delete rows from multiple tables with output for every row

  8. 8

    SQL Select rows and delete/update from dynamic multiple tables

  9. 9

    Delete from multiple tables with MyISAM

  10. 10

    Delete from multiple tables, if has

  11. 11

    Querying rows from multiple tables

  12. 12

    How to delete multiple rows in diferent tables?

  13. 13

    How to delete rows in multiple tables using pdo

  14. 14

    Combine multiple rows from multiple tables in MySQL

  15. 15

    SQL Server 2008 R2: Delete duplicate rows from multiple tables and keep original one

  16. 16

    MySQL - Delete from multiple tables using a UNION?

  17. 17

    Delete the same set of values from multiple tables

  18. 18

    Query to delete from multiple tables in Access

  19. 19

    LINQ Join to delete contents from multiple tables

  20. 20

    Query to delete from multiple tables in Access

  21. 21

    Delete from multiple related tables on one id?

  22. 22

    Delete records from multiple Access tables

  23. 23

    Delete from multiple tables with SQL in PHP

  24. 24

    Fetch data from multiple tables in postgresql

  25. 25

    MySQL combine rows from multiple tables into on row

  26. 26

    COUNT rows from multiple joined tables - MSSQL

  27. 27

    Deleting rows from multiple tables with jQuery

  28. 28

    Dynamic rows to columns from multiple tables with mysql

  29. 29

    SQL Count Rows From Multiple tables

HotTag

Archive