Today. Relations. n-ary relations. Binary relations and properties Relationship to functions. Definitions CS application: Relational DBMS

Similar documents
Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Lecture 16 : Relations and Functions DRAFT

Arkansas Tech University MATH 4033: Elementary Modern Algebra Dr. Marcel B. Finan

3. Relational Model and Relational Algebra

So let us begin our quest to find the holy grail of real analysis.

Relational Databases

Relational Database: Additional Operations on Relations; SQL

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Lecture 17 : Equivalence and Order Relations DRAFT

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Relational Algebra and SQL

A Primer on Index Notation

Basic Concepts of Set Theory, Functions and Relations

Notes on Algebraic Structures. Peter J. Cameron

Elements of Abstract Group Theory

Abstract Algebra Cheat Sheet

Mathematics for Computer Science/Software Engineering. Notes for the course MSM1F3 Dr. R. A. Wilson

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

A Little Set Theory (Never Hurt Anybody)

Relational Algebra. Basic Operations Algebra of Bags

MATH 304 Linear Algebra Lecture 9: Subspaces of vector spaces (continued). Span. Spanning set.

CS 3719 (Theory of Computation and Algorithms) Lecture 4

Copy in your notebook: Add an example of each term with the symbols used in algebra 2 if there are any.

Limitations of E-R Designs. Relational Normalization Theory. Redundancy and Other Problems. Redundancy. Anomalies. Example

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

1 Introduction to Matrices

4. FIRST STEPS IN THE THEORY 4.1. A

SQL Tables, Keys, Views, Indexes

3. Mathematical Induction

Solution to Homework 2

Data Mining: Algorithms and Applications Matrix Math Review

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

MATH APPLIED MATRIX THEORY

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

S on n elements. A good way to think about permutations is the following. Consider the A = 1,2,3, 4 whose elements we permute with the P =

The Relational Data Model: Structure

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

INTRODUCTORY SET THEORY

Basic Set Theory. 1. Motivation. Fido Sue. Fred Aristotle Bob. LX Semantics I September 11, 2008

MAT188H1S Lec0101 Burbulla

7.1 RELATIONS AND THEIR PROPERTIES

Section Inner Products and Norms

Matrix Algebra. Some Basic Matrix Laws. Before reading the text or the following notes glance at the following list of basic matrix algebra laws.

6.3 Conditional Probability and Independence

Math 312 Homework 1 Solutions

Solutions to In-Class Problems Week 4, Mon.

INCIDENCE-BETWEENNESS GEOMETRY

Vector Notation: AB represents the vector from point A to point B on a graph. The vector can be computed by B A.

NOTES ON LINEAR TRANSFORMATIONS


Databases 2011 The Relational Model and SQL

Matrix Representations of Linear Transformations and Changes of Coordinates

3 Some Integer Functions

Lecture 4: More SQL and Relational Algebra

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Lecture 2 Matrix Operations

7 Relations and Functions

Discrete Mathematics, Chapter 4: Number Theory and Cryptography

Introduction to Matrices for Engineers

Database Management Systems. Chapter 1

C H A P T E R Regular Expressions regular expression

Linear Algebra Notes for Marsden and Tromba Vector Calculus

Full and Complete Binary Trees

Binary Adders: Half Adders and Full Adders

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Linear Algebra Done Wrong. Sergei Treil. Department of Mathematics, Brown University

Relational Division and SQL

1 Sets and Set Notation.

Foundations of mathematics. 2. Set theory (continued)

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

CS377: Database Systems Data Security and Privacy. Li Xiong Department of Mathematics and Computer Science Emory University

Introduction to database design

Mathematics Review for MS Finance Students

The Relational Algebra

Lecture 25: Database Notes

MATH10040 Chapter 2: Prime and relatively prime numbers

Geometric Transformations

CONTENTS 1. Peter Kahn. Spring 2007

Handout #1: Mathematical Reasoning

1 VECTOR SPACES AND SUBSPACES

Mathematics Course 111: Algebra I Part IV: Vector Spaces

Cartesian Products and Relations

Lecture 12: Entity Relationship Modelling

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Algebra I Notes Relations and Functions Unit 03a

Regular Languages and Finite Automata

CM2202: Scientific Computing and Multimedia Applications General Maths: 2. Algebra - Factorisation

I. GROUPS: BASIC DEFINITIONS AND EXAMPLES

26 Ideals and Quotient Rings

Factoring Trinomials of the Form x 2 bx c

Introduction to SQL for Data Scientists

Vocabulary Words and Definitions for Algebra

