18.5 KB · commit a9602da
Relationships among elements of more than two sets often arise. For instance, there is a relationship involving the name of a student, the student’s major, and the student’s grade point average. Similarly, there is a relationship involving the airline, flight number, starting point, destination, departure time, and arrival time of a flight. An example of such a relationship in mathematics involves three integers, where the first integer is larger than the second integer, which is larger than the third. Another example is the betweenness relationship involving points on a line, such that three points are related when the second point is between the first and the third.
We will study relationships among elements from more than two sets in this section. These relationships are called n-ary relations. These relations are used to represent computer databases. These representations help us answer queries about the information stored in databases, such as: Which flights land at O’Hare Airport between 3 A.M. and 4 A.M.? Which students at your
school are sophomores majoring in mathematics or computer science and have greater than a 3.0 average? Which employees of a company have worked for the company less than 5 years and make more than $50,000?
We begin with the basic definition on which the theory of relational databases rests.
Let R be the relation on N×N×N consisting of triples (a,b,c), where a, b, and c are integers with a<b<c. Then (1,2,3)∈R, but (2,4,3)∈/R. The degree of this relation is 3. Its domains are all equal to the set of natural numbers. ◀
Let R be the relation on Z×Z×Z consisting of all triples of integers (a,b,c) in which a, b, and c form an arithmetic progression. That is, (a,b,c)∈R if and only if there is an integer k such that b=a+k and c=a+2k, or equivalently, such that b−a=k and c−b=k. Note that (1,3,5)∈R because 3=1+2 and 5=1+2⋅2, but (2,5,9)∈/R because 5−2=3 while 9−5=4. This relation has degree 3 and its domains are all equal to the set of integers. ◀
Let R be the relation on Z×Z×Z+ consisting of triples (a,b,m), where a, b, and m are integers with m≥1 and a≡b(modm). Then (8,2,3), (−1,9,5), and (14,0,7) all belong to R, but (7,2,3), (−2,−8,5), and (11,0,6) do not belong to R because 8≡2(mod3), −1≡9(mod5), and 14≡0(mod7), but 7≡2(mod3), −2≡−8(mod5), and 11≡0(mod6). This relation has degree 3 and its first two domains are the set of all integers and its third domain is the set of positive integers. ◀
Let R be the relation consisting of 5-tuples (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, if Nadir Express Airlines has flight 963 from Newark to Bangor at 15:00, then (Nadir,963,Newark,Bangor,15:00) belongs to R. The degree of this relation is 5, and its domains are the set of all airlines, the set of flight numbers, the set of cities, the set of cities (again), and the set of times. ◀
The time required to manipulate information in a database depends on how this information is stored. The operations of adding and deleting records, updating records, searching for records, and combining records from overlapping databases are performed millions of times each day in a large database. Because of the importance of these operations, various methods for representing databases have been developed. We will discuss one of these methods, called the relational data model, based on the concept of a relation.
A database consists of records, which are n-tuples, made up of fields. The fields are the entries of the n-tuples. For instance, a database of student records may be made up of fields containing the name, student number, major, and grade point average of the student. The relational data model represents a database of records as an n-ary relation. Thus, student records
are represented as 4-tuples of the form (Student_name,ID_number,Major,GPA). A sample database of six such records is
(Ackermann,231455,Computer Science,3.88) (Adams,888323,Physics,3.45) (Chou,102147,Computer Science,3.49) (Goodfriend,453876,Mathematics,3.45) (Rao,678543,Mathematics,3.90) (Stevens,786576,Psychology,2.99).
| Student_name | ID_number | Major | GPA |
|---|---|---|---|
| Ackermann | 231455 | Computer Science | 3.88 |
| Adams | 888323 | Physics | 3.45 |
| Chou | 102147 | Computer Science | 3.49 |
| Goodfriend | 453876 | Mathematics | 3.45 |
| Rao | 678543 | Mathematics | 3.90 |
| Stevens | 786576 | Psychology | 2.99 |
Relations used to represent databases are also called tables, because these relations are often displayed as tables. Each column of the table corresponds to an attribute of the database. For instance, the same database of students is displayed in Table 1. The attributes of this database are Student Name, ID Number, Major, and GPA.
A domain of an n-ary relation is called a primary key when the value of the n-tuple from this domain determines the n-tuple. That is, a domain is a primary key when no two n-tuples in the relation have the same value from this domain.
Records are often added to or deleted from databases. Because of this, the property that a domain is a primary key is time-dependent. Consequently, a primary key should be chosen that remains one whenever the database is changed. The current collection of n-tuples in a relation is called the extension of the relation. The more permanent part of a database, including the name and attributes of the database, is called its intension. When selecting a primary key, the goal should be to select a key that can serve as a primary key for all possible extensions of the database. To do this, it is necessary to examine the intension of the database to understand the set of possible n-tuples that can occur in an extension.
Which domains are primary keys for the n-ary relation displayed in Table 1, assuming that no n-tuples will be added in the future?
Solution: Because there is only one 4-tuple in this table for each student name, the domain of student names is a primary key. Similarly, the ID numbers in this table are unique, so the domain of ID numbers is also a primary key. However, the domain of major fields of study is not a primary key, because more than one 4-tuple contains the same major field of study. The domain of grade point averages is also not a primary key, because there are two 4-tuples containing the same GPA. ◀
Combinations of domains can also uniquely identify n-tuples in an n-ary relation. When the values of a set of domains determine an n-tuple in a relation, the Cartesian product of these domains is called a composite key.
Is the Cartesian product of the domain of major fields of study and the domain of GPAs a composite key for the n-ary relation from Table 1, assuming that no n-tuples are ever added?
Solution: Because no two 4-tuples from this table have both the same major and the same GPA, this Cartesian product is a composite key. ◀
Because primary and composite keys are used to identify records uniquely in a database, it is important that keys remain valid when new records are added to the database. Hence, checks should be made to ensure that every new record has values that are different in the appropriate field, or fields, from all other records in this table. For instance, it makes sense to use the student identification number as a key for student records if no two students ever have the same student identification number. A university should not use the name field as a key, because two students may have the same name (such as John Smith).
There are a variety of operations on n-ary relations that can be used to form new n-ary relations. Applied together, these operations can answer queries on databases that ask for all n-tuples that satisfy certain conditions.
The most basic operation on an n-ary relation is determining all n-tuples in the n-ary relation that satisfy certain conditions. For example, we may want to find all the records of all computer science majors in a database of student records. We may want to find all students who have a grade point average above 3.5. We may want to find the records of all computer science majors who have a grade point average above 3.5. To perform such tasks we use the selection operator.
To find the records of computer science majors in the n-ary relation R shown in Table 1, we use the operator sC1, where C1 is the condition Major=“Computer Science.” The result is the two 4-tuples (Ackermann,231455,Computer Science,3.88) and (Chou,102147,Computer Science,3.49). Similarly, to find the records of students who have a grade point average above 3.5 in this database, we use the operator sC2, where C2 is the condition GPA>3.5. The result is the two 4-tuples (Ackermann,231455,Computer Science,3.88) and (Rao,678543,Mathematics,3.90). Finally, to find the records of computer science majors who have a GPA above 3.5, we use the operator sC3, where C3 is the condition (Major=“Computer Science”∧GPA>3.5). The result consists of the single 4-tuple (Ackermann,231455,Computer Science,3.88). ◀
Projections are used to form new n-ary relations by deleting the same fields in every record of the relation.
In other words, the projection Pi1,i2,…,im deletes n−m of the components of an n-tuple, leaving the i1th,i2th,…, and imth components.
| Student_name | GPA |
|---|---|
| Ackermann | 3.88 |
| Adams | 3.45 |
| Chou | 3.49 |
| Goodfriend | 3.45 |
| Rao | 3.90 |
| Stevens | 2.99 |
| Student | Major | Course |
|---|---|---|
| Glauser | Biology | BI 290 |
| Glauser | Biology | MS 475 |
| Glauser | Biology | PY 410 |
| Marcus | Mathematics | MS 511 |
| Marcus | Mathematics | MS 603 |
| Marcus | Mathematics | CS 322 |
| Miller | Computer Science | MS 575 |
| Miller | Computer Science | CS 455 |
| Student | Major |
|---|---|
| Glauser | Biology |
| Marcus | Mathematics |
| Miller | Computer Science |
What results when the projection P1,3 is applied to the 4-tuples (2,3,0,4), (Jane Doe,234111001,Geography,3.14), and (a1,a2,a3,a4)?
Solution: The projection P1,3 sends these 4-tuples to (2,0), (Jane Doe,Geography), and (a1,a3), respectively. ◀
Example 9 illustrates how new relations are produced using projections.
What relation results when the projection P1,4 is applied to the relation in Table 1?
Solution: When the projection P1,4 is used, the second and third columns of the table are deleted, and pairs representing student names and grade point averages are obtained. Table 2 displays the results of this projection. ◀
Fewer rows may result when a projection is applied to the table for a relation. This happens when some of the n-tuples in the relation have identical values in each of the m components of the projection, and only disagree in components deleted by the projection. For instance, consider the following example.
What is the table obtained when the projection P1,2 is applied to the relation in Table 3?
Solution: Table 4 displays the relation obtained when P1,2 is applied to Table 3. Note that there are fewer rows after this projection is applied. ◀
The join operation is used to combine two tables into one when these tables share some identical fields. For instance, a table containing fields for airline, flight number, and gate, and another table containing fields for flight number, gate, and departure time can be combined into a table containing fields for airline, flight number, gate, and departure time.
In other words, the join operator Jp produces a new relation from two relations by combining all m-tuples of the first relation with all n-tuples of the second relation, where the last p components of the m-tuples agree with the first p components of the n-tuples.
| Professor | Department | Course_number |
|---|---|---|
| Cruz | Zoology | 335 |
| Cruz | Zoology | 412 |
| Farber | Psychology | 501 |
| Farber | Psychology | 617 |
| Grammer | Physics | 544 |
| Grammer | Physics | 551 |
| Rosen | Computer Science | 518 |
| Rosen | Mathematics | 575 |
| Department | Course_number | Room | Time |
|---|---|---|---|
| Computer Science | 518 | N521 | 2:00 P.M. |
| Mathematics | 575 | N502 | 3:00 P.M. |
| Mathematics | 611 | N521 | 4:00 P.M. |
| Physics | 544 | B505 | 4:00 P.M. |
| Psychology | 501 | A100 | 3:00 P.M. |
| Psychology | 617 | A110 | 11:00 A.M. |
| Zoology | 335 | A100 | 9:00 A.M. |
| Zoology | 412 | A100 | 8:00 A.M. |
What relation results when the join operator J2 is used to combine the relation displayed in Tables 5 and 6?
Solution: The join J2 produces the relation shown in Table 7. ◀
There are other operators besides projections and joins that produce new relations from existing relations. A description of these operations can be found in books on database theory.
The database query language SQL (short for Structured Query Language) can be used to carry out the operations we have described in this section. Example 12 illustrates how SQL commands are related to operations on n-ary relations.
We will illustrate how SQL is used to express queries by showing how SQL can be employed to make a query about airline flights using Table 8. The SQL statement
SELECT Departure_time
FROM Flights
WHERE Destination='Detroit'
is used to find the projection P5 (on the Departure_time attribute) of the selection of 5-tuples in the Flights database that satisfy the condition: Destination=‘Detroit’. The output would be a list containing the times of flights that have Detroit as their destination, namely, 08:10, 08:47,
| Professor | Department | Course_number | Room | Time |
|---|---|---|---|---|
| Cruz | Zoology | 335 | A100 | 9:00 A.M. |
| Cruz | Zoology | 412 | A100 | 8:00 A.M. |
| Farber | Psychology | 501 | A100 | 3:00 P.M. |
| Farber | Psychology | 617 | A110 | 11:00 A.M. |
| Grammer | Physics | 544 | B505 | 4:00 P.M. |
| Rosen | Computer Science | 518 | N521 | 2:00 P.M. |
| Rosen | Mathematics | 575 | N502 | 3:00 P.M. |
| Airline | Flight_number | Gate | Destination | Departure_time |
|---|---|---|---|---|
| Nadir | 122 | 34 | Detroit | 08:10 |
| Acme | 221 | 22 | Denver | 08:17 |
| Acme | 122 | 33 | Anchorage | 08:22 |
| Acme | 323 | 34 | Honolulu | 08:30 |
| Nadir | 199 | 13 | Detroit | 08:47 |
| Acme | 222 | 22 | Denver | 09:10 |
| Nadir | 322 | 34 | Detroit | 09:44 |
and 09:44. SQL uses the FROM clause to identify the n-ary relation the query is applied to, the WHERE clause to specify the condition of the selection operation, and the SELECT clause to specify the projection operation that is to be applied. (Beware: SQL uses SELECT to represent a projection, rather than a selection operation. This is an unfortunate example of conflicting terminology.) ◀
Example 13 shows how SQL queries can be made involving more than one table.
The SQL statement
SELECT Professor, Time
FROM Teaching_assignments, Class_schedule
WHERE Department='Mathematics'
is used to find the projection P1,5 of the 5-tuples in the database (shown in Table 7), which is the join J2 of the Teaching_assignments and Class_schedule databases in Tables 5 and 6, respectively, which satisfy the condition: Department=Mathematics. The output would consist of the single 2-tuple (Rosen,3:00 P.M.). The SQL FROM clause is used here to find the join of two different databases. ◀
We have only touched on the basic concepts of relational databases in this section. More information can be found in [AhUl95].