Get Practical Graph Structures in SQL Server and Azure SQL: Enabling Deeper Insights Using Highly Connected Data 1st Edition Louis Davidson free all chapters
Get Practical Graph Structures in SQL Server and Azure SQL: Enabling Deeper Insights Using Highly Connected Data 1st Edition Louis Davidson free all chapters
https://ebookmass.com
https://ebookmass.com/product/getting-started-with-sql-and-databases-
managing-and-manipulating-data-with-sql-1st-edition-mark-simon/
testbankdeal.com
https://ebookmass.com/product/sql-server-2022-administration-inside-
out-1st-edition-randolph-west/
testbankdeal.com
Louis Davidson
Practical Graph Structures in SQL Server and Azure SQL: Enabling Deeper Insights
Using Highly Connected Data
Louis Davidson
Cleveland, TN, USA
Preface��������������������������������������������������������������������������������������������������������������������xv
v
Table of Contents
vi
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 235
vii
About the Author
Louis Davidson has been working with databases for
more than 25 years as a corporate database developer
and architect. He is now the editor for the Redgate Simple
Talk website. He has been a Microsoft MVP for 18 years. In
addition to this book on graphs in SQL Server, he has written
six editions of his general-purpose SQL Server database
design book (Apress) and has worked on multiple other
book projects over the years.
Louis has been active in speaking about database design and implementation at many
conferences over the past 25 years, including SQL PASS, SQL Rally, SQL Saturday events,
CA World, Music City Data, and the devLink Technical Conference. He has a bachelor’s
degree in computer science from the University of Tennessee at Chattanooga. For more
information, please visit his website at drsql.org.
ix
About the Technical Reviewer
Kathi Kellenberger is a Customer Success Engineer at
Redgate and a Data Platform MVP. She has worked with
SQL Server since 1998 and has authored, co-authored, or
tech reviewed over 20 technical books. Kathi is a longtime
volunteer at LaunchCode in St. Louis where she has taught
T-SQL in the LaunchCode Women + program. When Kathi
isn’t working, she enjoys spending time with family and
friends, cycling, singing, and climbing the stairs of tall
buildings.
xi
Visit https://ebookmass.com
now to explore a rich
collection of eBooks and enjoy
exciting offers!
Acknowledgments
My wife, for suffering through yet another long and painful book writing process.
The Microsoft MVP Program for all the connections it has provided me to meet
wonderful, bizarrely smart people for 18 years. And my lead, Rie Merritt, for all she has
done for me through the years; amazing how we met so many years ago. Just meeting
Bob Ward and Conor Cunningham is thrill enough, and there have been hundreds
besides them.
Shreya Verma and Arvind Shyamsundar for their help over the past few years as I
attempted to do crazy things with the graph objects on my local personal computer.
Dr. David Rosenstein, who first got me interested in graphs in relational structures so
many years ago. Paul Nielsen for the talks we had on graph structures.
Kathi Kellenberger for tech editing this book. I appreciate the hard work!
All the doctors/medical professionals who kept me alive and kicking these past few
years. (If you want to know more, just ask, if you have a while to talk. There is a reason
this book took me over two years to write.)
My coworkers at CBN whom I left during the writing of this book. I hope the graph
objects I left you with are serving you nicely.
My new coworkers at Redgate. I have been a friend of Redgate (as well as a member
of Friends of Redgate) for many years, writing for them. My new manager and teammates
have been nothing but awesome.
Amber Davis, for giving me the chance to be a Dollywood Insider. Obviously being
a Dollywood insider has little to do with this book, but I just wanted to say it again
(which she may never see!) as I am writing the acknowledgements in the Dollywood
Dreammore lobby. I learned a lot from her that has been useful writing some of this book
and in my new job at Redgate as Simple-Talk editor.
xiii
Preface
I started working on my first book 23 years ago. It was on relational database design.
I had learned a little about graphs at that point from a class I took by Dr. David
Rozenshtein (his 1997 book The Essence of SQL : A Guide to Learning Most of SQL in the
Least Amount of Time was essential). His class was very influential and taught me a lot
about how to think about SQL problems. This was a really long time ago (clearly), but
in that same class, one of the sections was on trees in SQL Server. I was hooked on the
subject.
In my 2012 edition of my database design book, I started to include hierarchies
as one of the topics. In my latest book, that came out after SQL Server 2019 arrived, I
promised a book of SQL Graph. This book in your hands is the answer to that challenge.
This is my first programming-based book in many years. Usually I am more
interested in helping you shape a design, but in this case, I want to show you the
mechanics of building a graph database solution using SQL Server and leave it more
to you to decide what to do with it from there. Part of this is due to the newness and
relative complexity of the topic, but also because graphs are meant to be very flexible
structures…way more flexible than the standard relational databases.
The chapters of the book are as follows:
• Chapter 2: How graphs are implemented and the algorithms that are
used to process them. While the basic structure of the graph is really
simple, there is some value to understanding how graph structures
are built in coding and having some idea of what you will see in the
rest of the book.
xv
Preface
• Chapter 3: The syntax that Microsoft has implemented for use with
graph data stored in SQL Server tables is similar to what you probably
already know from working with relational tables, but it is so much
more. In this chapter, I teach you how to use the syntax provided by
Microsoft to query graph tables in interesting ways.
xvi
Preface
xvii
CHAPTER 1
Introduction to Graphs
With great power comes great responsibility.
--Voltaire (and Spider-Man’s Uncle Ben)
At its core, a graph data structure is simply a model representing the connection of one
thing to one other thing and then those things to even more other things. Essentially a
graph database formalizes a database based on many-to-many relationships as the core
relationship.
Everyone who has ever modeled a relational database will either have modeled
a graph purposefully or inadvertently because many-to-many relationships begin to
appear as you add more concepts to your data model. The vast difference you will see is
that relational implementations, ideally, are rigidly modeled.
The term frequently used to decide whether to model a many-to-many relationship
in a relational database or a graph structure is whether the data is highly connected.
With a graph database structure, you can connect many tables to many other tables
and query that structure in a different (yet quite similar) manner to how you query a
relational database.
There are many examples of graphs, but some of the most common examples you
probably hear about are
• Social networks, where you record the relationship from one person
to another
1
© Louis Davidson 2023
L. Davidson, Practical Graph Structures in SQL Server and Azure SQL,
https://doi.org/10.1007/978-1-4842-9459-8_1
Chapter 1 Introduction to Graphs
With this information, you can discover interesting things about an object based on
its relationships and similarity to relationships other entities have.
Possibly the most famous graph that most people will have heard of is the basis of the
parlor game “Six Degrees of Kevin Bacon.” This game suggests that everyone is no more
than six connections away from anyone else, including Kevin Bacon (and not just that
your favorite breakfast meat is bacon.)
The fact that graphs are very natural data structures does not make them easy
to work with. Because they are less rigid data structures than relational tables, the
flexibility makes them quite complex to work with (certainly in the database engine).
Graph objects can represent a very messy reality, and the more complex the data you
are modeling, the more complicated it is to work with. Of course, the reward for that
complexity is the solutions you create are often quite tremendous.
Like many computer science topics, graphs are also a fundamental mathematical
concept. Some of the insights you can get from the math of graphs will help once we
start analyzing data. So, to begin this book on graphs, I will provide a very cursory look
at some mathematical concepts as I define what a graph is and how it can be used in
software.
Note that the concepts in this chapter were gleaned from many resources and are
generally common knowledge. However, when I was searching for resources to read
for an introduction to the concepts of graphs, including mathematical proofs (none of
which will come up in this book, so no worries!) of such concepts, one was Introduction
to Graph Theory by Richard J. Trudeau from Dover Publications in 1993. It is far deeper
than this chapter and definitely more than you need to learn to code with SQL Server’s
graph database extensions, and mathematical proofs have always been my Kryptonite!
However, I highly recommend this as a place to ascend to the next level beyond what I
will show you.
This chapter will be a quick, high-level overview of the many topics you will find
in this book and many other sources, plus ideas you may wish to use in the graph table
objects I will cover once you start building objects. The book itself will be mostly focused
on very practical applications and how to write certain types of code, but I found that
knowing some of the graph fundamentals really helped me to envision what I was
attempting to accomplish.
2
Chapter 1 Introduction to Graphs
Graph Fundamentals
While most readers will have the explicit goal of applying a graph to a specific problem
they are trying to solve, it is best to start at the beginning and discuss what a graph is
in the pure sense. (If you don’t care, you can skip to Chapter 3 where the T-SQL code
starts!) Going through the fundamentals will help you free your mind from preconceived
notions. Perhaps, more importantly, you can ignore the limitations of the tools and
specific problem sets you may wish to solve and just think about the whole problem set.
In math, there are the concepts of “pure” math and “applied” math. Pure math is
math for math’s sake. It is there to ask, “what can be done with a construct?” without
asking the limiting questions of, “is this helpful code to solve my problem or, even more
importantly, any problem?” Applied math is more or less the sort of thing we typical
computer architects/programmers are typically interested in since we have a problem
and want to find something to help us with that problem (and immediately so). Most
of the time, solutions are only interesting if they can solve the specific problems we
currently know about and have a manager riding on our back.
However, I always prefer to start just trying to see what I can accomplish with a new
tool before getting my hands dirty (metaphorically, of course; as a programmer, my
hands never get dirty at work unless the day’s snack includes chocolate).
Some of the things I will discuss will then be familiar in understanding what
we might do when looking for patterns in the data and eventually translating into
algorithms. Some designs may not be realistically possible due to current computing
limitations in SQL Server (or any graph database platform) or reasonable hardware
limitations at the time of writing in 2023. Having written my first book in 2000, it
astounds me how different this statement feels to me 23 years later sitting here with a
computer on my desk that has more power that medium to large corporations were
running on when I first wrote T-SQL.
In one of my sample databases you can download, I have millions of rows in just one
table, and I can process reasonable queries on my desktop computer in mere minutes.
Limitations always exist, but there are fewer and fewer limitations for every generation of
computer architecture that passes.
My goal here in this first chapter (and to a large extent, the second chapter) is to
simply introduce some of the terms and concepts around graphs to help you understand
how graphs are shaped and, eventually, processed.
3
Chapter 1 Introduction to Graphs
Definition
Graphs are based on two primary data structures: nodes (or in math terms, vertices),
and edges. Nodes represent a thing that one might care about, much like a table in a
relational database. Edges establish a connection between exactly one or two nodes
(when the node count is one, it means the node is related to itself.) A graph is defined as
being a set of nodes and a set of edges.
In a graph database, a node is like most any table with attributes describing what
the node represents. The edge is analogous to a many-to-many relationship table with
at least attributes to represent the node that the relationship is from and which it is
to. You will see that two major things set these new concepts apart from a relational
implementation.
First, the from and the to in an edge generally can be from any node object. Whereas
a relational table column used to reference another table value communicates that it is
a foreign key from Table X and nothing else, the from and to attributes of an edge can be
from multiple different node types if you so desire.
Tip I am fully aware that you can put any value into a column, so every foreign
key value in a column needn’t come from the same table. But that is not how it
should be done because data where a column can mean multiple things is very
confusing in a relational table. Graph structures work very similar to how relational
tables work. Still, they have special properties that allow two rows to contain data
from multiple sources without confusing the user/engine.
Second, to make use of these flexible structures, a special set of operations is enabled in
a graph engine, allowing easier access to the meaning of the relationships than we can get
using SQL. More detail on these operations and the basic algorithms are in Chapters 2 and 3.
For the first example, consider the simple graph represented in Figure 1-1—two
nodes connected by a single edge. If the type of node is important, it will be indicated
using different shapes.
4
Chapter 1 Introduction to Graphs
When diagraming a graph, edges may also have a property of direction. The direction
indicates that the relationship is not reciprocal. For example, consider Figure 1-2.
In this graph, N1 connects to N2, but N2 is not connected to N1. This is analogous to
a social media relationship such as when I follow Paul McCartney, but Paul McCartney
doesn’t follow me! It will often be very important to understand the value of the direction
because of the unbalanced nature of some relationships. Mr. McCartney doesn’t all of
the sudden wish to get any communication about where I am speaking at a conference,
but I wish to be informed of his concerts and new music being released.
In SQL Graph, and other graph products, edges are almost always directed. This will
become important later but will only complicate our discussion of graphs concepts in
general, so to start with, let’s ignore the directedness of the edges.
In my first example graph in Figure 1-1, I have two nodes and a single edge. This is a
very simple graph, but it is not the simplest graph. The simplest graph is a graph with 0
edges and 0 nodes, otherwise referred to as the NULL graph. You can also have a graph
with a single node and no edges, and a single node with a single edge, and so on. (You
can’t have an edge without a node.)
Just like modeling a relational database, there is an inherent danger in thinking of a
graph as a drawing and not a complex data structure. Consider a graph with the nodes
and edges seen in Figure 1-3.
5
Visit https://ebookmass.com
now to explore a rich
collection of eBooks and enjoy
exciting offers!
Chapter 1 Introduction to Graphs
This graph could be expressed as sets of nodes and edges in the following manner:
Nodes: {N1,N2,N3,N4}; Edges: {E1:{N1,N2}; E2:{N2,N4}; E3:{N3,N4}; E4:{N1,N3}}
By definition, all nodes and edges must be distinct, much like a table requires
distinct rows. And just like a table, the order of the items in the sets has no importance.
The following graph is the same as diagrammed and as defined prior to this paragraph:
Nodes: {N2,N3,N1,N4}; Edges: {E2:{N2,N4}; E1:{N1,N2}; E4:{N1,N3}; E3:{N3,N4}}
When drawing a graph as an image, there is a very similar consideration. Every
programmer with any skill has frequented a white board to draw a diagram of a
structure. If you were to draw a shape of a circle, a triangle, and a square on the white
board, instinctively you would believe them to be different things. Even if you drew a
rectangle, square, and a rhombus (all shapes with four sides) you would not think they
were the same.
But, in graph terms (just like when diagramming tables), the two graphs in Figure 1-4
are the same as each other and the graph in Figure 1-3, in which they are considered
“equal.”
6
Chapter 1 Introduction to Graphs
The two graph diagrams in Figure 1-4 are copies of the same graph. The next concept
is related, in that we will look at graphs with the same shape but different nodes. When
a graph has the same node and edge shape (meaning the same nodes and edges, not
diagram shape,) the graphs are referred to as being isomorphic graphs. For example,
the two graphs in Figure 1-5 are not equal but they are isomorphic because they have the
same shape in their set of data (regardless of whether you draw them as a square or not).
This concept of isomorphism will be, if not actually referred to using the exact term,
interesting in your usage of graphs on occasion. Consider the set of nodes in Figure 1-6.
7
Random documents with unrelated
content Scribd suggests to you:
[38] Defrauding errand boys, or porters, of their load, by false
pretences of various kinds.
[39] Obtaining money from charitable persons, by some
fictitious statement of distress.
[40] Obtaining goods from a tradesman by false pretences, or
by a forged order in writing.
[41] Throwing snuff in the eyes of a shopkeeper, and then
running off with such money or valuable property as may lay
within reach.
[42] See the evidence of William Alderman.
[43] A short confinement is here meant.
[44] The settlement or camp at Hawkesbury, now called, the
town of Windsor.
[45] Governor King was a post-captain in the navy, and
principal commander of the Buffalo, having a second (acting)
captain under him. The Buffalo had been stationed in N.S.W. ever
since the year 1802, and was now relieved by the Porpoise, in
which ship Governor Bligh arrived.
[46] Now Surveyor-General of New South Wales.
MEMOIRS
OF
JAMES HARDY VAUX.
WRITTEN BY HIMSELF.
IN TWO VOLUMES.
VOL. II.
LONDON:
PRINTED BY W. CLOWES, NORTHUMBERLAND-COURT, STRAND.
AND SOLD BY
ALL RESPECTABLE BOOKSELLERS.
1819.
CONTENTS
OF
CHAPTER I.
I arrive in London—A sudden alarm—Visit my mother and sisters—
Set out for S⸺shire—Interview with my grandfather—Return
to town—A lucky hit on the road—Obtain a situation in the
Crown-Office, page 1.
CHAP. II.
Quit the Crown-Office, and engage as reader in a printing-office—
Determine to live a strictly honest life—Meet with an old
acquaintance who laughs me out of my resolution—Give up all
thoughts of servitude, and become a professed thief, page 13.
CHAP. III.
Various modes of obtaining money—My regular course of life, when
disengaged from my vicious companions—Meet with an
amiable girl, like myself the child of misfortune—We cohabit
together—Our mutual happiness, page 20.
CHAP. IV.
Adventures in the course of my profligate career—Motives which
induce me to marry my companion—Her exemplary behaviour
—A family misfortune, page 29.
CHAP. V.
Adventure of the silver snuff-box—Its consequences.—My narrow
escape from transportation, which I have since had reason to
regret, page 35.
CHAP. VI.
Visit Mr. Bilger, an eminent jeweller—His politeness, and the return
I made for it—Perfidy of a pawnbroker—Obliged to decamp
with precipitation, page 52.
CHAP. VII.
Take a house in St. George’s Fields—Stay at home for several
weeks—At length I venture out in quest of money—My
imprudent obstinacy in entering a house of ill repute, against
the advice and entreaties of my wife—I am taken in custody
and carried to the watch-house.—Distress of my wife on the
occasion, page 71.
CHAP. VIII.
Discover that I have been betrayed—Examined at Bow-street, and
committed for trial—Sent to Newgate—Prepare for my defence
—My trial and conviction, page 83.
CHAP. IX.
Account of my companion and fellow-sufferer in the condemned
cells—His unhappy fate—I receive sentence of death—Am
reprieved, and soon afterwards sent on board the hulks—Some
account of those receptacles of human misery, page 97.
CHAP. X.
I embark a second time for New South Wales—Indulgently treated
by the Captain—My employment during the voyage—Arrive at
Port Jackson, after an absence of four years—My reception
from Governor Macquarrie—Assigned by lot to a settler—His
brutal treatment of me—I find means to quit his service, and
return to Sydney, page 113.
CHAP. XI.
Appointed an overseer—Determine to reform my life, and become a
new man—All my good intentions rendered unavailing by an
unforeseen and unavoidable misfortune—I become a victim to
prejudice, and the depravity of a youth in years, but a veteran
in iniquity—I am banished to the coal-river, page 122.
CHAP. XII.
Return to head-quarters, after an exile of two years—Renew my
vows of rectitude, to which I strictly adhere—Proposal made
me to obtain my liberty—I make the attempt—Its failure, and
the consequent punishment inflicted on me—Conclusion, page
137.
MEMOIRS
OF
JAMES HARDY VAUX.
CHAPTER I.
I arrive in London.—A sudden Alarm.—Visit my Mother and Sisters. Set out for S
⸺shire.—Interview with my Grandfather.—Return to Town.—A lucky Hit on
the Road.—Obtain a Situation in the Crown-Office.