I have a table:
CREATE TABLE MENUPOINT (
id BIGINT NOT NULL,
parent BIGINT,
name VARCHAR(64),
CONSTRAINT "MENUPOINT_pkey" PRIMARY KEY(id),
CONSTRAINT fkc75dac36251dd346 FOREIGN KEY (parent)
REFERENCES MENUPOINT(id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE
);
And this content:
id parent name
------------------------
1 null root
2 1 child
All this to create this structure:
root
+- child
Now I need a checker on the Database to check that this can not be executed:
UPDATE MENUPOINT SET parent = 2 WHERE id = 1;
Because:
- I would not be able to find out who is root.
- The display of the tree would be endless like this:
root
+- child
+- root
+- child
+- root ....
What I have:
CONSTRAINT "NOT_SELF_REFERENCE" CHECK (id <> parent)
But it does not check the whole tree.
What have to be changed for a non-looping tree?