Question:
SQL Delete query. Help me with this.?
Vikram
2013-03-02 02:46:59 UTC
Hi,

I am facing error with the below query,

DELETE FROM Posting INNER JOIN Views ON Posting.category = Views.category WHERE PostingID=22

Thanks,
Vikram
Three answers:
Raghavendra Mudugal
2013-03-02 03:25:42 UTC
Hello;



When you are sure of deleting the post with the id = 22, then why are you using the join with the VIEW table. you just simply execute the delete statement directly.



delete from posting where postingid = 22



If you are trying to delete all the posts of some specific category then the statement will differ.



Post the error here; so we can help better.

=======

Hello;



In that case - this is actually referred as cascade deletion where you delete the primary row in the master table and that also deletes the secondary rows from the child table.



I am not sure which version of sql server you are using, but in general you cannot delete using 1 sql statement, in such case you need to use two sql statments, delete from the child first and then delete the parent.



If you are using the microsoft sql server 2008 and higher then on the child table when you are creating the link use the ON DELETE CASCADE settings and then recreate/modify the link. once this is done, you just need to delete the row from the parent table and automatically it will also deletes the row from the child.



Hope this helps
Blackened
2013-03-02 11:51:35 UTC
Looks like PostingID should be Posting.ID :



DELETE FROM Posting INNER JOIN Views ON Posting.category = Views.category WHERE Posting.ID=22
Tanmoy Mohish
2013-03-02 13:01:21 UTC
DELETE ******??TABLE NAME HERE???****** FROM Posting INNER JOIN Views ON Posting.category = Views.category WHERE PostingID=22


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...