SQL表自引用查询与使用校验和的唯一约束

发布于 2024-07-29 22:24:18 字数 931 浏览 6 评论 0原文

我想更好地了解在插入之前使用 CHECKSUM(具有唯一约束)自引用表 语句(如下所示)之间检查记录中的唯一性的差异。 在什么情况下,一种选择会成为优于另一种选择的最佳选择,原因是什么?

要求:每组列对于表中的每条记录都需要是唯一的,这就是我把这个语句放在一起的原因; 在一次数据库调用中检查所有列。

INSERT INTO tblTable
(Column1, Column2, Column3, Column4, Column5, Column6) 
SELECT 
@Column1, @Column2, @Column3, @Column4, @Column5, @Column6 
WHERE NOT EXISTS 
    (SELECT DISTINCT 
        t1.Column1, 
        t1.Column2,  
            t2.Column3, 
            t2.Column4, 
                t3.Column5, 
                t3.Column6 
    FROM tblTable t1 
        JOIN tblTable t2 ON (t1.UID = t2.UID)
        JOIN tblTable t3 ON (t1.UID = t3.UID)
    WHERE
        t1.Column1 = @Column1 and 
        t1.Column2 = @Column2 and 
            t2.Column3 = @Column3 and 
            t2.Column4 = @Column4 and 
                t3.Column5 = @Column5 and 
                t3.Column6 = @Column6)

I would like to better understand the differences for checking uniqueness in a record before an INSERT between using CHECKSUM (with unique constraints) versus self-referencing table statement like the one below. What scenarios would pose one option to be the best choice over the other, and for what reasons?

Requirement: Each set of columns need to be unique from every record in the table, which is why I put this statement together; to check for all columns in one call to the database.

INSERT INTO tblTable
(Column1, Column2, Column3, Column4, Column5, Column6) 
SELECT 
@Column1, @Column2, @Column3, @Column4, @Column5, @Column6 
WHERE NOT EXISTS 
    (SELECT DISTINCT 
        t1.Column1, 
        t1.Column2,  
            t2.Column3, 
            t2.Column4, 
                t3.Column5, 
                t3.Column6 
    FROM tblTable t1 
        JOIN tblTable t2 ON (t1.UID = t2.UID)
        JOIN tblTable t3 ON (t1.UID = t3.UID)
    WHERE
        t1.Column1 = @Column1 and 
        t1.Column2 = @Column2 and 
            t2.Column3 = @Column3 and 
            t2.Column4 = @Column4 and 
                t3.Column5 = @Column5 and 
                t3.Column6 = @Column6)

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

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

发布评论

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

评论(2

以往的大感动 2024-08-05 22:24:22

为什么您需要的不仅仅是这个 NOT EXISTS 子句?

NOT EXISTS 
    (SELECT *
    FROM tblTable t1 
    WHERE
        t1.Column1 = @Column1 and 
        t1.Column2 = @Column2)

严重地。

我从来没有想过......但自连接意味着对多于一行的唯一检查(或在一组行上唯一?或在父/子行上唯一?)。 这将使唯一行概念无效...

编辑,第二次阅读

只需使用 标准唯一约束

不需要 CHECKSUM,无论如何它都不能保证唯一性。

但是,有一个很小的机会
校验和不会改变。 为了这
原因,我们不建议使用
CHECKSUM 检测值是否有
更改,除非您的应用程序可以
容忍偶尔错过更改

Why would you need more than this NOT EXISTS clause?

NOT EXISTS 
    (SELECT *
    FROM tblTable t1 
    WHERE
        t1.Column1 = @Column1 and 
        t1.Column2 = @Column2)

Seriously.

I've never thought it through... but the self join implies unique checks over more than one row (or unique over a group of rows? or unique over parent/child rows?). Which would invalidate the unique row concept...

Edit, on 2nd reading

Just use a standard unique constraint.

No need for CHECKSUM, which does not guarantee uniqueness anyway.

However, there is a small chance that
the checksum will not change. For this
reason, we do not recommend using
CHECKSUM to detect whether values have
changed, unless your application can
tolerate occasionally missing a change

如梦 2024-08-05 22:24:21

使用唯一约束将保证受约束的数据是唯一的,无论其如何插入(从查询编辑器手动插入、通过存储过程、通过 ORM 工具或任何其他抽象层)。 它并不依赖于开发人员记住检查他们编写的每个查询是否违反了该原则。 它还使您的查询不那么复杂且易于阅读。 没有犯错的机会。

Using a Unique constraint will guarantee that the constrained data is unique regardless of how it is inserted (by hand from query editor, via a stored procedure, via an ORM tool or any other layer of abstraction). It doesn't rely on developers remembering to check with every query they write that they aren't violating the principle. It also makes your queries less convoluted and easy to read. No chance of mistakes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文