Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 删除数据库记录及其相关记录_Sql_Sql Server Ce - Fatal编程技术网

Sql 删除数据库记录及其相关记录

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

我有一个产品、类别和“productsInCategories”的数据库,看起来像:

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
  • 如果您首先删除类别中的产品,因为这取决于其他表,而不取决于它

  • 现在在no categories中查找产品-这些是仅在要删除的类别中的产品-这是通过从product到productsincategories的左外连接来完成的-这将提供products中的所有行,如果它们没有来自categories中产品的匹配记录,则为null

  • 删除类别本身,因为现在没有任何内容链接到它

  • 作为一个旁注,考虑不要只在这一类中删除产品,而要给出一个报告,供人们回顾和清理。在我看来,如果不是这样的话,工作可能会丢失得太多。也许至少在你删除它们之前给它们一个确认的机会

    还要注意,中间的语句将删除所有不属于某个类别的产品。。在这类问题上有两种观点

  • 任何不属于某一类别的产品都是错误的,所以尽可能地将它们全部清理干净
  • 有各种各样的原因,为什么一个产品可能没有类别这是没有用的我 如果您只想移除仅属于此类别的产品,而整体移除并不好,那么类似的操作可能会有所帮助:

    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
    
  • 如果您首先删除类别中的产品,因为这取决于其他表,而不取决于它

  • 现在在no categories中查找产品-这些是仅在要删除的类别中的产品-这是通过从product到productsincategories的左外连接来完成的-这将提供products中的所有行,如果它们没有来自categories中产品的匹配记录,则为null

  • 删除类别本身,因为现在没有任何内容链接到它

  • 作为一个旁注,考虑不要只在这一类中删除产品,而要给出一个报告,供人们回顾和清理。在我看来,如果不是这样的话,工作可能会丢失得太多。也许至少在你删除它们之前给它们一个确认的机会

    还要注意,中间的语句将删除所有不属于某个类别的产品。。在这类问题上有两种观点

  • 任何不属于某一类别的产品都是错误的,所以尽可能地将它们全部清理干净
  • 有各种各样的原因,为什么一个产品可能没有类别这是没有用的我 如果您只想移除仅属于此类别的产品,而整体移除并不好,那么类似的操作可能会有所帮助:

    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上有这么多用户,肯定有人会“快”,即第一个看到你的问题的人。当然。我不是想批评你或任何快速指出它的事情(=)因为你的主要问题是关于删除,而不是加入,我会相应地修改标题。你很快。易昌