Delete from one table unless row is referenced in another table

synic

We have two tables, that look something like this:

CREATE TABLE devices(
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(255) NOT NULL,
    "last_log_id" integer NULL
);

CREATE TABLE log(
    "id" serial NOT NULL PRIMARY KEY, 
    "created_at" timestamp with time zone NOT NULL,
    "msg" varchar(255) NOT NULL,
);

ALTER TABLE "devices" ADD CONSTRAINT 
   "device_last_log_id" FOREIGN KEY ("last_log_id") 
   REFERENCES "log" ("id") DEFERRABLE INITIALLY DEFERRED;

What is an efficient query to delete all "log" rows older than a certain "created_at" date, unless they are referenced by the "devices" table "last_log_id" column?

wildplasser
delete from log l
where l.created_at < 'somedate'
and not exists (select 1
     from devices d
     where d.last_log_id = l.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

prevent soft delete if row is referenced in another table

From Dev

Delete all rows in one table that aren't referenced by another table

From Dev

Checking certain row from a table which is referenced to another table?

From Dev

Select values from one table depending on referenced value in another table

From Dev

How to delete row from table without delete a row from referenced table?

From Dev

Copy a row from one table to another

From Dev

Jquery - copy a row from one table to another

From Dev

Move table columns from one row into another

From Dev

delete row from table where column does not exist in another table

From Dev

Delete Row in a Table Based on a Lookup Value from Another Table

From Dev

Joining row from one table with the sum value from another table

From Dev

How to delete rows from one table matching another table?

From Dev

How to add a value from one table to a particular row in another table?

From Dev

Insert many rows from a table into one unique row in another table

From Dev

SQL query to select row from one table which is not in another table

From Dev

Delete row in a table by clicking cell in another table

From Dev

PostgreSQL: deleting rows referenced from another table

From Dev

How to get referenced values from another table?

From Dev

Delete rows in one table with query in another table

From Dev

how to delete from another 2 table using one key?

From Dev

Delete a row from Parse Table

From Dev

Delete a row from sqlite table

From Dev

Delete row from dynamic table

From Dev

Delete row from table conditional on a related table

From Dev

Migrating row from one table to another with conditions [MYSQL]

From Dev

Add row when selected from one table to another using php

From Dev

How to submit row from one table to another using php, msqli

From Dev

find only one row from table in foreign with another tables

From Dev

Copying row from one table to another when trigger is called

Related Related

  1. 1

    prevent soft delete if row is referenced in another table

  2. 2

    Delete all rows in one table that aren't referenced by another table

  3. 3

    Checking certain row from a table which is referenced to another table?

  4. 4

    Select values from one table depending on referenced value in another table

  5. 5

    How to delete row from table without delete a row from referenced table?

  6. 6

    Copy a row from one table to another

  7. 7

    Jquery - copy a row from one table to another

  8. 8

    Move table columns from one row into another

  9. 9

    delete row from table where column does not exist in another table

  10. 10

    Delete Row in a Table Based on a Lookup Value from Another Table

  11. 11

    Joining row from one table with the sum value from another table

  12. 12

    How to delete rows from one table matching another table?

  13. 13

    How to add a value from one table to a particular row in another table?

  14. 14

    Insert many rows from a table into one unique row in another table

  15. 15

    SQL query to select row from one table which is not in another table

  16. 16

    Delete row in a table by clicking cell in another table

  17. 17

    PostgreSQL: deleting rows referenced from another table

  18. 18

    How to get referenced values from another table?

  19. 19

    Delete rows in one table with query in another table

  20. 20

    how to delete from another 2 table using one key?

  21. 21

    Delete a row from Parse Table

  22. 22

    Delete a row from sqlite table

  23. 23

    Delete row from dynamic table

  24. 24

    Delete row from table conditional on a related table

  25. 25

    Migrating row from one table to another with conditions [MYSQL]

  26. 26

    Add row when selected from one table to another using php

  27. 27

    How to submit row from one table to another using php, msqli

  28. 28

    find only one row from table in foreign with another tables

  29. 29

    Copying row from one table to another when trigger is called

HotTag

Archive