#1109 - 多次删除未知 table 'QUESTION_ANSWER'

#1109 - Unknown table 'QUESTION_ANSWER' in MULTI DELETE

正在学习MySQL,正在学习多删但不能完全掌握

我有 4 个table。

  1. 用户
  2. 问题
  3. 回答
  4. QUESTION_ANSWER

我想查询当它删除一个用户时,它会删除QUESTION, ANSWER, QUESTION_ANSWER.

中的所有相关数据

我的table结构如下

TABLE: ANSWER

ANSWER_NUMBER (PRIMARY KEY)
VOTE_COUNT
isAccepted
ANSWER_DESCRIPTION
USER_ID (FOREIGN KEY FROM USERS.USER_ID)

TABLE: QUESTION

QUESTION_NUMBER (PRIMARY KEY)
QUESTION_DESCRIPTION
VOTE_COUNT
QUESTION_VIEWS
USER_ID
QUESTION_NUMBER_OF_ANSWERS

Table: QUESTION_ANSWER

Q_NUM (FOREIGN KEY FROM QUESTION.QUESTION_NUMBER)
A_NUM (FOREIGN KEY FROM ANSWER.ANSWER_NUMBER)

TABLE USERS:

USER_ID (PRIMARY)
USER_NAME
USER_REPUTATION
USER_NO_OF_ANSWERS
USER_NO_OF_QUESTIONS

假设我想删除用户id 1;我想删除所有问题,用户 id 1 的答案。

我到这里了。

DELETE QUESTION_ANSWER, QUESTION, USERS, ANSWER 
FROM USERS
    INNER JOIN
    QUESTION ON QUESTION_ANSWER.Q_NUM = QUESTION.QUESTION_NUMBER
    INNER JOIN ANSWER
    ON ANSWER.ANSWER_NUMBER = QUESTION_ANSWER.A_NUM
    WHERE USERS.USER_ID=1;

但是报错

DELETE QUESTION_ANSWER, QUESTION, USERS, ANSWER 
FROM USERS
    INNER JOIN
    QUESTION ON QUESTION_ANSWER.Q_NUM = QUESTION.QUESTION_NUMBER
    INNER JOIN ANSWER
    ON ANSWER.ANSWER_NUMBER = QUESTION_ANSWER.A_NUM
    WHERE USERS.USER_ID=1
MySQL said: Documentation

#1109 - Unknown table 'QUESTION_ANSWER' in MULTI DELETE

参与连接的表必须全部在 FROM 子句之后定义。
DELETE 子句中,您必须枚举将从中删除行的表。

DELETE qa, q, u, a 
FROM USERS u
INNER JOIN QUESTION q ON q.USER_ID = u.USER_ID
INNER JOIN QUESTION_ANSWER qa ON qa.Q_NUM = q.QUESTION_NUMBER
INNER JOIN ANSWER a ON a.ANSWER_NUMBER = qa.A_NUM
WHERE u.USER_ID=1

如您所见,我为表格使用了别名,因此代码更短且更易读。

如果您使用 ON DELETE CASCADE 定义了表之间的引用完整性,例如:

CREATE TABLE USERS(
USER_ID  int PRIMARY KEY,
USER_NAME VARCHAR(100),
USER_REPUTATION int,
USER_NO_OF_ANSWERS int,
USER_NO_OF_QUESTIONS int
);

CREATE TABLE ANSWER(
ANSWER_NUMBER int PRIMARY KEY,
VOTE_COUNT int,
isAccepted int,
ANSWER_DESCRIPTION VARCHAR(100)
);

CREATE TABLE QUESTION(
QUESTION_NUMBER  int PRIMARY KEY,
QUESTION_DESCRIPTION VARCHAR(100),
VOTE_COUNT int,
QUESTION_VIEWS int,
USER_ID int,
QUESTION_NUMBER_OF_ANSWERS int,
FOREIGN KEY(USER_ID) REFERENCES USERS(USER_ID) ON DELETE CASCADE
);

CREATE TABLE QUESTION_ANSWER(
Q_NUM  int,
A_NUM  int,
FOREIGN KEY(Q_NUM) REFERENCES QUESTION(QUESTION_NUMBER) ON DELETE CASCADE,
FOREIGN KEY(A_NUM) REFERENCES ANSWER(ANSWER_NUMBER) ON DELETE CASCADE
);

那么你所要做的就是从 USERSANSWER 中删除行,子表的所有行也将被删除:

DELETE u, a 
FROM USERS u
INNER JOIN QUESTION q ON q.USER_ID = u.USER_ID
INNER JOIN QUESTION_ANSWER qa ON qa.Q_NUM = q.QUESTION_NUMBER
INNER JOIN ANSWER a ON a.ANSWER_NUMBER = qa.A_NUM
WHERE u.USER_ID=1

查看简化版 demo.

首先,在删除任何相关记录时,在要删除的外键中添加级联,例如,一个用户可以创建许多问题,这意味着外键在问题中table 所以应用于那里的级联意味着每当删除用户时,它的相关问题也将被删除。

CREATE TABLE USERS(
    USER_ID  INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    USER_NAME VARCHAR(100) NOT NULL,
    USER_REPUTATION INT NOT NULL,
    USER_NO_OF_ANSWERS INT NOT NULL,
    USER_NO_OF_QUESTIONS INT NOT NULL
);

CREATE TABLE QUESTION(
    QUESTION_NUMBER INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    QUESTION_DESCRIPTION VARCHAR(255) NOT NULL,
    VOTE_COUNT INT NOT NULL,
    QUESTION_VIEWS INT NOT NULL,
    USER_ID INT NOT NULL,
    QUESTION_NUMBER_OF_ANSWERS INT NOT NULL,
    FOREIGN KEY QUESTION(USER_ID) REFERENCES USERS(USER_ID) ON DELETE CASCADE
);

现在在两个table中都插入一些数据,并尝试删除1个用户来清楚地理解级联的概念,你会看到只要删除用户,与该用户相关的问题也会自动删除.删除查询是:

DELETE FROM USERS [WHERE Clause]

现在你会清楚这个概念,第二步,尝试在ANSWER中添加一个外键table,并在那边应用级联,因为Answers也与用户相关(清楚地显示在您要删除特定用户答案的​​问题)。

CREATE TABLE ANSWER(
    ANSWER_NUMBER INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    VOTE_COUNT INT,
    isAccepted INT,
    ANSWER_DESCRIPTION VARCHAR(100) NOT NULL,
    USER_ID INT NOT NULL,
    FOREING KEY ANSWER(USER_ID) REFERENCES USERS(USER_ID) ON DELETE CASCADE
);

现在最后一件事是也从网桥中删除记录 table QUESTION_ANSWER.

CREATE TABLE QUESTION_ANSWER(
    Q_NUM  INT NOT NULL,
    A_NUM  INT NOT NULL,
    FOREIGN KEY QUESTION_ANSWER(Q_NUM) REFERENCES QUESTION(QUESTION_NUMBER) ON 
    DELETE CASCADE,
    FOREIGN KEY QUESTION_ANSWER(A_NUM) REFERENCES ANSWER(ANSWER_NUMBER) ON 
    DELETE CASCADE
);

现在你可以开始了,只需删除用户的任何记录,它在问题、答案和 QUESTION_ANSWER table 中的相关记录也将被删除。

DELETE FROM USERS [WHERE Clause]