0

我在下面有 2 个数据库表:

问题表

QuestionId(Pk)  SessionId(PK)   Question
1               AAA             my name is
2               AAA             my age is
1               AAB             my DOB is

Image_问题表

ImageQuestionId(Pk)  ImageId   SessionId   QuestionId
1                    1         AAB         1
2                    2         AAA         2 
3                    3         AAA         1

我想要发生的是,如果用户从问题表中删除这一行:

1               AAB         my DOB is

我希望它通过引用 SessionId 和 Question Id 来删除 Image_Question 表中的相应行。由于问题表中删除的行由 SessionId = AAB 和 QuestionId = 1 组成,它应该从包含 SessionId = AAB AND QuestionId = 1 的 Image_Question 表中删除相应的行,这意味着应该从中删除的唯一行图片问题如下:

1                    1         AAB         1

这是因为 SessionId 和 QuestionId 都与问题表中已删除的行匹配。

有谁知道如何为此编写 SQL 代码?

更新:

下面是我试过的?

ALTER TABLE Image_Question ADD CONSTRAINT FK_SessionQuestion FOREIGN KEY (SessionId,QuestionId) REFERENCES Question (SessionId,QuestionId) ON DELETE CASCADE;
4

3 回答 3

1

你在你的约束下完全正确地做这件事。基本表定义一定有问题。也许你发表你的CREATE TABLE陈述。我的实验都成功了。看看我的日志:

我已经在以下SQL Fiddle的创建表中尝试过它并且它有效:

...
                            FOREIGN KEY (QuestionId,SessionId) 
                             REFERENCES Question(QuestionId,SessionId)
                             ON DELETE CASCADE
...

但是你想要的是之后添加它。所以我制作了另一个SQL Fiddle,当我稍后添加约束时它也可以工作:

ALTER TABLE Image_Question ADD CONSTRAINT
                           FOREIGN KEY (QuestionId,SessionId) 
                             REFERENCES Question(QuestionId,SessionId)
                             ON DELETE CASCADE;

唯一的问题是,这似乎与您所做的完全相同:

ALTER TABLE Image_Question ADD CONSTRAINT FK_SessionQuestion 
                            FOREIGN KEY (SessionId,QuestionId) 
                            REFERENCES Question (SessionId,QuestionId) 
                            ON DELETE CASCADE;

我尝试在此SQL Fiddle中为约束命名。结果相同。

编辑:你的桌子INDEX上有一个QUESTION以便FOREIGN KEY正确引用吗?我意识到您FOREIGN KEY的顺序不同,SessionID,QuestionID而不是QuestionId,SessionId可以,只要您INDEXQUESTION表格中的顺序也是如此。

编辑2:这里的全部内容:

CREATE TABLE Question (QuestionId INT      NOT NULL, 
                       SessionId  CHAR(3)  NOT NULL,
                       Question   CHAR(10) NOT NULL ) ENGINE=INNODB;

INSERT INTO Question VALUES (1,'AAA','my name is');
INSERT INTO Question VALUES (2,'AAA','my age is');
INSERT INTO Question VALUES (1,'AAB','my DOB is');

-- This is how to add a primary key afterwards
ALTER TABLE QUESTION ADD PRIMARY KEY(SessionId,QuestionId);

CREATE TABLE Image_Question (ImageQuestionId INT     NOT NULL, 
                             ImageId          INT     NOT NULL,
                             SessionId        CHAR(3) NOT NULL,
                             QuestionId       INT     NOT NULL,
                            PRIMARY KEY (ImageQuestionId)
                            ) ENGINE=INNODB;

INSERT INTO Image_Question VALUES (1,1,'AAB',1);
INSERT INTO Image_Question VALUES (2,2,'AAA',2);
INSERT INTO Image_Question VALUES (3,3,'AAA',1);

-- This is how to just add an index afterwards
CREATE INDEX par_ind on Image_Question (SessionId,QuestionId);

ALTER TABLE Image_Question ADD CONSTRAINT FK_CONSTRAINT_NAME
                           FOREIGN KEY (SessionId,QuestionId) 
                             REFERENCES Question(SessionId,QuestionId)
                             ON DELETE CASCADE;

delete from Question where QuestionId = 1 and SessionId = 'AAB';

而作为SQL Fiddle

于 2012-09-27T21:42:08.777 回答
0
DELETE FROM Image_Question 
WHERE QuestionID = 1 AND SessionId = AAB;
于 2012-09-27T20:57:16.940 回答
0

使用外键约束,您必须有唯一的方法来识别两个表上的记录。由于Questions表上有一个复合主键 ( QuestionId& SessionId(PK)),因此您需要创建一个引用复合键的约束。因此,您需要在Image_Question表上创建一个键复合键 ( ImageQuestionId& SessionId)。

于 2012-09-27T21:17:35.643 回答