Edge tables

Relationships in SQL Server 2017 are represented with edge tables. To create an edge table, use the extended CREATE TABLE statement. Unlike node tables, an edge table can be created without a single property. In this example, you will create the dbo.Follows edge table, which should represent who is followed by whom from the set of users created in the previous section. Assume that the users follow the other users according to the following table:

User

Follows

@MilosSQL

@DejanSarka, @sql_williamd, @tomaz_tsql, @WienerSportklub

@DejanSarka

@MilosSQL, @sql_williamd, @tomaz_tsql, @nkolimpija

@sql_williamd

@DejanSarka, @tomaz_tsql

@tomaz_tsql

@MilosSQL, @DejanSarka, @sql_williamd

@WienerSportklub

@MilosSQL

@nkolimpija

@DejanSarka

In a relational world, this relation would be represented with a junction table, as in the following example:

CREATE TABLE dbo.UserFollows(
UserId BIGINT NOT NULL,
FollowingUserId BIGINT NOT NULL,
CONSTRAINT PK_UserFollows PRIMARY KEY CLUSTERED(
UserId ASC,
FollowingUserId ASC)
);

Use the following code to create foreign keys in this junction table, to ensure data integrity:

ALTER TABLE dbo.UserFollows  WITH CHECK ADD CONSTRAINT FK_UserFollows_TwitterUser1 FOREIGN KEY(UserId) REFERENCES dbo.TwitterUser (UserId);
ALTER TABLE dbo.UserFollows CHECK CONSTRAINT FK_UserFollows_TwitterUser1;
GO
ALTER TABLE dbo.UserFollows WITH CHECK ADD CONSTRAINT FK_UserFollows_TwitterUser2 FOREIGN KEY(FollowingUserId) REFERENCES dbo.TwitterUser (UserId);
ALTER TABLE dbo.UserFollows CHECK CONSTRAINT FK_UserFollows_TwitterUser2;
GO

And here is the INSERT statement that implements the relation from the previous table:

INSERT INTO dbo.UserFollows VALUES (1,2),(1,3),(1,4),(1,5),(2,1),(2,3),(2,4),(2,6),(3,2),(3,4),(4,1),(4,2),(4,3),(5,1);

To create an edge table in SQL Server 2017, you can use the following statement:

CREATE TABLE dbo.Follows AS EDGE;

As you can see, you did not specify anything but the table name! Similar to node tables, whenever an edge table is created, three visible implicit columns are automatically generated:

  • $edge_id: This column uniquely identifies a given edge in the database (along to the $node_id column of a node table)
  • $from_id : It stores the $node_id of the node, from where the edge originates
  • $from_id : It stores the $node_id of the node, at which the edge terminates

In addition to these three, there are five more hidden columns, as shown in the upcoming screenshot. Now, you will enter the same relations that you previously inserted into the relational table, but this time into the edge table. Entries in edge tables connect two nodes. An edge table enables users to model many-to-many relationships in the graph. Unlike a normal table, in an edge table, you need to refer to the $node_id keys rather than to natural business keys. Use the following statement to populate the edge table by using the entries in the relational table:

INSERT INTO dbo.Follows  
SELECT u1.$node_id, u2.$node_id
FROM dbo.UserFollows t
INNER JOIN dbo.TwitterUser u1 ON t.UserId = u1.UserId
INNER JOIN dbo.TwitterUser u2 ON t.FollowingUserId = u2.UserId;

Now, use a simple SELECT statement to check the content of the edge table:

SELECT * FROM dbo.Follows;

Here is the result:

Content of an edge table

In analog to node tables, you can also use short names for the automatically generated columns in an edge table. The following statement also returns the output shown in the preceding screenshot:

SELECT $edge_id, $from_id, $to_id FROM dbo.Follows;

Here is what this edge table looks like in SSMS:

Graph edge table in SQL Server Management Studio

You can also see that an edge table without properties has eight automatically generated columns. It is also recommended that you create an index on the $from_id and $to_id columns for faster lookups in the direction of the edge, especially in a typical OLTP workload:

CREATE CLUSTERED INDEX ixcl ON dbo.Follows($from_id, $to_id);

 In the first release, converting an existing relational table into a node or edge table is not supported. There are many restrictions and limitations in edge tables. You will see them later in the SQL Graph limitations section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.128.113