Lesson 1 of 2 Graph Data

Modelling Data with Node and Edge Tables

This lesson explains SQL graph node and edge tables, edge constraints and when a graph model is better than junction tables.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe node and edge tables and their pseudo-columns.
  • Explain how edge constraints restrict which nodes an edge connects.
  • Choose between graph tables and relational junction tables.

Nodes and edges

A node table (CREATE TABLE ... AS NODE) represents an entity such as a customer or product. The engine adds a hidden $node_id column, a JSON value that identifies the node, and a unique index on it is recommended. An edge table (AS EDGE) represents a relationship and has $edge_id, $from_id and $to_id columns that point to the connected nodes; it can have its own attributes, such as a rating or a date. Edges are directional, from one node to another, and one edge table can connect several node types.

CREATE TABLE graph.Customer (CustomerID int PRIMARY KEY, Name nvarchar(100)) AS NODE;
CREATE TABLE graph.Product  (ProductID  int PRIMARY KEY, Name nvarchar(100)) AS NODE;

CREATE TABLE graph.Bought
(
    PurchasedOn date,
    Rating      tinyint,
    CONSTRAINT EC_Bought CONNECTION (graph.Customer TO graph.Product) ON DELETE CASCADE
) AS EDGE;

INSERT INTO graph.Bought ($from_id, $to_id, PurchasedOn, Rating)
SELECT c.$node_id, p.$node_id, '2026-05-01', 5
FROM graph.Customer AS c, graph.Product AS p
WHERE c.CustomerID = 1 AND p.ProductID = 42;

The following diagram shows node tables connected by directional edges.

Node and edge tables. A node table represents an entity, such as a customer or product, and has a hidden $node_id. An edge table represents a directional relationship from one node to another, with $edge_id, $from_id and $to_id columns, and can carry its own attributes, such as a purchase date and rating. MATCH patterns then follow the edges in queries.
Edges are rows with their own attributes.
Sort into groups

Node or edge?

Exam skills 1.1.2

Decide whether each item belongs in a node table or an edge table.

Log in to try this activity Interactive activities are free with an account.

Edge constraints and indexing

An edge constraint (CONNECTION (A TO B)) restricts which node types an edge can connect, which prevents, for example, an edge from a product to a customer. With ON DELETE CASCADE, deleting a node deletes its edges; otherwise deleting a node that has edges fails. Indexes on ($from_id, $to_id) and ($to_id, $from_id) speed up traversals in both directions.

When to use graph tables

Graph tables and ordinary relational tables can express the same data, so the choice depends on the queries. The following comparison summarizes the trade-off.

Relational tables with join tables
Many-to-many relationshipsA junction table per relationship
Multi-hop queriesChains of joins or recursive CTEs
Fixed, simple relationshipsSimple and efficient
Choose when Hierarchies and fixed schemas
Graph node and edge tables
Many-to-many relationshipsEdge tables, which can connect several node types
Multi-hop queriesMATCH patterns and SHORTEST_PATH
Fixed, simple relationshipsAdds unnecessary complexity
Choose when Networks, recommendations and paths of unknown length
Scenario questions

Graph or relational?

Exam skills 1.1.2

Choose the model that fits each requirement.

Log in to try this activity Interactive activities are free with an account.
Worked example

Modelling follows and purchases

A social shopping app records which users follow other users and which users bought which products, and it must recommend products bought by people a user follows, up to three hops away. Each relationship must only connect valid node types. How should the data be modelled?

Create User and Product as node tables, a Follows edge table with an edge constraint CONNECTION (User TO User), and a Bought edge table with CONNECTION (User TO Product). Recommendations are then MATCH patterns, and paths of up to three hops use SHORTEST_PATH with a quantifier. Junction tables would work, but multi-hop recommendations would need recursive CTEs, and without edge constraints an invalid relationship such as a product following a user could be inserted.

Check yourself

What does ON DELETE CASCADE in an edge constraint do?

Show answer

Deleting a node also deletes the edges connected to it; without it, deleting a node that has edges fails.

Exam Tips

Remember for the exam

  • Entities: AS NODE; relationships: AS EDGE with $from_id and $to_id.
  • Restrict which nodes an edge connects: an edge constraint CONNECTION (A TO B).
  • Delete edges with their nodes: ON DELETE CASCADE in the edge constraint.
  • Index ($from_id, $to_id) and the reverse for traversals.