GRAPHING IN POLAR COORDINATES SYMMETRY

Relational Calculus. Module 3, Lecture 2. Database Management Systems, R. Ramakrishnan 1

1 The Concept of a Mapping

Lecture 1. Basic Concepts of Set Theory, Functions and Relations

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

SQL: Queries, Programming, Triggers

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Transcription:

Today Relations Binary relations and properties Relationship to functions n-ary relations Definitions CS application: Relational DBMS

Binary relations establish a relationship between elements of two sets Definition: Let A and B be two sets. A binary relation from A to B is a subset of A B. In other words, a binary relation R is a set of ordered pairs (a i, b i ) where a i A and b i B. Notation: We say that a R b if (a,b) R a R b if (a,b) R

Example: Course Enrollments Let s say that Alice and Bob are taking CS 441. Alice is also taking Math 336. Furthermore, Charlie is taking Art 212 and Business 444. Define a relation R that represents the relationship between people and classes. Solution: Let the set P denote people, so P = {Alice, Bob, Charlie} Let the set C denote classes, so C = {CS 441, Math 336, Art 212, Business 444) By definition R P C From the above statement, we know that (Alice, CS 441) R (Bob, CS 441) R (Alice, Math 336) R (Charlie, Art 212) R (Charlie, Business 444) R So, R = {(Alice, CS 441), (Bob, CS 441), (Alice, Math 336), (Charlie, Art 212), (Charlie, Business 444)}

A relation can also be represented as a graph Let s say that Alice and Bob are taking CS 441. Alice is also taking Math 336. Furthermore, Charlie is taking Art 212 and Business 444. Define a relation R that represents the relationship between people and classes. (Alice, CS 441) R Alice Bob Charlie Art 212 Business 444 CS 441 Math 336 Elements of P (i.e., people) Elements of C (i.e., classes)

A relation can also be represented as a table Let s say that Alice and Bob are taking CS 441. Alice is also taking Math 336. Furthermore, Charlie is taking Art 212 and Business 444. Define a relation R that represents the relationship between people and classes. Name of the relation (Bob, CS 441) R Elements of C (i.e., courses) R Art 212 Business 444 CS 441 Math 336 Alice X X Bob Charlie X X X Elements of P (i.e., people)

Wait, doesn t this mean that relations are the same as functions? Not quite Recall the following definition from past Lecture. Definition: Let A and B be nonempty sets. A function, f, is an assignment of exactly one element of set B to each element of set A. This would mean that, e.g., a person only be enrolled in one course! Reconciling this with our definition of a relation, we see that 1. Every function is also a relation 2. Not every relation is a function Let s see some quick examples

Short and sweet 1. Consider f : S G Clearly a function Can also be represented as the relation R = {(Anna, C), (Brian, A), (Christine A)} f : S G Anna Brian Christine A B C D F 1. Consider the set R = {(A, 1), (A, 2)} Clearly a relation Cannot be represented as a function! R A 1 2

We can also define binary relations on a single set Definition: A relation on the set A is a relation from A to A. That is, a relation on the set A is a subset of A A. Example: Let A be the set {1, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) a divides b}?

We can also define binary relations on a single set Definition: A relation on the set A is a relation from A to A. That is, a relation on the set A is a subset of A A. Example: Let A be the set {1, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) a divides b}? Solution: 1 divides everything (1,1), (1,2), (1,3), (1,4) 2 divides itself and 4 (2,2), (2,4) 3 divides itself (3,3) 4 divides itself (4,4) So, R = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,4), (3,3), (4,4)}

Representing the last example as a graph Example: Let A be the set {1, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) a divides b}? 1 2 3 4 1 2 3 4 1 2 3 4

Tell me what you know Question: Which of the following relations contain each of the pairs (1,1), (1,2), (2,1), (1,-1), and (2,2)? R 1 = {(a,b) a b} R 2 = {(a,b) a > b} R 3 = {(a,b) a = b or a = -b} R 4 = {(a,b) a = b} R 5 = {(a,b) a = b + 1} R 6 = {(a,b) a + b 3} These are all relations on an infinite set! Answer: (1,1) (1,2) (2,1) (1,-1) (2,2) R 1 Yes Yes No No Yes R 2 No No Yes Yes No R 3 Yes No No Yes Yes R 4 Yes No No No Yes R 5 No No Yes No No R 6 Yes Yes Yes Yes No

