SQL, How to delete rows from related tables using a query?

calev

I have the following tables

    vehicle (veh_num(PK), veh_desc)
    log (log_num(PK), veh_num(FK), log_date, log_complaint)
    log_line (log_num(PK,FK), logline_num(PK), emp_id(FK), logline_date,logline_action)
    part (part_code(PK), logline_num(FK), log_num(FK), part_desc)
    signout (signout_num(PK), part_code(FK), emp_id(FK), log_num(FK), signout_date)

I want to run a query which will delete all the records in the vehicle table with for instance, veh_num = "EK458" and also delete rows which are related to the veh_num in the other tables.

I have started with the following query,

    DELETE FROM signout WHERE EXISTS
    (select * from vehicle,log,log_line,part
    where 
    vehicle.veh_num = 'EK458'  AND
    vehicle.veh_num = log.veh_num AND
    log.log_num = log_line.log_num AND
    log_line.log_num = part.log_num AND 
    part.part_code = signout.part_code);

This query deletes all the associated values of veh_num = "EK458" in the signout table, however, I want a query which will delete the rows from the all the tables which are related to veh_num. Thanks in advance

SamiHuutoniemi

I think what you want to do is having the delete cascade into other tables.

Take a look at this How do I use cascade delete with SQL Server?

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 two tables related

From Dev

How to delete rows from three tables - query erro

From Dev

How to fill WPF DataGridView from related tables using LINQ query?

From Dev

Delete rows from multiple tables as one query

From Dev

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

From Dev

How to delete rows from two tables using INNER JOIN in mysql?

From Dev

SQL query for returning rows from two tables

From Dev

How to delete rows in multiple tables using pdo

From Dev

Trigger to delete rows from related tables before deleting rows from actual table

From Dev

how to retrieve all stations from a query with many related tables using Laravel Eloquent?

From Dev

How can I delete rows from tables using EF when inside an Asp.Net method?

From Dev

delete rows not correspondent in sql tables

From Dev

SQL Select rows and delete/update from dynamic multiple tables

From Dev

How to retrieve data from two tables related using a third table, SQL Server

From Dev

How to retrieve data from two tables related using a third table, SQL Server

From Dev

Delete multiple rows from multiple tables in mysql using single value

From Dev

Delete rows from all tables

From Dev

How to query rows from 2 tables in a single query?

From Dev

How to do LEFT JOIN two tables and exclude multiple rows from main query in subquery using MySQL?

From Dev

SQL Query to delete data from tables defined in table

From Dev

How can I delete rows from two tables?

From Dev

get rows from two tables using join and sub query

From Dev

Delete row from 2 related tables

From Dev

Delete data from three different tables that is related

From Dev

Delete from multiple related tables on one id?

From Dev

Deleting rows from two tables related by constraint

From Dev

SQL Delete rows based on query from same table?

From Dev

SQL Delete rows based on query from same table?

From Dev

SQL - query tables related in two different ways

Related Related

  1. 1

    Delete rows from two tables related

  2. 2

    How to delete rows from three tables - query erro

  3. 3

    How to fill WPF DataGridView from related tables using LINQ query?

  4. 4

    Delete rows from multiple tables as one query

  5. 5

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

  6. 6

    How to delete rows from two tables using INNER JOIN in mysql?

  7. 7

    SQL query for returning rows from two tables

  8. 8

    How to delete rows in multiple tables using pdo

  9. 9

    Trigger to delete rows from related tables before deleting rows from actual table

  10. 10

    how to retrieve all stations from a query with many related tables using Laravel Eloquent?

  11. 11

    How can I delete rows from tables using EF when inside an Asp.Net method?

  12. 12

    delete rows not correspondent in sql tables

  13. 13

    SQL Select rows and delete/update from dynamic multiple tables

  14. 14

    How to retrieve data from two tables related using a third table, SQL Server

  15. 15

    How to retrieve data from two tables related using a third table, SQL Server

  16. 16

    Delete multiple rows from multiple tables in mysql using single value

  17. 17

    Delete rows from all tables

  18. 18

    How to query rows from 2 tables in a single query?

  19. 19

    How to do LEFT JOIN two tables and exclude multiple rows from main query in subquery using MySQL?

  20. 20

    SQL Query to delete data from tables defined in table

  21. 21

    How can I delete rows from two tables?

  22. 22

    get rows from two tables using join and sub query

  23. 23

    Delete row from 2 related tables

  24. 24

    Delete data from three different tables that is related

  25. 25

    Delete from multiple related tables on one id?

  26. 26

    Deleting rows from two tables related by constraint

  27. 27

    SQL Delete rows based on query from same table?

  28. 28

    SQL Delete rows based on query from same table?

  29. 29

    SQL - query tables related in two different ways

HotTag

Archive