Skip to content

Commit 79f0ac4

Browse files
authored
Merge pull request #20983 from MikeRayMSFT/20220110-graph-edge-constraints
Update graph edge constraints
2 parents d557f0d + 62592d0 commit 79f0ac4

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

docs/relational-databases/tables/graph-edge-constraints.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,45 @@ SQL Graph supports edge constraints, which enable users to add constraints to th
3232

3333
### Edge Constraint Clauses
3434

35-
Each edge constraint consists of one or more edge constraint clause(s). An edge constraint clause is the pair of FROM and TO nodes that the given edge could connect.
35+
A single edge constraint consists of one or more edge constraint clause(s).
36+
````
37+
CONSTRAINT constraint_name CONNECTION (cause1[, clause2...])
38+
````
3639

37-
Consider that you have `Product` and `Customer` nodes in your graph and you use `bought` edge to connect these nodes. The edge constraint clause specifies the FROM and TO node pair and the direction of the edge. In this case the edge constraint clause will be `Customer` TO `Product`. That is, inserting a `bought` that goes from a `Customer` to `Product` will be allowed. Attempts to insert an edge that goes from `Product` to `Customer` fail.
40+
- An edge constraint clause is a pair of node table names, separated by the `TO` keyword.
41+
- The first table name in the edge constraint clause is the name of the FROM node table for the edge relationship.
42+
- The second table name in the edge constraint clause is the name of the TO node table for the edge relationship.
43+
- The pair of table names therefore indicates the _direction_ of the edge relationship.
44+
- As stated previously, an edge constraint can contain one or more edge constraint clauses.
3845

39-
- An edge constraint clause contains a pair of FROM and TO node tables that the edge constraint is enforced on.
40-
- Users may specify multiple edge constraint clauses per edge constraint, which will be applied as a disjunction.
41-
- If multiple edge constraints are created on a single edge table, edges must satisfy ALL constraints to be allowed. For a detailed explanation of where this scenario could be used, please see the example "Creating a new edge constraint on existing edge table, with new edge constraint clause" later in this page.
46+
#### Multiple constraints and clauses
47+
- Multiple edge constraints, defined for the same edge table, are enforced with an `AND` operator.
48+
- Multiple edge constraint _clauses_, defined within the same edge constraint, are enforced with an `OR` operator.
49+
50+
Consider the `Supplier` and `Customer` nodes in your graph. Each can be related to the `Product` node by a single, shared edge table: `bought`. The `bought` edge table supports `Customer-(bought)->Product` and `Supplier-(bought)->Product` relationship types. This can be accomplished using a single edge constraint with multiple edge constraint clauses.
51+
52+
##### Examples
53+
54+
```sql
55+
CONSTRAINT EC_BOUGHT CONNECTION (Customer TO Product)
56+
```
57+
58+
The above example shows one edge constraint, with one edge constraint clause. This constraint supports `Customer-(bought)->Product`. That is, inserting a `bought` edge relationship going from a `Customer` to `Product` would be allowed. Inserting any other combination of nodes, like `Supplier-(bought)->Product`, even though it may describe a valid relationship in the real world, would fail.
59+
60+
```sql
61+
CONSTRAINT EC_BOUGHT CONNECTION (Supplier TO Product, Customer TO Product)
62+
```
63+
64+
The above example defines one edge constraint with two edge constraint clauses. These constraint clauses allow the `bought` edge to contain either `Supplier-(bought)->Product` or `Customer-(bought)->Product` relationships. Inserting any other types of edge relationships into the `bought` table would fail.
65+
66+
```sql
67+
CONSTRAINT EC_BOUGHT1 CONNECTION (Supplier TO Product)
68+
CONSTRAINT EC_BOUGHT2 CONNECTION (Customer TO Product)
69+
```
70+
71+
The above example shows two constraints on the same edge table, with each edge constraint specifying one constraint clause. In this situation, SQL would only allow inserts that satisfy BOTH edge constraint clauses simultaneously. This is, of course, impossible. There is no node pair that can satisfy both edge constraint clauses. This edge constraint combination makes the edge table unusable.
72+
73+
For a detailed explanation of where multiple edge constraints can be used in a real-life scenario, please see the example "Creating a new edge constraint on existing edge table, with new edge constraint clause" later in this page.
4274

4375
### Indexes on edge constraints
4476

0 commit comments

Comments
 (0)