我有兩個表p
和u
如下:(PostgreSQL的9.3)如何在外鍵死鎖時刪除記錄?
CREATE TABLE p
(
pid integer NOT NULL,
uid integer,
CONSTRAINT p_fkey FOREIGN KEY (uid)
REFERENCES u (uid) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
);
CREATE TABLE u
(
uid integer NOT NULL,
pid integer,
CONSTRAINT u_fkey FOREIGN KEY (pid)
REFERENCES p (pid) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
);
在p
我:
pid uid
161556 176266
在u
我
uid pid
176266 161556
我想做的事情:
DELETE FROM u WHERE uid=176266;
DELETE FROM p WHERE pid=113116;
但我不能。
ERROR: update or delete on table "u" violates foreign key constraint "p_fkey" on table "p" DETAIL: Key (uid)=(176266) is still referenced from table "p".
我明白錯誤,但我不知道我能做些什麼來刪除。
對此提出建議?
這不是一個「僵局」。 – zerkms
@zerkms它的死鎖定義。除非先刪除b,否則不能刪除a,除非先刪除b,否則不能刪除b。 – avi