DELETE FROM table with self-referencing FK #7551
-
Given CREATE TABLE SOME_TABLE (
ID INTEGER NOT NULL,
DATA VARCHAR(50),
PARENT_ID INTEGER
);
ALTER TABLE SOME_TABLE ADD PRIMARY KEY (ID);
ALTER TABLE SOME_TABLE ADD FOREIGN KEY (PARENT_ID) REFERENCES SOME_TABLE (ID);
COMMIT;
INSERT INTO SOME_TABLE (ID, DATA, PARENT_ID) VALUES (1, 'd1', NULL);
INSERT INTO SOME_TABLE (ID, DATA, PARENT_ID) VALUES (2, 'd2', 1);
INSERT INTO SOME_TABLE (ID, DATA, PARENT_ID) VALUES (3, 'd3', 1);
INSERT INTO SOME_TABLE (ID, DATA, PARENT_ID) VALUES (4, 'd4', 3);
INSERT INTO SOME_TABLE (ID, DATA, PARENT_ID) VALUES (5, 'd5', 3);
COMMIT; How could someone delete all records from this table?
I'm working on Firebird dialect for SQL Alchemy and I got stuck with this problem. It appears all "main" databases (PostgreSQL, Oracle, Microsoft SQL Server, MySQL and SQLite) supported by SQL Alchemy test suite pass without problem with a full Questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
It's well know problem, Firebird checks integrity after each row is deleted instead of after the full statement execution. One workaround is ORDER BY clause that can be used in DELETE and UPDATE. |
Beta Was this translation helpful? Give feedback.
-
Moved to firebird-devel. |
Beta Was this translation helpful? Give feedback.
-
I've a big doubt that such order by could exists in practice. Another workaround is to use undo log with the self-FK checks, like it is used for the stable cursor feature. |
Beta Was this translation helpful? Give feedback.
It's well know problem, Firebird checks integrity after each row is deleted instead of after the full statement execution.
One workaround is ORDER BY clause that can be used in DELETE and UPDATE.