You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] offers graph database capabilities to model many-to-many relationships. The graph relationships are integrated into [!INCLUDE[tsql-md](../../includes/tsql-md.md)] and receive the benefits of using [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] as the foundational database management system.
19
19
20
+
## What is a graph database?
20
21
21
-
## What is a graph database?
22
22
A graph database is a collection of nodes (or vertices) and edges (or relationships). A node represents an entity (for example, a person or an organization) and an edge represents a relationship between the two nodes that it connects (for example, likes or friends). Both nodes and edges may have properties associated with them. Here are some features that make a graph database unique:
23
+
23
24
- Edges or relationships are first class entities in a Graph Database and can have attributes or properties associated with them.
24
25
- A single edge can flexibly connect multiple nodes in a Graph Database.
25
26
- You can express pattern matching and multi-hop navigation queries easily.
@@ -28,55 +29,65 @@ A graph database is a collection of nodes (or vertices) and edges (or relationsh
28
29
## When to use a graph database
29
30
30
31
A relational database can achieve anything a graph database can. However, a graph database makes it easier to express certain kinds of queries. Also, with specific optimizations, certain queries may perform better. Your decision to choose either a relational or graph database is based on following factors:
32
+
31
33
- Your application has hierarchical data. The HierarchyID datatype can be used to implement hierarchies, but it has some limitations. For example, it doesn't allow you to store multiple parents for a node.
32
34
- Your application has complex many-to-many relationships; as application evolves, new relationships are added.
33
35
- You need to analyze interconnected data and relationships.
34
36
35
-
## Graph features introduced in [!INCLUDE[sssql17](../../includes/sssql17-md.md)]
36
-
The following features were introduced in SQL Server 2017.
37
+
## Graph features introduced in [!INCLUDE[sssql17](../../includes/sssql17-md.md)]
38
+
39
+
The following features were introduced in SQL Server 2017.
37
40
38
41
### Create graph objects
42
+
39
43
[!INCLUDE[tsql-md](../../includes/tsql-md.md)] extensions allow users to create node or edge tables. Both nodes and edges can have properties associated to them. Since, nodes and edges are stored as tables, all the operations that are supported on relational tables are supported on node or edge table. Here's an example:
40
44
41
-
```
45
+
```sql
42
46
CREATETABLEPerson (ID INTEGERPRIMARY KEY, Name VARCHAR(100), Age INT) AS NODE;
43
47
CREATETABLEfriends (StartDate date) AS EDGE;
44
48
```
45
49
46
-

47
-
Nodes and Edges are stored as tables
50
+
The following diagram shows how Nodes and Edges are stored as tables.
48
51
49
-
### Query language extensions
50
-
New `MATCH` clause is introduced to support pattern matching and multi-hop navigation through the graph. The `MATCH` function uses ASCII-art style syntax for pattern matching. For example:
52
+
:::image type="content" source="../../relational-databases/graphs/media/person-friends-tables.png" alt-text="Diagram showing the Nodes and Edges are stored as tables.":::
51
53
52
-
```
54
+
### Query language extensions
55
+
56
+
New `MATCH` clause is introduced to support pattern matching and multi-hop navigation through the graph. The `MATCH` function uses ASCII-art style syntax for pattern matching. For example, to find friends of "John":
57
+
58
+
```sql
53
59
-- Find friends of John
54
60
SELECTPerson2.Name
55
61
FROM Person Person1, Friends, Person Person2
56
62
WHERE MATCH(Person1-(Friends)->Person2)
57
63
ANDPerson1.Name='John';
58
-
```
59
-
60
-
### Fully integrated in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] Engine
61
-
Graph extensions are fully integrated in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] engine. Use the same storage engine, metadata, query processor, etc. to store and query graph data. Query across graph and relational data in a single query. Combining graph capabilities with other [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] technologies like Columnstore indexes, HA, R services, etc. SQL graph also supports all the security and compliance features available with [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
64
+
```
65
+
66
+
### Fully integrated in [!INCLUDE [ssde](../../includes/ssdenoversion-md.md)]
67
+
68
+
Graph extensions are fully integrated in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] engine. Use the same storage engine, metadata, query processor, etc. to store and query graph data. Query across graph and relational data in a single query. Combining graph capabilities with other [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] technologies like columnstore indexes, HA, R services, etc. SQL graph also supports all the security and compliance features available with [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
62
69
63
70
### Tooling and ecosystem
64
71
65
72
Benefit from existing tools and ecosystem that [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] offers. Tools like backup and restore, import and export, BCP just work out of the box. Other tools or services like SSIS, SSRS, or Power BI work with graph tables, just the way they work with relational tables.
66
73
67
-
## Edge Constraints
74
+
## Edge constraints
75
+
68
76
An edge constraint is defined on a graph edge table and is a pair of node table(s) that a given edge type can connect. Edge constraints help developers restrict the type of nodes that a given edge can connect.
69
77
70
-
To learn more about how to create and use edge constraints refer to [Edge Constraints](../../relational-databases/tables/graph-edge-constraints.md)
78
+
To learn more about how to create and use edge constraints, refer to [Edge Constraints](../../relational-databases/tables/graph-edge-constraints.md).
79
+
80
+
## Merge DML
71
81
72
-
## Merge DML
73
82
The [MERGE](../../t-sql/statements/merge-transact-sql.md) statement performs insert, update, or delete operations on a target table based on the results of a join with a source table. For example, you can synchronize two tables by inserting, updating, or deleting rows in a target table based on differences between the target table and the source table. Using MATCH predicates in a MERGE statement is now supported on Azure SQL Database and SQL Server vNext. That is, it's now possible to merge your current graph data (node or edge tables) with new data using the MATCH predicates to specify graph relationships in a single statement, instead of separate INSERT/UPDATE/DELETE statements.
74
83
75
-
To learn more about how match can be used in merge DML refer to [MERGE Statement](../../t-sql/statements/merge-transact-sql.md)
84
+
To learn more about how match can be used in merge DML, refer to [MERGE Statement](../../t-sql/statements/merge-transact-sql.md).
85
+
86
+
## Shortest path
87
+
88
+
The [SHORTEST_PATH](./sql-graph-shortest-path.md) function finds shortest path between any two nodes in a graph or starting from a given node to all the other nodes in the graph. `SHORTEST PATH` can also be used to find a transitive closure or for arbitrary length traversals in the graph.
76
89
77
-
## Shortest Path
78
-
The [SHORTEST_PATH](./sql-graph-shortest-path.md) function finds shortest path between any two nodes in a graph or starting from a given node to all the other nodes in the graph. Shortest path can also be used to find a transitive closure or for arbitrary length traversals in the graph.
90
+
## Next steps
79
91
80
-
## Next steps
81
-
Read the [SQL Graph Database - Architecture](./sql-graph-architecture.md)
82
-
To get started with SQL Graph, see [SQL Graph Database - Sample](./sql-graph-sample.md)
92
+
- Read the [SQL Graph Database - Architecture](./sql-graph-architecture.md)
93
+
- To get started with SQL Graph, see [SQL Graph Database - Sample](./sql-graph-sample.md)
Copy file name to clipboardExpand all lines: docs/relational-databases/graphs/sql-graph-sample.md
+93-48Lines changed: 93 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
---
2
2
title: SQL Graph Database Sample
3
-
description: A quick sample that helps you get started with the new syntax introduced in SQL graph database.
3
+
description: "In this tutorial, create a graph database with nodes and edges and then use the new MATCH clause to match some patterns and traverse through the graph."
This sample provides a [!INCLUDE[tsql-md](../../includes/tsql-md.md)] script to create a graph database with nodes and edges and then use the new MATCH clause to match some patterns and traverse through the graph. This sample script works on both Azure SQL Database and [!INCLUDE[sssql17](../../includes/sssql17-md.md)]
19
+
This sample provides a [!INCLUDE[tsql-md](../../includes/tsql-md.md)] script to create a graph database with nodes and edges and then use the new MATCH clause to match some patterns and traverse through the graph. This sample script works on both Azure SQL Database and [!INCLUDE[sssql17](../../includes/sssql17-md.md)] and later versions.
20
20
21
21
## Sample Schema
22
22
23
-
This sample creates a graph schema, as showed in Figure 1, for a hypothetical social network that has People, Restaurant and City nodes. These nodes are connected to each other using Friends, Likes, LivesIn and LocatedIn edges.
23
+
This sample creates a graph schemafor a hypothetical social network that has `People`, `Restaurant` and `City` nodes. These nodes are connected to each other using `Friends`, `Likes`, `LivesIn` and `LocatedIn` edges. The following diagram shows a sample schema with `restaurant`, `city`, `person` nodes and `LivesIn`, `LocatedIn`, `Likes` edges.
24
24
25
-

26
-
27
-
Figure 1: Sample schema with restaurant, city, person nodes and LivesIn, LocatedIn, Likes edges.
25
+
:::image type="content" source="media/sql-graph-sample/person-cities-restaurants-tables.png" alt-text="Diagram showing a sample schema with restaurant, city, person nodes and LivesIn, LocatedIn, Likes edges.":::
28
26
29
27
## Sample Script
30
28
31
-
```
32
-
-- Create a graph demo database
29
+
The following sample script uses the new T-SQL syntax to create node and edge tables. Learn how to insert data into node and edge tables using `INSERT` statement and also shows how to use `MATCH` clause for pattern matching and navigation.
30
+
31
+
This script performs the following steps:
32
+
33
+
1. Create a database named `GraphDemo`.
34
+
1. Create node tables.
35
+
1. Create edge tables.
36
+
37
+
```sql
38
+
-- Create a GraphDemo database
33
39
IF NOT EXISTS (SELECT*FROMsys.databasesWHERE NAME ='graphdemo')
Now, we'll insert data to represent the relationships.
63
72
73
+
1. Insert data into node tables.
74
+
1. Inserting into a node table is same as inserting into a regular table.
75
+
1. Insert data into edge tables, in this case, for which restaurants each person likes into the `likes` edge.
76
+
1. While inserting into an edge table, provide the `$node_id` from `$from_id` and `$to_id` columns.
77
+
1. Insert data into the `livesIn` edge to associate persons with the city where they live.
78
+
1. Insert data into the `locatedIn` edge to associate restaurants with the city where they are located.
79
+
1. Insert data into the `friendOf` edge to associated friends.
80
+
81
+
```sql
64
82
-- Insert data into node tables. Inserting into a node table is same as inserting into a regular table
65
83
INSERT INTO Person (ID, name)
66
-
VALUES (1, 'John')
67
-
, (2, 'Mary')
68
-
, (3, 'Alice')
69
-
, (4, 'Jacob')
70
-
, (5, 'Julie');
84
+
VALUES (1, 'John')
85
+
, (2, 'Mary')
86
+
, (3, 'Alice')
87
+
, (4, 'Jacob')
88
+
, (5, 'Julie');
71
89
72
90
INSERT INTO Restaurant (ID, name, city)
73
-
VALUES (1, 'Taco Dell','Bellevue')
74
-
, (2, 'Ginger and Spice','Seattle')
75
-
, (3, 'Noodle Land', 'Redmond');
91
+
VALUES (1, 'Taco Dell','Bellevue')
92
+
, (2, 'Ginger and Spice','Seattle')
93
+
, (3, 'Noodle Land', 'Redmond');
76
94
77
95
INSERT INTO City (ID, name, stateName)
78
-
VALUES (1,'Bellevue','WA')
79
-
, (2,'Seattle','WA')
80
-
, (3,'Redmond','WA');
96
+
VALUES (1,'Bellevue','WA')
97
+
, (2,'Seattle','WA')
98
+
, (3,'Redmond','WA');
81
99
82
100
-- Insert into edge table. While inserting into an edge table,
83
101
-- you need to provide the $node_id from $from_id and $to_id columns.
84
102
/* Insert which restaurants each person likes */
85
103
INSERT INTO likes
86
-
VALUES ((SELECT $node_id FROM Person WHERE ID = 1), (SELECT $node_id FROM Restaurant WHERE ID = 1), 9)
87
-
, ((SELECT $node_id FROM Person WHERE ID = 2), (SELECT $node_id FROM Restaurant WHERE ID = 2), 9)
88
-
, ((SELECT $node_id FROM Person WHERE ID = 3), (SELECT $node_id FROM Restaurant WHERE ID = 3), 9)
89
-
, ((SELECT $node_id FROM Person WHERE ID = 4), (SELECT $node_id FROM Restaurant WHERE ID = 3), 9)
90
-
, ((SELECT $node_id FROM Person WHERE ID = 5), (SELECT $node_id FROM Restaurant WHERE ID = 3), 9);
104
+
VALUES ((SELECT $node_id FROM Person WHERE ID =1), (SELECT $node_id FROM Restaurant WHERE ID =1), 9)
105
+
, ((SELECT $node_id FROM Person WHERE ID =2), (SELECT $node_id FROM Restaurant WHERE ID =2), 9)
106
+
, ((SELECT $node_id FROM Person WHERE ID =3), (SELECT $node_id FROM Restaurant WHERE ID =3), 9)
107
+
, ((SELECT $node_id FROM Person WHERE ID =4), (SELECT $node_id FROM Restaurant WHERE ID =3), 9)
108
+
, ((SELECT $node_id FROM Person WHERE ID =5), (SELECT $node_id FROM Restaurant WHERE ID =3), 9);
91
109
92
110
/* Associate in which city live each person*/
93
111
INSERT INTO livesIn
94
-
VALUES ((SELECT $node_id FROM Person WHERE ID = 1), (SELECT $node_id FROM City WHERE ID = 1))
95
-
, ((SELECT $node_id FROM Person WHERE ID = 2), (SELECT $node_id FROM City WHERE ID = 2))
96
-
, ((SELECT $node_id FROM Person WHERE ID = 3), (SELECT $node_id FROM City WHERE ID = 3))
97
-
, ((SELECT $node_id FROM Person WHERE ID = 4), (SELECT $node_id FROM City WHERE ID = 3))
98
-
, ((SELECT $node_id FROM Person WHERE ID = 5), (SELECT $node_id FROM City WHERE ID = 1));
112
+
VALUES ((SELECT $node_id FROM Person WHERE ID =1), (SELECT $node_id FROM City WHERE ID =1))
113
+
, ((SELECT $node_id FROM Person WHERE ID =2), (SELECT $node_id FROM City WHERE ID =2))
114
+
, ((SELECT $node_id FROM Person WHERE ID =3), (SELECT $node_id FROM City WHERE ID =3))
115
+
, ((SELECT $node_id FROM Person WHERE ID =4), (SELECT $node_id FROM City WHERE ID =3))
116
+
, ((SELECT $node_id FROM Person WHERE ID =5), (SELECT $node_id FROM City WHERE ID =1));
99
117
100
118
/* Insert data where the restaurants are located */
101
119
INSERT INTO locatedIn
102
-
VALUES ((SELECT $node_id FROM Restaurant WHERE ID = 1), (SELECT $node_id FROM City WHERE ID =1))
103
-
, ((SELECT $node_id FROM Restaurant WHERE ID = 2), (SELECT $node_id FROM City WHERE ID =2))
104
-
, ((SELECT $node_id FROM Restaurant WHERE ID = 3), (SELECT $node_id FROM City WHERE ID =3));
120
+
VALUES ((SELECT $node_id FROM Restaurant WHERE ID =1), (SELECT $node_id FROM City WHERE ID =1))
121
+
, ((SELECT $node_id FROM Restaurant WHERE ID =2), (SELECT $node_id FROM City WHERE ID =2))
122
+
, ((SELECT $node_id FROM Restaurant WHERE ID =3), (SELECT $node_id FROM City WHERE ID =3));
105
123
106
124
/* Insert data into the friendOf edge */
107
125
INSERT INTO friendOf
108
-
VALUES ((SELECT $NODE_ID FROM Person WHERE ID = 1), (SELECT $NODE_ID FROM Person WHERE ID = 2))
109
-
, ((SELECT $NODE_ID FROM Person WHERE ID = 2), (SELECT $NODE_ID FROM Person WHERE ID = 3))
110
-
, ((SELECT $NODE_ID FROM Person WHERE ID = 3), (SELECT $NODE_ID FROM Person WHERE ID = 1))
111
-
, ((SELECT $NODE_ID FROM Person WHERE ID = 4), (SELECT $NODE_ID FROM Person WHERE ID = 2))
112
-
, ((SELECT $NODE_ID FROM Person WHERE ID = 5), (SELECT $NODE_ID FROM Person WHERE ID = 4));
126
+
VALUES ((SELECT $NODE_ID FROM Person WHERE ID =1), (SELECT $NODE_ID FROM Person WHERE ID =2))
127
+
, ((SELECT $NODE_ID FROM Person WHERE ID =2), (SELECT $NODE_ID FROM Person WHERE ID =3))
128
+
, ((SELECT $NODE_ID FROM Person WHERE ID =3), (SELECT $NODE_ID FROM Person WHERE ID =1))
129
+
, ((SELECT $NODE_ID FROM Person WHERE ID =4), (SELECT $NODE_ID FROM Person WHERE ID =2))
130
+
, ((SELECT $NODE_ID FROM Person WHERE ID =5), (SELECT $NODE_ID FROM Person WHERE ID =4));
131
+
```
113
132
133
+
Next, we'll query the data to find insights from the data.
114
134
135
+
1. Use the graph [MATCH](../../t-sql/queries/match-sql-graph.md) function to find which restaurants that John likes.
136
+
1. Finds the restaurants that John's friends like.
137
+
1. Find people who like a restaurant in the same city they live in.
138
+
139
+
```sql
115
140
-- Find Restaurants that John likes
116
141
SELECTRestaurant.name
117
142
FROM Person, likes, Restaurant
@@ -128,7 +153,11 @@ AND person1.name='John';
128
153
SELECTPerson.name
129
154
FROM Person, likes, Restaurant, livesIn, City, locatedIn
130
155
WHERE MATCH (Person-(likes)->Restaurant-(locatedIn)->City AND Person-(livesIn)->City);
156
+
```
131
157
158
+
Finally, a more advanced query finds the friends of friends of friends. This query excludes those cases where the relationship "loops back". For example, Alice is a friend of John; John is a friend of Mary; and Mary in turn is a friend of Alice. This causes a "loop" back to Alice. In many cases, it is necessary to explicitly check for such loops and exclude the results.
159
+
160
+
```sql
132
161
-- Find friends-of-friends-of-friends, excluding those cases where the relationship "loops back".
133
162
-- For example, Alice is a friend of John; John is a friend of Mary; and Mary in turn is a friend of Alice.
134
163
-- This causes a "loop" back to Alice. In many cases, it is necessary to explicitly check for such loops and exclude the results.
@@ -142,9 +171,10 @@ AND Person.name != Person4.name;
142
171
```
143
172
144
173
## Clean Up
145
-
Clean up the schema and database created for the sample.
146
174
147
-
```
175
+
Clean up the schema and database created for the sample in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
176
+
177
+
```sql
148
178
USE graphdemo;
149
179
go
150
180
@@ -162,11 +192,26 @@ DROP DATABASE graphdemo;
162
192
go
163
193
```
164
194
165
-
## Script explanation
166
-
This script uses the new T-SQL syntax to create node and edge tables. Shows how to insert data into node and edge tables using `INSERT` statement and also shows how to use `MATCH` clause for pattern matching and navigation.
195
+
Clean up the schema and database created for the sample in [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)].
196
+
197
+
```sql
198
+
--Connect to the graphdemo database
199
+
DROPTABLE IF EXISTS likes;
200
+
DROPTABLE IF EXISTS Person;
201
+
DROPTABLE IF EXISTS Restaurant;
202
+
DROPTABLE IF EXISTS City;
203
+
DROPTABLE IF EXISTS friendOf;
204
+
DROPTABLE IF EXISTS livesIn;
205
+
DROPTABLE IF EXISTS locatedIn;
206
+
207
+
--Connect to the master database
208
+
DROPDATABASE graphdemo;
209
+
go
210
+
```
211
+
212
+
## Next steps
167
213
168
-
|Command |Notes
169
-
|--- |--- |
170
-
|[CREATE TABLE (Transact-SQL)](../../t-sql/statements/create-table-sql-graph.md)|Create graph node or edge table |
171
-
|[INSERT (Transact-SQL)](../../t-sql/statements/insert-sql-graph.md)|Insert into a node or edge table |
172
-
|[MATCH (Transact-SQL)](../../t-sql/queries/match-sql-graph.md)|Use MATCH to match a pattern or traverse through the graph |
0 commit comments