Tell me what you know Question: Which of the following relations contain each of the pairs (1,1), (1,2), (2,1), (1,-1), and (2,2)? R 1 = {(a,b) a b} R 2 = {(a,b) a > b} R 3 = {(a,b) a = b or a = -b} R 4 = {(a,b) a = b} R 5 = {(a,b) a = b + 1} R 6 = {(a,b) a + b 3} Answer:

Properties of Relations Definition: A relation R on a set A is reflexive if (a,a) R for every a A. Note: Our divides relation on the set A = {1,2,3,4} is reflexive. 1 2 3 4 1 2 3 4 1 X X X X 2 X X 3 X 4 X Every a A divides itself!

Properties of Relations Definition: A relation R on a set A is symmetric if (b,a) R whenever (a,b) R for every a,b A. If R is a relation in which (a,b) R and (b,a) R implies that a=b, we say that R is antisymmetric. Mathematically: Symmetric: a b((a,b) R (b,a) R) Antisymmetric: a b(((a,b) R (b,a) R) (a = b)) Examples: Symmetric: R = {(1,1), (1,2), (2,1), (2,3), (3,2), (1,4), (4,1), (4,4)} Antisymmetric: R = {(1,1), (1,2), (1,3), (1,4), (2,4), (3,3), (4,4)}

Symmetric and Antisymmetric Relations R = {(1,1), (1,2), (2,1), (2,3), (3,2), (1,4), (4,1), (4,4)} R = {(1,1), (1,2), (1,3), (1,4), (2,4), (3,3), (4,4)} 1 2 3 4 1 X X X X 2 X X 3 X X 4 X X 1 2 3 4 1 X X X X 2 X 3 X 4 X Symmetric relation Diagonal axis of symmetry Not all elements on the axis of symmetry need to be included in the relation Asymmetric relation No axis of symmetry Only symmetry occurs on diagonal Not all elements on the diagonal need to be included in the relation

Properties of Relations Definition: A relation R on a set A is transitive if whenver (a,b) R and (b,c) R, then (a,c) R for every a,b,c A. Note: Our divides relation on the set A = {1,2,3,4} is transitive. 1 divides 2 1 2 2 divides 4 3 4 This isn t terribly interesting, but it is transitive nonetheless. More common transitive relations include equality and comparison operators like <, >,, and.

Examples, redux Question: Which of the following relations are reflexive, symmetric, antisymmetric, and/or transitive? R 1 = {(a,b) a b} R 2 = {(a,b) a > b} R 3 = {(a,b) a = b or a = -b} R 4 = {(a,b) a = b} R 5 = {(a,b) a = b + 1} R 6 = {(a,b) a + b 3} Answer:

Examples, redux Question: Which of the following relations are reflexive, symmetric, antisymmetric, and/or transitive? R 1 = {(a,b) a b} R 2 = {(a,b) a > b} R 3 = {(a,b) a = b or a = -b} R 4 = {(a,b) a = b} R 5 = {(a,b) a = b + 1} R 6 = {(a,b) a + b 3} Answer: Reflexive Symmetric Antisymmetric Transitive R 1 Yes No Yes Yes R 2 No No Yes Yes R 3 Yes Yes No Yes R 4 Yes Yes Yes Yes R 5 No No Yes No R 6 No Yes No No

Relations can be combined using set operations Example: Let R be the relation that pairs students with courses that they have taken. Let S be the relation that pairs students with courses that they need to graduate. What do the relations R S, R S, and S R represent? Solution: R S = All pairs (a,b) where student a has taken course b OR student a needs to take course b to graduate R S = All pairs (a,b) where Student a has taken course b AND Student a needs course b to graduate S R = All pairs (a,b) where Student a needs to take course b to graduate BUT Student a has not yet taken course b

Relations can be combined using functional composition Definition: Let R be a relation from the set A to the set B, and S be a relation from the set B to the set C. The composite of R and S is the relation of ordered pairs (a, c), where a A and c C for which there exists an element b B such that (a, b) R and (b, c) S. We denote the composite of R and S by R º S. Example: What is the composite relation of R and S? R: {1,2,3} {1,2,3,4} R = {(1,1),(1,4),(2,3),(3,1),(3,4)} S: {1,2,3,4} {0,1,2} S = {(1,0),(2,0),(3,1),(3,2),(4,1)} So: R º S = {(1,0), (3,0), (1,1), (3,1), (2,1), (2,2)}

