Leonardo Jimenez
Leonardo Jimenez

Reputation: 45

Deleting using Inner Join having foreign key

I need some help with a query.

These are my tables:

Table 1 - General

id

name

last_name

Table 2 - User

id

username

table1_Id (This one references to table1 ids) - FK

Since my references are on cascade, if I delete table1, it will delete all the others.

But I don't know how to do the query.

I want to get first a Id in Table2 then get Table2.Table1_id , then go to Table1 and delete the Id that I just got.

Upvotes: 0

Views: 33

Answers (1)

twentylemon
twentylemon

Reputation: 1246

delete from Table1

your keys are on cascade, this will also delete all the rows in Table2

you can delete one row from table1 the same as you would normally

delete from Table1 where id=to_delete

this will cascade and delete the rows in table2 that referenced to_delete

ok; edit again

delete from Table1 where id=(select table1_id from Table2 where id=group_to_delete)

will delete the row from Table1 with the id you get from the row in Table2, which will cascade and delete the rows in Table2 that share the same key.

Upvotes: 1

Related Questions