Sql删除查询问题。 [英] Sql Delete query Problem.

查看:56
本文介绍了Sql删除查询问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题删除查询



Q1第一张表tbl_tags

和第二张表tbl_Book_tag。



问题

tbl_tag(tag_id,tagname)要删除tag_id的tbl_tag。但是tbl_Book_tag的基础因为

tbl_Book_tag(tag_id,BookID)属于tag_Id



如果我想要tbl_tag到tag_id但tag_id属于tbl_Book_tag那么tag_id不能删除。





从tbl_Tags中删除Tag_Id =(从tbl_Book_Tag中选择Tag_Id,其中Tag_Id = @ tagid)





plz help解决这个删除查询

Problem delete query

Q1 first table tbl_tags
and second table tbl_Book_tag.

problem
tbl_tag(tag_id,tagname) the tbl_tag to delete tag_id .but the base of tbl_Book_tag because
tbl_Book_tag(tag_id,BookID) belongs to tag_Id

and if i want tbl_tag to tag_id but tag_id belong to tbl_Book_tag then tag_id not be deleted.


delete from tbl_Tags where Tag_Id=(select Tag_Id from tbl_Book_Tag where Tag_Id =@tagid)


plz help solve this delete query

推荐答案

如果我理解正确你有两个表,其中tbl_tags的主键为Tag_Id,而tbl_Book_tag有一个指向该表的外键链接(或可能反过来了)



您可以采取强力方法并首先删除子表中的所有行...

If I understand you correctly you have two tables where tbl_tags has a primary key of Tag_Id and tbl_Book_tag has a Foreign Key link to that table (or possibly the other way around)

You could take the "brute force" approach and delete all of the rows in the "child" table first ...
delete from tbl_Book_Tag where Tag_Id =@tagid

然后你可以从主表中删除

and then you can delete from the "master" table

delete from tbl_tags where Tag_Id =@tagid





SQL表可以设置为你自动执行级联删除 ...请参阅以下链接以获取示例

http://stackoverflow.com/questions/3656099/how-to-delete-rows-in-tables-that-c​​ontain-foreign-keys-to-other-tables [ ^ ]

http://forums.asp.net/t/1771996.aspx [ ^ ]



SQL tables can be set up to do Cascade deletion automatically for you ... see the following links for examples
http://stackoverflow.com/questions/3656099/how-to-delete-rows-in-tables-that-contain-foreign-keys-to-other-tables[^]
http://forums.asp.net/t/1771996.aspx[^]

这篇关于Sql删除查询问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