元组约束中的子查询 DB2

发布于 2024-09-18 00:06:01 字数 1140 浏览 6 评论 0 原文

在我的数据库课程中,我们使用一本书(Database System - the Complete Book),书中说以下是标准 SQL 中的有效创建表语句:

CREATE TABLE Participants (
    meetid INT NOT NULL,
    -- ...
    CONSTRAINT RoomConstraint
        CHECK (1 >= ALL (SELECT num FROM Numbers)
);

但是 DB2 抱怨并给出了 20 种可能的解释来说明为什么该语句失败。

那么,DB2 不支持元组约束中的子查询吗?如果不是,TRIGGER 是强制执行子查询约束的唯一解决方案吗?

更新:我发现这个链接表明这是不可能的:http://bytes.com/topic/db2/answers/837390-can-constraint-replace-trigger

但是,TRIGGER 是唯一的出路吗? (我正在尝试强制执行一种关系,其中属性可以引用两个不同的表(它不是我的数据库))

更新 2: 如果没有ALL,它就无法工作要么:

CREATE TABLE Foo (
   meetid INT NOT NULL,
   CHECK (meetid IN (SELECT meetid FROM Foo)));

更新 3: 这个想法是我想要一个引用两个表的外键,如下所示:

Table Participants (pid, ...)
Table Rooms (room, ...)
Table People (userid, ...)

本质上,pid 应存在于 Rooms(属性 room)或 People(属性 userid)中。我可以用行约束来检查 pid 是在 Rooms 还是在 People 中,但 DB2 不允许。 (我知道还有很多其他东西需要限制来模拟外键)

In my database course we use a book (Database System - the Complete Book) which says the following is a valid create table statement in standard SQL:

CREATE TABLE Participants (
    meetid INT NOT NULL,
    -- ...
    CONSTRAINT RoomConstraint
        CHECK (1 >= ALL (SELECT num FROM Numbers)
);

But DB2 complains and gives 20 possible explanations for why this statement fails.

So, does DB2 not support sub-queries in tuple-constraints? And if not, is a TRIGGER the only solution for enforcing the sub-query constraint?

Update: I've found this link which states it ain't possible: http://bytes.com/topic/db2/answers/837390-can-constraint-replace-trigger

But again, is a TRIGGER the only way out? (I'm trying to enforce a relationship where a attribute can refer to two different tables (it ain't my database))

Update 2: It does not work without ALL either:

CREATE TABLE Foo (
   meetid INT NOT NULL,
   CHECK (meetid IN (SELECT meetid FROM Foo)));

Update 3: The idea is that I want a foreign key which references two tables like the following:

Table Participants (pid, ...)
Table Rooms (room, ...)
Table People (userid, ...)

Essentially, a pid shall exists in either Rooms (attribute room) or in People (attribute userid). I could a part of this with a row constraint which checks whether pid is in Rooms or in People - but DB2 won't let me. (I know there is a lot of other stuff to constrain for emulating the foreign key)

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

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

发布评论

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

评论(2

一抹淡然 2024-09-25 00:06:01

如何在 SQL Server 中实现可选(或备用)外键检查约束

create function dbo.meetidinmeetings(@meetid)
returns bit
as
begin
declare @return bit
as
if exists(select 1 from meetings where meetid = @meetid)
set @return =1 
else
set @return = 0
return @return
end

...

CREATE TABLE Foo (
meetid INT NOT NULL,
ismeeting bit NOT NULL DEFAULT 0

ALTER TABLE FOO
ADD CONSTRAINT CHK_FOO_MEETID
CHECK ((ismeeting = 0) or (ismeeting = 1 and dbo.is_meetidinmeetings(meetid) = 1)))

How to implement an optional (or alternate) Foreign Key check constraint in SQL Server

create function dbo.meetidinmeetings(@meetid)
returns bit
as
begin
declare @return bit
as
if exists(select 1 from meetings where meetid = @meetid)
set @return =1 
else
set @return = 0
return @return
end

then...

CREATE TABLE Foo (
meetid INT NOT NULL,
ismeeting bit NOT NULL DEFAULT 0

ALTER TABLE FOO
ADD CONSTRAINT CHK_FOO_MEETID
CHECK ((ismeeting = 0) or (ismeeting = 1 and dbo.is_meetidinmeetings(meetid) = 1)))
骷髅 2024-09-25 00:06:01

ALL() 不是标准 SQL** —— 它是 T-SQL 扩展。 DB2 不支持这一点。

我不确定您想对约束做什么 - 看起来您正在尝试确保 Numbers 表中 num 的每个值都小于或等于 1。如果情况确实如此,那么您应该在 Numbers 表上添加约束,而不是在参与者上添加约束。

**SQL92 标准,我不相信它被添加到 SQL99 或 SQL2003

ALL() is not standard SQL** -- it's a T-SQL extension. DB2 does not support this.

I'm not sure what you are trying to do with your constraint -- it looks like you're trying to ensure that every value of num in the Numbers table is less than or equal to 1. If this is actually the case, you should add a constraint on the Numbers table, not on Participants.

**the SQL92 standard, and I don't believe it was added to SQL99 or SQL2003

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