CONSTRAINT in more than one column

From: Bob Swerdlow <swerdlow_at_maine.rr.com>
Date: Fri, 21 Dec 2001 01:25:09 GMT
Message-ID: <VbwU7.10$zJ3.7723_at_typhoon.maine.rr.com>


I'm having difficulty constructing a constraint I want on a table that represents trees of related nodes.

Each tree of nodes is identified within the table by a topicID. Each node has a reference to its parent node and can have any number of child nodes. However, there must be one and only one root node for the tree - it is identified by having a null reference to its parent node.

So, I want a constraint to ensure that there is only one node for each topicID that has a null reference to its parent node.

If you assume this table structure:

CREATE TABLE MyNode (
nodeID INTEGER PRIMARY KEY,
parentNodeID INTEGER REFERENCES MyNode (nodeID), topicID INTEGER NOT NULL);

What I want is something like:

ALTER TABLE MyNode
ADD( CONSTRAINT MyNode_OneRootNode_Check CHECK ( (parentNodeID IS NOT NULL) OR UNIQUE(topicID) ) );

However, this produces a "Missing expression" error on 'UNIQUE'. Is there a way to do this?

Many Thanks!

--
Bob Swerdlow
Chief Operating Officer
Transpose, LLC
rswerdlow_at_transpose.com
Received on Fri Dec 21 2001 - 02:25:09 CET

Original text of this message