简体   繁体   中英

How to ensure an oriented graph constraint in a relational database?

I'm currently building a small database which represents a pipe network. I have divided the network in zones, which are connected to each others. In graph theory terms, the zones are the vertices, the connections between the zones are the edge. I'm storing the edges as a database table, with two fields : one for the "upstream" or "left" zone, the other for the "downstream" or "right" zone. I want my graph to be oriented, eg for two zones there can be only one connection between them.

Is there a way to enforce this constraint in SQL, or do I have to check that with a stored proc ?

You could add a unique index in the 2 values - assuming you have a table EDGES 2 columns ZONE_A_ID and ZONE_B_ID, the code would look something like...

CREATE UNIQUE INDEX UNIQUE_EDGE ON EDGES(ZONE_A_ID, ZONE_B_ID);

You could also create a constraint ZONE_A_ID < ZONE_B_ID to prevent duplicates...

CHECK (ZONE_A_ID < ZONE_B_ID)

The syntax for these will differ depending on which DB you use.

See:

http://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm#i1106547

http://www.techonthenet.com/oracle/check.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM