nary Relations and Its Applications nary Relations nary

  • Slides: 26
Download presentation
n-ary Relations and Its Applications

n-ary Relations and Its Applications

n-ary Relations • n-ary Relations are relationships among elements from more than two sets.

n-ary Relations • n-ary Relations are relationships among elements from more than two sets. • Example : • A relationship involving the name of student, the student’s major, and the student’s GPA • A relationship involving the airline, flight number, starting point, destination, departure time, and arrival time of a flight. • A relationship in mathematics involving three integers where the first integer is larger than the second integer, which is larger than the third.

n-ary Relations • n-ary Relations are used to represent computer databases. These relationships help

n-ary Relations • n-ary Relations are used to represent computer databases. These relationships help us answer queries about information stored in databases, such as : • Which employees of a company have worked for the company less than 5 years ? • Which students majoring in mathematics and have greater than a 3. 0 GPA ?

n-ary Relations • The concept of binary relations is generalized to n-ary relations. •

n-ary Relations • The concept of binary relations is generalized to n-ary relations. • Definition: Let A 1, A 2, …, An be sets. An n-ary relation on these sets is a subset of A 1 A 2 … An. • The sets A 1, A 2, …, An are the domains of the relation. • “n” is the degree of the relation.

n-ary Relations Example 1 : Let R = {(a, b, c) | (a =

n-ary Relations Example 1 : Let R = {(a, b, c) | (a = 2 b) (b = 2 c) with a, b, c are integers} What is the degree of R? The degree of R is 3. What are its domains? Its domains are all equal to the set of integers. Is (2, 4, 8) in R? No. Is (4, 2, 1) in R? Yes.

n-ary Relations Example 2 : Let R be the relation consisting of 5 -fields

n-ary Relations Example 2 : Let R be the relation consisting of 5 -fields (A, N, S, D, T) representing airplane flights where A is the airline, N is the flight number, S is the starting point, D is the destination, and T is the departure time. For instance : Singapore Airlines has flight number 777 from Changi to Juanda at 15: 00, then (SIA, 777, Changi, Juanda, 15: 00) belongs to R. The degree of this relation is 5. Its domains are the set of all airlines, the set of flight numbers, the set of starting point airports, the set of destination airports, and the set of departure times.

The applications of n-ary Relations • n-ary Relations are used to represent computer databases.

The applications of n-ary Relations • n-ary Relations are used to represent computer databases. • A database is a storehouse of associated information about some enterprise. • The user of the database can retrieve some specific information stored in the database. • In a well-designed database a user can perform queries on the database to retrieve information not contained in a any single fact. • To design a useful and efficient large database, it is necessary to model the enterprise with which the database is concerned.

Databases • One type of database representation that is based on relations is a

Databases • One type of database representation that is based on relations is a relational data model. • A database consists of n-tuples called records, which are made up of fields. These fields are the entries of the n tuples. • The relational data model represents a database as an n-ary relation, that is, a set of records.

Databases Example 1: Consider a database of students, whose records are represented as 4

Databases Example 1: Consider a database of students, whose records are represented as 4 -tuples with the fields Student Name, ID Number, Major, and GPA : (Ackermann, 231455, CS, 3. 88), (Adams, 888323, Physics, 3. 45), (Chou, 102147, CS, 3. 79), (Goodfriend, 453876, Math, 3. 45), (Rao, 678543, Math, 3. 90), (Stevens, 786576, Psych, 2. 99)

Databases • Relational databases are also called tables, since they are often displayed as

Databases • Relational databases are also called tables, since they are often displayed as tables (rows = records; columns = fields). • Table 1, page 385.

Databases • A variety of operations on n-ary relations can be applied to form

Databases • A variety of operations on n-ary relations can be applied to form new relations: – Select (will not be discussed) – Project – Join

Database • Definition: The projection Pi 1, i 2, …, im maps the ntuple

Database • Definition: The projection Pi 1, i 2, …, im maps the ntuple (a 1, a 2, …, an) to the m-tuple (ai 1, ai 2, …, aim), 1 2 m where m n. • In other words, a projection Pi 1, i 2, …, im keeps the m components ai 1, ai 2, …, aim of an n-tuple and deletes its (n – m) other components. • Note: The project operation creates a subset of the columns that represent certain attributes. • Projections are used to form new n-ary relations by deleting the same fields in every record of the relation.

Database Example (again): R = {(Ackermann, 231455, CS, 3. 88), (Adams, 888323, Physics, 3.

Database Example (again): R = {(Ackermann, 231455, CS, 3. 88), (Adams, 888323, Physics, 3. 45), (Chou, 102147, CS, 3. 79), (Goodfriend, 453876, Math, 3. 45), (Rao, 678543, Math, 3. 90), (Stevens, 786576, Psych, 2. 99)} • When the projection P 1, 3 is applied to the R relation, the result will be as follows : {(Ackermann, CS), (Adams, Physics), (Chou, CS), (Goodfriend, Math), (Rao, Math), (Stevens, Psych)} • Note : Show the table (will not be stored in the HD).

Database • In some cases, applying a projection to an entire table may not

Database • In some cases, applying a projection to an entire table may not only result in fewer columns, but also in fewer rows. • Why is that? • Some records may only have differed in those fields that were deleted, so they become identical, and there is no need to list identical records more than once.

Database • Example : (Page 387) • What is the table obtained when the

Database • Example : (Page 387) • What is the table obtained when the projection P 1, 2 is applied to the relation in Table 3 ? • Solution : • Note : Show the table (will not be stored in the HD).

Database (25/9) • The join operation is used to combine two tables into one

Database (25/9) • The join operation is used to combine two tables into one if they share some identical fields. • Join is therefore not really a separate operation but is defined as the result of doing a Cartesian product followed by a select.

Database • Definition: Let R be a relation of degree m and S a

Database • Definition: Let R be a relation of degree m and S a relation of degree n. The join Jp(R, S), where p m and p n, is a relation of degree m + n – p that consists of all (m + n – p)-tuples: (a 1, a 2, …, am-p, c 1, c 2, …, cp, b 1, b 2, …, bn-p), where the m-tuple (a 1, a 2, …, am-p, c 1, c 2, …, cp) belongs to R, and the n-tuple (c 1, c 2, …, cp, b 1, b 2, …, bn-p) belongs to S.

Database • That is, to generate Jp(R, S), all the elements in R whose

Database • That is, to generate Jp(R, S), all the elements in R whose “p” last components match the “p” first components of an element in S, need to be found. • The new relation contains exactly these matches which are combined to tuples that contain each matching field only once. • Note: The definition of the join operation here, assumes that overlapping columns occur at the end of R, and at the beginning of S. If the overlapping columns occur in different places, they can be rearranged using a projection.

Database Example : Given: S, R; What is J 1(S, R) ? S =

Database Example : Given: S, R; What is J 1(S, R) ? S = {(1978, Ackermann), (1972, Adams), (1917, Chou), (1984, Goodfriend), (1982, Rao), (1970, Stevens)}, (S contains the fields: Year of Birth, Student Name). R = {(Ackermann, 231455, CS, 3. 88), (Adams, 888323, Physics, 3. 45), (Chou, 102147, CS, 3. 79), (Goodfriend, 453876, Math, 3. 45), (Rao, 678543, Math, 3. 90), (Stevens, 786576, Psych, 2. 99)} (R contains the fields: Student Name, ID, Major and GPA).

Database Solution: The resulting relation J 1(S, R) is: {(1978, Ackermann, 231455, CS, 3.

Database Solution: The resulting relation J 1(S, R) is: {(1978, Ackermann, 231455, CS, 3. 88), (1972, Adams, 888323, Physics, 3. 45), (1917, Chou, 102147, CS, 3. 79), (1984, Goodfriend, 453876, Math, 3. 45), (1982, Rao, 678543, Math, 3. 90), (1970, Stevens, 786576, Psych, 2. 99)} • Since S has two fields and R has four, the relation J 1(S, R) has 2 + 4 – 1 = 5 fields.

Database Example 8 (Page 388) : What relation results when the join operator J

Database Example 8 (Page 388) : What relation results when the join operator J 2 is used to combine the relation displayed in Tables 5 and 6 ? Solution :

Representing Relations There are various ways to represent relations, two of which are: –

Representing Relations There are various ways to represent relations, two of which are: – Zero-one matrices – Directed graphs. • If R is a relation from A = {a 1, a 2, …, am} to B = {b 1, b 2, …, bn}, then R can be represented by the zero-one matrix MR = [mij], with mij = 1, if (ai, bj) R, and mij = 0, if (ai, bj) R. • • Note: for creating the above matrix, the elements in A and B need to be listed in a particular, but arbitrary order.

Representing Relations Example : How can we represent the relation R = {(2, 1),

Representing Relations Example : How can we represent the relation R = {(2, 1), (3, 2)} as a zero-one matrix? (R is from A to B; A = {1, 2, 3} and B = {1, 2}). Solution: The matrix MR is given by

Representing Relations Example 2: (Page 390) Let A = {a 1, a 2, a

Representing Relations Example 2: (Page 390) Let A = {a 1, a 2, a 3} and B = {b 1, b 2, b 3, b 4, b 5}. Which ordered pairs are in the relation R represented by the matrix MR = Solution:

Representing Relations • Definition: A directed graph, or digraph, consists of a set V

Representing Relations • Definition: A directed graph, or digraph, consists of a set V of vertices (or nodes) together with a set E of ordered pairs of elements of V called edges (or arcs). • The vertex “a” is called the initial vertex of the edge (a, b), and the vertex “b” is called the terminal vertex of this edge. Arrows are used to display graphs.

Representing Relations Using Digraphs Example: Display the digraph with V = {a, b, c,

Representing Relations Using Digraphs Example: Display the digraph with V = {a, b, c, d}, E = {(a, b), (a, d), (b, b), (b, d), (c, a), (c, b), (d, b)}. a d b c An edge of the form (b, b) is called a loop.