5

我有一个 h2 数据库,其中包含两个带有外键的表,例如

CREATE TABLE master (masterId INT PRIMARY KEY, slaveId INT);
CREATE TABLE slave (slaveId INT PRIMARY KEY, something INT,
    CONSTRAINT fk FOREIGN KEY (slaveId) REFERENCES master(slaveId));

并且需要类似的东西

DELETE master, slave FROM master m JOIN slave s ON m.slaveId = s.slaveId
    WHERE something = 43;

即,从两个表中删除(AFAIK 在 MySql 中工作)。由于 FOREIGN KEY,我无法先从 master 中删除。当我从从属服务器开始删除时,我丢失了从主服务器删除哪些行的信息。使用 ON DELETE CASCADE 会有所帮助,但我不希望它每次都自动发生。我应该暂时允许吗?我应该使用临时表还是最好的解决方案?

4

1 回答 1

10

没有人回答,但这实际上是微不足道的:

SET REFERENTIAL_INTEGRITY FALSE;
BEGIN TRANSACTION;
DELETE FROM master WHERE slaveId IN (SELECT slaveId FROM slave WHERE something = 43);
DELETE FROM slave WHERE something = 43;
COMMIT;
SET REFERENTIAL_INTEGRITY TRUE;
于 2012-07-27T13:52:51.597 回答