Sql 删除数据库记录及其相关记录
我有一个产品、类别和“productsInCategories”的数据库,看起来像:Sql 删除数据库记录及其相关记录,sql,sql-server-ce,Sql,Sql Server Ce,我有一个产品、类别和“productsInCategories”的数据库,看起来像: SELECT * FROM Products INNER JOIN ProductsInCategories ON Products.Id=ProductsInCategories.product WHERE category=@0 ,@0=categoryId 用户 id█ name ▌ ▀▀█▀▀▀▀▀▀▀▀▌ 1 █ john ▌ 69█ ja
SELECT *
FROM Products
INNER JOIN ProductsInCategories
ON Products.Id=ProductsInCategories.product
WHERE category=@0
,@0=categoryId
用户
id█ name ▌
▀▀█▀▀▀▀▀▀▀▀▌
1 █ john ▌
69█ jane ▌
id█ name █ cost █description▌
▀▀█▀▀▀▀▀▀▀▀█▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▀▀▌
21█snickers█ 23 █ foo ▌
34█ mars █ 20 █ bar ▌
37█ daim █ 21 █ oofrab ▌
79█ banana █ 8 █ foobar ▌
80█ apple █ 10 █ barfoo ▌
id█ userId █ name ▌
▀▀█▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▌
10█ 69 █chocolate▌
55█ 69 █favorites▌
20█ 1 █ fruit ▌
categoryId█productId▌
▀▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▌
10 █ 21 ▌
10 █ 34 ▌
20 █ 79 ▌
20 █ 80 ▌
55 █ 21 ▌
55 █ 37 ▌
产品
id█ name ▌
▀▀█▀▀▀▀▀▀▀▀▌
1 █ john ▌
69█ jane ▌
id█ name █ cost █description▌
▀▀█▀▀▀▀▀▀▀▀█▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▀▀▌
21█snickers█ 23 █ foo ▌
34█ mars █ 20 █ bar ▌
37█ daim █ 21 █ oofrab ▌
79█ banana █ 8 █ foobar ▌
80█ apple █ 10 █ barfoo ▌
id█ userId █ name ▌
▀▀█▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▌
10█ 69 █chocolate▌
55█ 69 █favorites▌
20█ 1 █ fruit ▌
categoryId█productId▌
▀▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▌
10 █ 21 ▌
10 █ 34 ▌
20 █ 79 ▌
20 █ 80 ▌
55 █ 21 ▌
55 █ 37 ▌
类别
id█ name ▌
▀▀█▀▀▀▀▀▀▀▀▌
1 █ john ▌
69█ jane ▌
id█ name █ cost █description▌
▀▀█▀▀▀▀▀▀▀▀█▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▀▀▌
21█snickers█ 23 █ foo ▌
34█ mars █ 20 █ bar ▌
37█ daim █ 21 █ oofrab ▌
79█ banana █ 8 █ foobar ▌
80█ apple █ 10 █ barfoo ▌
id█ userId █ name ▌
▀▀█▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▌
10█ 69 █chocolate▌
55█ 69 █favorites▌
20█ 1 █ fruit ▌
categoryId█productId▌
▀▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▌
10 █ 21 ▌
10 █ 34 ▌
20 █ 79 ▌
20 █ 80 ▌
55 █ 21 ▌
55 █ 37 ▌
产品类别
id█ name ▌
▀▀█▀▀▀▀▀▀▀▀▌
1 █ john ▌
69█ jane ▌
id█ name █ cost █description▌
▀▀█▀▀▀▀▀▀▀▀█▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▀▀▌
21█snickers█ 23 █ foo ▌
34█ mars █ 20 █ bar ▌
37█ daim █ 21 █ oofrab ▌
79█ banana █ 8 █ foobar ▌
80█ apple █ 10 █ barfoo ▌
id█ userId █ name ▌
▀▀█▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▌
10█ 69 █chocolate▌
55█ 69 █favorites▌
20█ 1 █ fruit ▌
categoryId█productId▌
▀▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀▀▌
10 █ 21 ▌
10 █ 34 ▌
20 █ 79 ▌
20 █ 80 ▌
55 █ 21 ▌
55 █ 37 ▌
这产生了一些可以被视为:
Users
john
fruit
banana
apple
jane
chocolate
snickers
mars
daim
favorites
snickers
daim
当我想获得某个类别的所有产品时,我会执行以下操作:
SELECT *
FROM Products
INNER JOIN ProductsInCategories
ON Products.Id=ProductsInCategories.product
WHERE category=@0
,@0=categoryId
这一切都很好,插入或删除产品非常简单。
然而,我现在有一个问题,我不能缠绕我的头
即删除类别。我该怎么做呢?我已经做了几个小时了,但似乎无法使它工作。
如果用户删除某个类别,我希望仅存在于该类别中的所有产品与关联的productInCategories记录一起被删除。
如果说用户“Jane”删除了她的类别“chocolate”,那么我想删除产品“mars”,而不是“snickers”或“mars”,因为它们也存在于类别“favorites”中。一些pseodocode可以帮助您:
Delete from ProductsIncategories where category = X
Delete from products where productID in (
select productID from
products left outer join productsincategories
on products.productID = productsincategories.productID
where
productsincategories.categoryid is null
)
delete from categories where category = x
select productID form
from
products as p
inner join productsincategories as pic
on PIC.productid = p.productid
inner join (
select distinct productID from productsincategories
where categoryid = X
) as t
on t.productid = p.productid
group by
productid
having
count(*) = 1
下面是使用约阿希姆·艾萨克森的小提琴进行的选择(现在用你的答案加上约阿希姆)
左外部联接-快速注释
table a
id
1
2
table b
aID value
1 x
1 y
a left outer join b (select * from a left outer join b on a.id = b.aID)
id aID value
1 1 x
1 1 y
2 NULLNULL
b left outer join a (select * from b left outer join a on a.id = b.aID)
aID value id
1 x 1
1 y 1
left outer join提供了左侧表中的所有内容,以及右侧表中的匹配项。如果左表中有一行而右表中没有行,则右表中的所有列都将为NULL。希望这会有所帮助,但值得一试谷歌搜索和尝试
当你想说查看所有商品的销售情况,但不管是否有销售,你都想拥有商品列表时,它非常有用。或者我们在这里使用它来查找一个表中的所有内容,而不是另一个表中的所有内容,方法是只选择“right”表为null的位置,然后使用一些pseodocode来启动:
Delete from ProductsIncategories where category = X
Delete from products where productID in (
select productID from
products left outer join productsincategories
on products.productID = productsincategories.productID
where
productsincategories.categoryid is null
)
delete from categories where category = x
select productID form
from
products as p
inner join productsincategories as pic
on PIC.productid = p.productid
inner join (
select distinct productID from productsincategories
where categoryid = X
) as t
on t.productid = p.productid
group by
productid
having
count(*) = 1
下面是使用约阿希姆·艾萨克森的小提琴进行的选择(现在用你的答案加上约阿希姆)
左外部联接-快速注释
table a
id
1
2
table b
aID value
1 x
1 y
a left outer join b (select * from a left outer join b on a.id = b.aID)
id aID value
1 1 x
1 1 y
2 NULLNULL
b left outer join a (select * from b left outer join a on a.id = b.aID)
aID value id
1 x 1
1 y 1
left outer join提供了左侧表中的所有内容,以及右侧表中的匹配项。如果左表中有一行而右表中没有行,则右表中的所有列都将为NULL。希望这会有所帮助,但值得一试谷歌搜索和尝试
当你想说查看所有商品的销售情况,但不管是否有销售,你都想拥有商品列表时,它非常有用。或者就像我们在这里使用它来查找一个表中而不是另一个表中的所有内容一样,只需选择“right”表为null的位置,除非您在模式中设置了级联删除,否则您需要使用3个delete语句,每个表一个
-- Delete category 10 from Categories table.
DELETE FROM Categories WHERE Id=10;
-- Delete category 10 from ProductsInCategories table.
DELETE FROM ProductsInCategories WHERE CategoryId=10;
-- Delete all products that are no longer in a category
DELETE Products
FROM Products
LEFT JOIN ProductsInCategories
ON ProductId=Products.Id
WHERE ProductId IS NULL;
.除非在模式中设置级联删除,否则需要使用3条delete语句,每个表一条
-- Delete category 10 from Categories table.
DELETE FROM Categories WHERE Id=10;
-- Delete category 10 from ProductsInCategories table.
DELETE FROM ProductsInCategories WHERE CategoryId=10;
-- Delete all products that are no longer in a category
DELETE Products
FROM Products
LEFT JOIN ProductsInCategories
ON ProductId=Products.Id
WHERE ProductId IS NULL;
.既然你的主要问题是关于删除,而不是加入,我会相应地修改标题。你很快。我在发布问题后的5秒钟内更改了它。标题实际上来自另一个问题,我在开始写这个问题后自己解决了这个问题。stackoverflow上有这么多用户,肯定有人会“快”,即第一个看到你的问题的人。当然。我不是想批评你或任何快速指出它的事情(=)因为你的主要问题是关于删除,而不是加入,我会相应地修改标题。你很快。易昌