Skip to content

Commit ce57e76

Browse files
committed
Adding sample for derived tables and views in graph match query feature
1 parent 8b7658d commit ce57e76

4 files changed

Lines changed: 611 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
drop view if exists OperatesIn, CUstomer, Novelty_supplier, Novelty_customer
2+
go
3+
4+
----------------------------------------------------------------------------
5+
-- Querying heterogeneous edges
6+
----------------------------------------------------------------------------
7+
CREATE VIEW OperatesIn AS
8+
SELECT *, 'located' AS relation FROM locatedIn
9+
UNION ALL
10+
SELECT *, 'delivery' FROM deliveryIn
11+
GO
12+
13+
SELECT SupplierID, SupplierName, PhoneNumber, relation
14+
FROM Supplier,
15+
City,
16+
OperatesIn
17+
WHERE MATCH(Supplier-(OperatesIn)->City)
18+
AND City.CityName = 'San Francisco'
19+
20+
----------------------------------------------------------------------------
21+
-- Querying heterogeneous nodes
22+
----------------------------------------------------------------------------
23+
24+
CREATE VIEW Customer AS
25+
SELECT SupplierID AS ID,
26+
SupplierName AS NAME,
27+
SupplierCategory AS CATEGORY
28+
FROM Supplier
29+
UNION ALL
30+
SELECT CustomerID,
31+
CustomerName,
32+
CustomerCategory
33+
FROM Customers
34+
GO
35+
36+
37+
SELECT Customer.ID, Customer.NAME, Customer.CATEGORY
38+
FROM Customer,
39+
City,
40+
locatedIn
41+
WHERE MATCH(Customer-(locatedIn)->City)
42+
AND City.CityName = 'San Francisco'
43+
44+
SELECT Customer.ID, Customer.NAME, Customer.CATEGORY
45+
FROM Customer,
46+
City,
47+
OperatesIn
48+
WHERE MATCH(Customer-(OperatesIn)->City)
49+
AND City.CityName = 'San Francisco'
50+
51+
52+
----------------------------------------------------------------------------
53+
-- Querying heterogeneous nodes and edges
54+
----------------------------------------------------------------------------
55+
56+
57+
CREATE VIEW Novelty_Supplier AS
58+
SELECT SupplierID,
59+
SupplierName ,
60+
SupplierCategory ,
61+
ValidTo
62+
FROM Supplier
63+
WHERE SupplierCategory LIKE '%Novelty%' OR SupplierCategory LIKE '%Toy%'
64+
GO
65+
66+
CREATE VIEW Novelty_Customer AS
67+
SELECT CustomerID,
68+
CustomerName,
69+
CustomerCategory,
70+
ValidTo
71+
FROM Customers
72+
WHERE CustomerCategory LIKE '%Novelty%' OR CustomerCategory LIKE '%Gift%'
73+
GO
74+
75+
SELECT Name, ID, Category
76+
FROM
77+
(SELECT SupplierID AS ID, SupplierName AS Name,
78+
SupplierCategory AS Category, ValidTo
79+
FROM Novelty_Supplier WHERE ValidTo > getdate()
80+
UNION ALL
81+
SELECT CustomerID, CustomerName, CustomerCategory, ValidTo
82+
FROM Novelty_Customer WHERE ValidTo > getdate()) AS NoveltyCust,
83+
StockItems,
84+
bought,
85+
OperatesIn,
86+
City
87+
WHERE MATCH(City<-(OperatesIn)-NoveltyCust-(bought)->Stockitems)
88+
AND StockItemName = 'White chocolate snow balls 250g'
89+
AND city.cityname = 'San Francisco'
90+
GO
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
![DerivedTablesAndViewsInGraphMatch Schema](graph-layout.png)
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+
28.3 KB
Loading

0 commit comments

Comments
 (0)