简体   繁体   English

如何确保关系数据库中的有向图约束?

[英]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 ? 有没有一种方法可以在SQL中强制执行此约束,或者我是否必须使用存储的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... 您可以在2个值中添加一个唯一索引-假设您有一个表EDGES 2列ZONE_A_ID和ZONE_B_ID,代码将类似于...

CREATE UNIQUE INDEX UNIQUE_EDGE ON EDGES(ZONE_A_ID, ZONE_B_ID); 在边上创建UNIQUE INDEX UNIQUE_EDGE(ZONE_A_ID,ZONE_B_ID);

You could also create a constraint ZONE_A_ID < ZONE_B_ID to prevent duplicates... 您还可以创建约束ZONE_A_ID <ZONE_B_ID以防止重复...

CHECK (ZONE_A_ID < ZONE_B_ID) 检查(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://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm#i1106547

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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