|
| 1 | +# Derived Tables and Views in Graph Match Queries |
| 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 derived tables and views created on graph node or edge tables in graph match queries. This feature is in public preview on Azure SQL Database and SQL Server 2019 CTP2.1. |
| 3 | + |
| 4 | + |
| 5 | +To demonstrate the functionality, we will be using WideWorldImporters 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 | + - Azure SQL Database v12 (or higher) |
| 16 | + - SQL Server 2019 CTP2.1 (or higher) |
| 17 | +2. **Demos:** |
| 18 | + - Build and populate graph node and edge tables |
| 19 | + - Use derived tables and views in graph match queries. |
| 20 | +3. **Workload:** Queries executed on [WideWorldImporters](https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0) |
| 21 | +4. **Programming Language:** T-SQL |
| 22 | +5. **Author:** Shreya Verma |
| 23 | + |
| 24 | +## Before you begin |
| 25 | +To run these demo scripts, you need the following prerequisites. |
| 26 | + |
| 27 | +**Account and Software prerequisites:** |
| 28 | + |
| 29 | +1. Either |
| 30 | + - Azure SQL Database v12 (or higher) |
| 31 | + - SQL Server 2019 CTP2.1 (or higher) |
| 32 | +2. SQL Server Management Studio 17.x (or higher) |
| 33 | + |
| 34 | +**Azure prerequisites:** |
| 35 | + |
| 36 | +1. An Azure subscription. If you don't already have an Azure subscription, you can get one for free here: [get Azure free trial](https://azure.microsoft.com/en-us/free/) |
| 37 | + |
| 38 | +2. When your Azure subscription is ready to use, you have to create an Azure SQL Database, to do that, you must have completed the first three steps explained in [Design your first Azure SQL database](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-design-first-database) |
| 39 | + |
| 40 | +## Run This Sample |
| 41 | + |
| 42 | +### Setup |
| 43 | +#### Azure SQL Database Setup |
| 44 | + |
| 45 | +1. Download the **WideWorldImporters-Standard.bacpac** from the WideWorldImporters database [page](https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0) |
| 46 | + |
| 47 | +2. Import the **WideWorldImporters-Standard.bacpac** bacpac file to your Azure subscription. [This document](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-import) describes how you can restore a bacpac file to your Azure SQL Database. |
| 48 | + |
| 49 | +3. Launch SQL Server Management Studio and connect to the newly created WideWorldImporters database |
| 50 | + |
| 51 | + |
| 52 | +#### SQL Server Setup |
| 53 | + |
| 54 | +1. Download [**WideWorldImporters-Full.bak**](https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0) |
| 55 | + |
| 56 | +2. Launch SQL Server Management Studio, connect to your SQL Server instance (2017) and restore **WideWorldImporters-Full.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). |
| 57 | + |
| 58 | + |
| 59 | +#### Running the Sample Scripts |
| 60 | +1. Once the database is restored, run the *setup-wwi-graph.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 the following graph schema |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +2. Run the *DerivedTablesAndViewsInGraphMatch.sql* script to create derived tables and views on nodes and edges, that can be then used in graph match queries to generate insights. The script demonstrates: |
| 66 | + |
| 67 | + 1. How to query heterogeneous edges in match query. |
| 68 | + 2. How to query heterogeneous nodes in match query. |
| 69 | + 3. How to query heterogeneous nodes and edges in match query. |
| 70 | + |
| 71 | +## Related Links |
| 72 | + |
| 73 | +For more information about Graph DB in SQL Server 2017, see these articles: |
| 74 | + |
| 75 | +1. [Graph processing with SQL Server and Azure SQL Database](https://docs.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-overview) |
| 76 | + |
| 77 | +2. [SQL Graph Architecture](https://docs.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-architecture) |
| 78 | + |
| 79 | +3. [Using MATCH in MERGE DML](https://blogs.msdn.microsoft.com/sqlserverstorageengine/2018/07/16/match-support-in-merge-dml-for-graph-tables/) |
| 80 | +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/) |
| 81 | + |
0 commit comments