Validation issues in edge tables

In SQL Server 2017, it is not possible to define constraints on the edge table to restrict it from connecting any two types of nodes. That is, an edge can connect any two nodes in the graph, regardless of their types. Therefore, you can have relational duplicates in the table and thus, for instance, let the user follow him/herself. In a relational table, you can create a constraint and prevent this, as shown in the following code:

ALTER TABLE dbo.UserFollows ADD CONSTRAINT CHK_UserFollows CHECK (UserId <> FollowingUserId); 

Now, when you try to insert an entry that violates the constraints, as shown in the following code:

INSERT INTO dbo.UserFollows VALUES (1, 1);

You will be welcomed with the following error ...

Get SQL Server 2017 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.