|
| 1 | +# SHORTEST_PATH |
| 2 | +SQL Server (starting with SQL Server 2017) and Azure SQL Database now let you create a graph database, to hold your entities and complex many to many relationships. There are several examples on github which demonstrate how the new graph features work. This example shows how you can use SHORTEST_PATH function to write a shortest path or arbitrary length traversal query. This feature is available for public preview with SQL Server 2019 CTP3.1 |
| 3 | + |
| 4 | + |
| 5 | +To demonstrate the functionality, we will be using AdventureWorks2014 as our sample database. |
| 6 | + |
| 7 | +## Contents |
| 8 | +[About this sample](#about-this-sample)<br/> |
| 9 | +[Before you begin](#before-you-begin)<br/> |
| 10 | +[Run this sample](#run-this-sample)<br/> |
| 11 | +[Related links](#related-links) |
| 12 | + |
| 13 | +## About this sample |
| 14 | +1. **Applies to:** |
| 15 | + - SQL Server 2019 CTP3.1 (or higher) |
| 16 | +2. **Demos:** |
| 17 | + - Build and populate graph node and edge tables |
| 18 | + - Use SHORTEST_PATH function to compute shortest path |
| 19 | + - between 2 given nodes. |
| 20 | + - starting from a given node to all other nodes in the graph. |
| 21 | + - starting from a given node to all other nodes, which are 1 to 3 hops away from the start node. |
| 22 | +3. **Workload:** Queries executed on [AdventureWorks2014](https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorks2014.bak) |
| 23 | +4. **Programming Language:** T-SQL |
| 24 | +5. **Author:** Shreya Verma |
| 25 | + |
| 26 | +## Before you begin |
| 27 | +To run these demo scripts, you need the following prerequisites. |
| 28 | + |
| 29 | +**Account and Software prerequisites:** |
| 30 | + |
| 31 | +1. SQL Server 2019 CTP3.1 (or higher) |
| 32 | +2. SQL Server Management Studio 18.x (or higher) |
| 33 | + |
| 34 | +## Run This Sample |
| 35 | + |
| 36 | +### Setup |
| 37 | + |
| 38 | +#### SQL Server Setup |
| 39 | + |
| 40 | +1. Download [**AdventureWorks2014.bak**](https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorks2014.bak) |
| 41 | + |
| 42 | +2. Launch SQL Server Management Studio, connect to your SQL Server instance (2019) and restore **AdventureWorks2014.bak**. This document describes how to [Restore a Database Backup Using SSMS](https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms). |
| 43 | + |
| 44 | + |
| 45 | +#### Running the Sample Scripts |
| 46 | +1. Once the database is restored, run the *setup-BOMGraph.sql* script to create the necessary graph node and edge tables. We will be using these tables to run our sample queries. The setup file creates Product node table and IsPartOf edge table, which represents the BOM for products in AdventureWorks Cycles manufacturing pipeline. |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +2. Run the *ShortestPath.sql* script to run some SHORTEST_PATH queries. The script has following example queries: |
| 52 | + 1. Starting from a given node, find shortest path to all the other nodes in the graph. |
| 53 | + 2. Starting from a given node, find shortest path to all the other nodes in the graph, which are 1 -3 hops away from the start node. |
| 54 | + 3. Find shortest path between 2 given nodes (start and end node) |
| 55 | + |
| 56 | + |
| 57 | +## Related Links |
| 58 | + |
| 59 | +For more information about Graph DB in SQL Server 2017, see these articles: |
| 60 | + |
| 61 | +1. [Graph processing with SQL Server and Azure SQL Database](https://docs.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-overview) |
| 62 | + |
| 63 | +2. [SQL Graph Architecture](https://docs.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-architecture) |
| 64 | + |
| 65 | +3. [Using MATCH in MERGE DML](https://blogs.msdn.microsoft.com/sqlserverstorageengine/2018/07/16/match-support-in-merge-dml-for-graph-tables/) |
| 66 | +4. [Edge Constraints on graph tables](https://blogs.msdn.microsoft.com/sqlserverstorageengine/2018/09/28/public-preview-of-graph-edge-constraints-on-sql-server-2019/) |
| 67 | + |
0 commit comments