We can also relate elements of more than two sets Definition: Let A 1, A 2,, A n be sets. An n-ary relation on these sets is a subset of A 1 A 2 A n. The sets A 1, A 2,, A n are called the domains of the relation, and n is its degree. Example: Let R be the relation on Z Z Z consisting of triples (a, b, c) in which a,b,c form an arithmetic progression. That is (a,b,c) R iff there exist some k integer such that b=a+k and c=a+2k. What is the degree of this relation? What are the domains of this relation? Are the following tuples in this relation? (1,3,5)?? (2,5,9)??

We can also relate elements of more than two sets Definition: Let A 1, A 2,, A n be sets. An n-ary relation on these sets is a subset of A 1 A 2 A n. The sets A 1, A 2,, A n are called the domains of the relation, and n is its degree. Example: Let R be the relation on Z Z Z consisting of triples (a, b, c) in which a,b,c form an arithmetic progression. That is (a,b,c) R iff there exist some k integer such that b=a+k and c=a+2k. What is the degree of this relation? 3 What are the domains of this relation? Ints, Ints, Ints Are the following tuples in this relation? (1,3,5) 3=1+2 and 5= 1+2*2 (2,5,9) 5=2+3 but 9 2+2*3

N-ary relations are the basis of relational database management systems Data is stored in relations (a.k.a., tables) Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 CS 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 334322 Math 336 546346 Math 422 964389 Art 707 Columns of a table represent the attributes of a relation Rows, or records, contain the actual data defining the relation

Operations on an RDBMS are formally defined in terms of a relational algebra Relational algebra gives a formal semantics to the operations performed on a database by rigorously defining these operations in terms of manipulations on sets of tuples (i.e., records) Operators in relational algebra include: Selection Projection Rename Join Equijoin Left outer join Right outer join Aggregation

The selection operator allows us to filter the rows in a table Definition: Let R be an n-ary relation and let C be a condition that elements in R must satisfy. The selection s C maps the n-ary relation R to the n-ary relation of all n-tuples from R that satisfy the condition C. Example: Consider the Students relation from earlier in lecture. Let the condition C1 be Major= CS and let C2 be GPA > 2.5. What is the result of s C1 C2 (Students)? Answer: (Alice, 334322, CS, 3.45) (Charlie, 045628, CS, 2.75) Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 CS 2.75 Denise 964389 Art 4.0

The projection operator allows us to consider only a subset of the columns of a table Definition: The projection P i1,,in maps the n-tuple (a 1, a 2,, a n ) to the m-tuple (a i1,, a im ) where m n Example: What is the result of applying the projection P 1,3 to the Students table? Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 CS 2.75 Denise 964389 Art 4.0 Name Alice Bob Charlie Denise Major CS Math CS Art

The equijoin operator allows us to create a new table based on data from two or more related tables Definition: Let R be a relation of degree m and S be a relation of degree n. The equijoin J i1=j1,,ik=jk, where k m and k n, creates a new relation of degree m+n-k containing the subset of S R in which s i1 = r j1,, s ik =r jk and duplicate columns are removed (via projection). Example: What is the result of the equijoin J 2=1 on the Students and Enrollment tables? Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 CS 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 334322 Math 336 546346 Math 422 964389 Art 707

What is the result of the equijoin J 2=1 on the Students and Enrollment tables? Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 CS 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 334322 Math 336 546346 Math 422 964389 Art 707 Name ID Major GPA Course Alice 334322 CS 3.45 CS 441 Alice 334322 CS 3.45 Math 336 Bob 546346 Math 3.23 Math 422 Denise 964389 Art 4.0 Art 707

SQL queries correspond to statements in relational algebra Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 CS 2.75 Denise 964389 Art 4.0 Name ID Alice 334322 Charlie 045628 SELECT Name, ID FROM Students WHERE Major = CS AND GPA > 2.5 SELECT is actually a projection (in this case, P 1,2 ) The WHERE clause lets us filter (i.e., S major= CS GPA>2.5 )

SQL: An Equijoin Example Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 CS 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 334322 Math 336 546346 Math 422 964389 Art 707 SELECT Name, ID, Major, GPA, Course FROM Students, Enrollment WHERE ID = Stud_ID Name ID Major GPA Course Alice 334322 CS 3.45 CS 441 Alice 334322 CS 3.45 Math 336 Bob 546346 Math 3.23 Math 422 Denise 964389 Art 4.0 Art 707

Group Work! Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 CS 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 334322 Math 336 546346 Math 422 964389 Art 707 Problem 1: What is P 1,4 (Students)? Problem 2: What relational operators would you use to generate a table containing only the names of Math and CS majors with a GPA > 3.0? Problem 3: Write an SQL statement corresponding to the solution to problem 2.