Search code examples
sqlsql-servert-sqlsql-server-2000

Delete with inner join across 3 tables


I have three tables one of this storing users basic information, the other one is profile information and the last one is storing user picture.

When i deleting these user i need to delete all of the data in these tables. So i write a query like this.

DELETE Kullanicilar FROM Kullanicilar 
INNER JOIN ProfilBilgileri ON Kullanicilar.ID = ProfilBilgileri.UyeID 
INNER JOIN UyeResim ON Kullanicilar.ID = UyeResim.UyeID 
WHERE Kullanicilar.ID=@ID

But it just deleting the data from "Kullanicilar" table.
Any suggestions?

EDIT : I'm using MSSQL 2008 but hosting firm 2000 so i need compatible code.


Solution

  • You can use a trigger like Xavinou sugested or, if you have foreign keys in your tables, you can go with Delete on Cascade option. Your foreign keys will be created using this:

    FOREIGN KEY ([Id]) REFERENCES AnotherTable
    ON DELETE CASCADE