如何编写SQL语句,例如下面

时间:2012-09-27 20:53:33

标签: mysql sql

我有2个数据库表:

问题表

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

Image_Question表

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,它应该从Image_Question表中删除相应的行,其中包含SessionId = AAB AND QuestionId = 1,这意味着应该从中删除的唯一行图像问题如下:

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;

3 个答案:

答案 0 :(得分: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这是可以的,只要INDEX表中的QUESTION也按此顺序排列即可。

编辑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

一样

答案 1 :(得分:0)

DELETE FROM Image_Question 
WHERE QuestionID = 1 AND SessionId = AAB;

答案 2 :(得分:0)

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