Download presentation
Presentation is loading. Please wait.
1
Discrete Structures for Computer Science
Presented By:Andrew F. Conn Lecture #23: N-ary relations and Representations November 30th, 2016
2
Announcements This is the end of new material!!! Thank you for sticking it out with me. Please take your OMET evaluations. Also, a huge thanks to Dr. Lee whose material was the basis for this class.
3
Today n-ary relations Relation Representations Definitions
CS application: Relational DBMS Relation Representations Matrix Representation Directed Graphs
4
We can also โrelateโ elements of more than two sets
Definition: Let ๐ด 1 , ๐ด 2 ,โฆ, ๐ด ๐ be sets. An n-ary relation on these sets is a subset of ๐ด 1 ร ๐ด 2 รโฆร๐ด. The sets ๐ด 1 , ๐ด 2 ,โฆ, ๐ด ๐ are called the domains of the relation, and ๐ is its degree. Example: Let ๐
be the relation on โครโคร โค + consisting of triples ๐, ๐, ๐ where ๐โก๐ (๐ฆ๐จ๐ ๐). What is the degree of this relation? What are the domains of this relation? Are the following tuples in this relation? (8,2,3) (-1,9,5) (11,0,6)
5
N-ary relations are the basis of relational database management systems
Data is stored in relations (a.k.a., tables) Columns of a table represent the attributes of a relation Rows, or records, contain the actual data defining the relation Students Enrollment Name ID Major GPA Stud_ID Course Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 Ask about degree, domains
6
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 Inner Join Outer join โฆ Aggregation
7
The selection operator allows us to filter the rows in a table
Definition: Let ๐
be an n-ary relation and let ๐ถ be a condition that elements in ๐
must satisfy. The selection ๐ ๐ maps the n-ary relation ๐
to the n-ary relation of all n-tuples from ๐
that satisfy the condition ๐ถ. Example: Consider the Students relation from earlier in lecture. Let the condition ๐ถ1 be Major=โCSโ and let ๐ถ2 be GPA > What is the result of ๐ ๐ถ1 โง ๐ถ2 (Students)? Answer: (Alice, , CS, 3.45) (Charlie, , CS, 2.75) Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0
8
The projection operator allows us to consider only a subset of the columns of a table
Definition: The projection ๐ ๐ 1 , ๐ 2 ,โฆ, ๐ ๐ maps the n-tuple ( ๐ 1 , ๐ 2 , โฆ, ๐ ๐ ) to the m-tuple ( ๐ ๐ 1 , ๐ ๐ 2 ,โฆ, ๐ ๐ ๐ ) where ๐โค๐ Example: What is the result of applying the projection ๐ 1,3 to the Students table? Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Name Major Alice CS Bob Math Charlie Denise Art
9
The join operator allows us to create a new table based on data from two or more related tables
Definition: Let ๐
be a relation of degree ๐ and ๐ be a relation of degree ๐. The Join ๐ฝ ๐ ๐
,๐ , where ๐โค๐ and ๐โค๐, creates a new relation of degree ๐+๐โ๐ containing the subset of ๐ร๐
in which elements from column ๐ match. Example: What is the result of the join ๐ฝ 2 (Students,Enrollments)? Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 NOTE: This syntax different than in book
10
What is the result of the join ๐ฝ 2 Students,Enrollments ?
Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 ๐=2 Corresponding Column in ๐ Name ID Major GPA Course Alice 334322 CS 3.45 CS 441 Math 336 Bob 546346 Math 3.23 Math 422 Denise 964389 Art 4.0 Art 707
11
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 2.75 Denise 964389 Art 4.0 Name ID Alice 334322 Charlie 045628 SELECT is actually a projection (in this case, ๐ท ๐,๐ ) SELECT Name, ID FROM Students WHERE Major = โCSโ AND GPA > 2.5 The WHERE clause lets us filter (in this case, ๐บ ๐๐๐ฃ๐จ๐ซ="๐ช๐บ" โง ๐ฎ๐ท๐จ>๐.๐ )
12
SQL: An Join Example Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 SELECT Name, ID, Major, GPA, Course FROM Students JOIN Enrollment ON ID = Stud_ID Name ID Major GPA Course Alice 334322 CS 3.45 CS 441 Math 336 Bob 546346 Math 3.23 Math 422 Denise 964389 Art 4.0 Art 707
13
Group Work! Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 Problem 1: What is ๐ 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. Student/GPA S_{(Major = CS OR Major = Math) AND GPA > 3.0} P_1 Students SELECT Name FROM Students WHERE (Major = CS OR Major = Math) AND GPA > 3.0
14
Representations of Binary Relations
Weโve already learned that Binary Relations can be represented with tables, but letโs formalize that. A Relation between finite sets can be represented as a zero-one matrix. Let ๐
be a relation from ๐ด to ๐ต. Then the matrix that represents ๐
is defined by: ๐ ๐,๐ = 1, ๐๐ ๐ ๐ , ๐ ๐ โ๐
0, ๐๐ ๐ ๐ , ๐ ๐ โ๐
We label the resulting matrix ๐ ๐
.
15
Example Relations and Matrices
๐
1 = 2,1 , 3,1 , 3,2 ๐ ๐
1 = ๐ ๐
2 = ๐
2 = ๐ 1 , ๐ 2 , ๐ 2 , ๐ 1 , ๐ 2 , ๐ 3 , ๐ 2 , ๐ 4 , ๐ 3 , ๐ 1 , ๐ 3 , ๐ 3 , ๐ 3 , ๐ 5
16
Final Thoughts Next time: Wrap up and Exam Review!
Relations allow us to represent and reason about the relationships between sets in a more general way than functions did Without n-ary relations, DBMS systems would not exist! Do you find this stuff interesting? CS 1555: Introduction to Database Systems CS 1571: Introduction to Artificial Intelligence Next time: Wrap up and Exam Review!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.