Professional Documents
Culture Documents
By
Gregory Larsen
2011/11/16
Presented by
Alex Albergo
The Series
• This article is part of the Stairway Series: Stairway to T-SQL DML
• The Stairway Series will provide you with a basic understanding of how to
work with data from SQL Server tables, using SQL Server’s Transact-SQL
(T-SQL) dialect. DML is the Data Manipulation Language, and is the aspect
of the language dealing with the data. It includes the statements SELECT,
INSERT, UPDATE and DELETE. This Stairway will as also provide some
history of the SQL language and some general concepts about set theory.
Each level will build upon the prior level, so by the time you are finished you
will have a good understanding of how to select and modify data from SQL
Server.
• A relational database contains tables that relate to each other by key
values. When querying data from these related tables you may choose to
select data from a single table or many tables. If you select data from many
tables, you normally join those tables together using specified join criteria.
The concepts of selecting data from tables and joining tables together is all
about managing and manipulating sets of data. In Level 4 of this Stairway I
will be exploring the concepts of set theory and mathematical operators to
join, merge, and return data from multiple SQL Server tables.
What is a table really?
• A table is just a bunch of rows that contain
values in one or more columns.
• A table can be thought of as a set containing
zero, one, or many records.
– Each row in a table is considered a member of the
table set.
– The concept of sets and members of sets are the
basic elements of set theory.
– By performing set operations, such as intersection
and union against different sets (or against different
tables in a SQL Server database) we are able to
retrieve the data we need
Basic Set Theory
• In the late 1800’s Georg Cantor formally defined a set. He defined a
set as “any collection into a whole M of definite and separate objects
m.”, where the separate objects m are elements of the set M. .
• Cantor’s work eventually grew into what we now called the set
theory.
• Edgar Codd used the concept of set theory to explain the
relationships between data in database tables, which formed the
basis of our all familiar relational database.
• The relational database concept that Edgar Codd developed was
just a projection of the mathematical concepts of set theory for
retrieving data stored in a database.
• By better understanding set theory, we can better understand and
manipulate our data that is stored in our relational databases
Basic Set Theory continued
• In addition to a set, there is also the concept of a
subset.
• A subset is just a collection of members of a set
where the number of members in the subset is
less than the number members of the whole set.
• A subset can be thought of as a partial list of
members of a set.
• In SQL Server, a subset of a table would be a
set of rows, where the total number of rows in
the subset has fewer rows than the original table
Basic Set Theory continued
• To retrieve data for your application you are
typically doing more than just retrieving a set of
records from a single SQL Server table.
• You will commonly need to join one set with
another set, and then finally use some parts of
that joined set to support the data that your
application might need.
• To explore the idea of joining sets together a
little more I will be looking at a concept known as
Venn diagrams.
Venn Diagrams
• A Venn diagram is a graphical representation of
how two or more sets intersect each other. A
Venn diagram makes it easy to visualize and
describe the different parts of intersecting sets.
Alex Albergo
Director of programming
Passport Software, Inc.