从两个表中删除行

发布于 2024-07-15 03:47:58 字数 671 浏览 9 评论 0原文

我有两张桌子。 这些表之间有两种关系。

Table 1
   * ID_XPTO (PK)
   * Detail

Table 2
   * ID_XPTO (FK) (PK)
   * ID_XPTO2 (FK) (PK)

这两种关系是存在的。

Table 1 -< Table2 
Table 1 -< Table2

我的问题是我需要删除表 1 中的某些行。我目前正在做,

declare @table Table (xptoTable2 int)
insert into @table
        select ID_XPTO2
        from Table2 
        where ID_XPTO = @ID_XPTO

delete from Table2
where ID_XPTO = @ID_XPTO

delete from Table
where ID_XPTO in (select xptoTable2from @table)

我知道我可以在表 2 上使用 ON DELETE SET NULL 。 这样我就可以在 ID_XPTO2 上搜索所有具有空值的行并删除它们,但 DBA 不想使用它。

有没有更好的解决方案来完成这个过程?

I have two tables. Those tables have two relation between them.

Table 1
   * ID_XPTO (PK)
   * Detail

Table 2
   * ID_XPTO (FK) (PK)
   * ID_XPTO2 (FK) (PK)

Those two relations exists.

Table 1 -< Table2 
Table 1 -< Table2

My question is that I need to delete some row in table 1. I'm currently doing,

declare @table Table (xptoTable2 int)
insert into @table
        select ID_XPTO2
        from Table2 
        where ID_XPTO = @ID_XPTO

delete from Table2
where ID_XPTO = @ID_XPTO

delete from Table
where ID_XPTO in (select xptoTable2from @table)

I know that I could use ON DELETE SET NULL on table2. On that way I could then search for all rows with null value on ID_XPTO2 and delete them, but DBA does not wants to use it.

Is there some better solution to do this process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

简单爱 2024-07-22 03:47:58

您有以下选项:

  • 像您现在所做的那样,在两个语句中删除。 首先从 Table2 中删除。

  • 如果您的数据库品牌支持多表DELETE语法(例如MySQL),则可以在一条语句中删除两个表。 这不是标准 SQL,但很方便。

  • 使用级联引用完整性约束(我了解您的 DBA 已取消此选项)。

  • 在 Table1 上编写一个触发器 BEFORE DELETE,以删除 Table2 中的任何引用或将其设置为 NULL。 请咨询您的 DBA,看看这是否比级联 RI 约束更容易接受。

最后,我建议与您的 DBA 交谈并询问您在此提出的相同问题。 找出他/她希望您使用什么解决方案。 StackOverflow 上的人们可以回答技术问题,但听起来您正在处理 IT 政策问题

You have these options:

  • Delete in two statements, as you are doing now. Delete from Table2 first.

  • Delete from two tables in one statement, if your brand of database supports multi-table DELETE syntax (e.g. MySQL). This is not standard SQL, but it is handy.

  • Use cascading referential integrity constraints (I understand your DBA has nixed this option).

  • Write a trigger BEFORE DELETE on Table1, to delete or set NULL any reference in Table2. Check with your DBA to see if this is any more acceptable than the cascading RI constraints.

Finally, I would advise talking to your DBA and asking the same question you asked here. Find out what solution he/she would prefer you to use. Folks on StackOverflow can answer technical questions, but it sounds like you are dealing with an IT policy question.

彻夜缠绵 2024-07-22 03:47:58

使用ON DELETE CASCADE。 它会自动删除引用行。

Use ON DELETE CASCADE. It'll automatically delete referencing rows.

我纯我任性 2024-07-22 03:47:58

为什么不使用ON DELETE CASCASE

DROP TABLE t_f
DROP TABLE t_m
CREATE TABLE t_m (id INT NOT NULL IDENTITY PRIMARY KEY , value VARCHAR(50))
CREATE TABLE t_f (id INT NOT NULL IDENTITY PRIMARY KEY, m INT, CONSTRAINT fk_m FOREIGN KEY (m) REFERENCES t_m(id) ON DELETE CASCADE)
INSERT INTO t_m (value) VALUES ('test')
INSERT INTO t_f (m) VALUES (1)
DELETE FROM t_m
SELECT * FROM t_m
SELECT * FROM t_f

id           value
------------ ------
0 rows selected

id           m
------------ ------
0 rows selected

Why don't you use ON DELETE CASCASE?

DROP TABLE t_f
DROP TABLE t_m
CREATE TABLE t_m (id INT NOT NULL IDENTITY PRIMARY KEY , value VARCHAR(50))
CREATE TABLE t_f (id INT NOT NULL IDENTITY PRIMARY KEY, m INT, CONSTRAINT fk_m FOREIGN KEY (m) REFERENCES t_m(id) ON DELETE CASCADE)
INSERT INTO t_m (value) VALUES ('test')
INSERT INTO t_f (m) VALUES (1)
DELETE FROM t_m
SELECT * FROM t_m
SELECT * FROM t_f

id           value
------------ ------
0 rows selected

id           m
------------ ------
0 rows selected
秋凉 2024-07-22 03:47:58

我知道的两种方法:

  1. 您可以使用 ON DELETE CASCADE

  2. 编写 SQL 来自行清理,即:

     声明 @DetailCriteria ... 
    
       设置@DetailCriteria = '....' 
    
       开始传输 
       -- 首先清除Table2的任何子记录 
          从表2中删除  
          在哪里  
            ID_XPTO IN(从表 1 中选择 ID_XPTO,其中详细信息 = @DetailCriteria) 
            OR ID_XPTO2 IN(从表 1 中选择 ID_XPTO,其中详细信息 = @DetailCriteria) 
    
       -- 接下来清除 Table2(这将很好地删除,因为您已经遵循了引用链) 
          从表 1 中删除,其中详细信息 = @DetailCriteria 
    
       -- 如果你满意就提交(应该首先检查@@ERROR) 
       犯罪 
      

Two methods I know of:

  1. You could use ON DELETE CASCADE

  2. Write your SQL to clean up after itself ie:

     DECLARE @DetailCriteria ...
    
     SET @DetailCriteria = '....'
    
     BEGIN TRAN
     -- First clear the Table2 of any child records
        DELETE FROM Table2 
        WHERE 
          ID_XPTO IN (SELECT ID_XPTO FROM Table1 WHERE Detail = @DetailCriteria)
          OR ID_XPTO2 IN (SELECT ID_XPTO FROM Table1 WHERE Detail = @DetailCriteria)
    
     -- Next clear Table2 (which will delete fine because you've followed the referential chain)
        DELETE FROM Table1 WHERE Detail = @DetailCriteria
    
     -- commit if you're happy (should check @@ERROR first)
     COMMIT
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文