通过内连接删除 3 个表
我有三个表,其中一个存储用户基本信息,另一个是个人资料信息,最后一个存储用户图片。
当我删除这些用户时,我需要删除这些表中的所有数据。所以我写了一个这样的查询。
DELETE Kullanicilar FROM Kullanicilar
INNER JOIN ProfilBilgileri ON Kullanicilar.ID = ProfilBilgileri.UyeID
INNER JOIN UyeResim ON Kullanicilar.ID = UyeResim.UyeID
WHERE Kullanicilar.ID=@ID
但它只是从“Kullanicilar”表中删除数据。
有什么建议吗?
编辑:我正在使用 MSSQL 2008,但托管公司 2000,所以我需要兼容的代码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用像 Xavinou sugested 这样的触发器,或者,如果您的表中有外键,您可以使用“级联删除”选项。您的外键将使用以下命令创建:
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:
我想到的解决方案是使用事务。
The solution that springs to mind is to use a transaction.
当然,我假设 Kullanicilar 和其他两个表之间存在一一对应关系。如果是一对多,则其他每个表都需要一个临时表,并根据该临时表进行删除。希望这能为您指明正确的方向!
Of course, I'm assuming that there's a 1-to-1 correspondence between Kullanicilar and the other two tables. If it's 1-to-many, you'll need a temp table for each of the other tables and delete based on that. Hope this points you in the right direction!