
A binary relation connects two components. Student records, flight records, and transactions often require more components. An n-ary relation stores them together as tuples, allowing queries to be expressed through operations on sets.
1. n-ary Relations and Tables
Definition, Domains, and Degree
For sets , an n-ary relation is a subset
Each element is an ordered n-tuple . The set is the domain of the th component, and is the degree of the relation. Degree counts components, not records.
For example, the ternary relation of integer arithmetic progressions is
The triple belongs to , whereas does not: its consecutive differences are 3 and 4. All three domains are , and the degree is 3.
In the relational data model, a record is a tuple, its components are fields or attributes, and a relation is commonly displayed as a table. Each column has a fixed attribute and position; each row is a complete record. A mathematical relation is a set, so identical complete tuples do not occur more than once, and row order does not affect the relation.
A flight may be represented by five fields: airline, flight number, origin, destination, and departure time. Specify what each time field means: a clock time such as 09:00 is not a flight duration.
Primary and Composite Keys
A key is a field or set of fields that uniquely identifies a record. A key using more than one field is a composite key. A candidate key is minimal: no proper subset of its fields still uniquely identifies records. The primary key is the candidate key chosen as the main identifier; it may be a single field or a composite key. Test uniqueness by asking whether two distinct records have the same values in the proposed key fields.
Example: Keys in a current table and in future updates. Given the following table, first assume that no more records will be added. Determine which of Name, Student ID, Major, and GPA uniquely identify a record, and whether Major together with GPA is a composite key. Then explain how to choose a key for a database maintained over time.
| Name | Student ID | 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 |
Show solution and steps
Names and student IDs are distinct in the six current records, so each field is a candidate key for this fixed table. Either can be chosen as the primary key; uniqueness alone does not designate both as primary keys. Majors repeat, and GPA 3.45 also repeats, so neither field is a key by itself.
No two records have both the same major and the same GPA. Thus Major together with GPA is a composite key for the current table. In particular, Adams and Goodfriend share GPA 3.45 but have different majors.
For future updates, choose a key using the constraints on all allowed records. Names may repeat, and a major–GPA combination may also repeat. If the database guarantees unique student IDs, Student ID is a suitable lasting key. Check the uniqueness constraint whenever records are added; uniqueness in the current table alone does not establish a permanent guarantee.
Keys connect records to functions: every key value that occurs determines one tuple of the remaining attribute values. Thus the relation can be viewed as the graph of a function from the set of occurring key values to tuples of the other attributes.
2. Three Basic Query Operations
Selection and Projection
Selection filters records according to a condition while retaining all their fields:
If has degree , its selection still has degree , though it may have fewer rows.
Projection retains specified components and removes the others. For ,
The result is an m-ary relation. Since relations are sets, identical projected tuples are merged, so projection can reduce the number of rows as well as the number of columns.
Example: Selection and projection on the student table. Let be the relation containing the six student records in the preceding section.
- Select the records whose major is Computer Science.
- Select the records whose GPA is greater than 3.5.
- Select the records satisfying both conditions.
- Project the result of part 3 onto Name and GPA.
Show solution and steps
| Condition | Complete records retained |
|---|---|
| Major is Computer Science | ; |
| GPA | ; |
| Both conditions |
Part 4 gives . The complete query can be expressed as
Keep Major and GPA until the filtering step is complete. If Major is removed first, the condition cannot be evaluated directly on the resulting table.
Example: Projection merges duplicate rows. Find for the following enrollment relation , and explain why the result does not contain eight rows.
| 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 |
Show solution and steps
Once Course is removed, Glauser’s three records become the same pair, as do Marcus’s three records and Miller’s two records. After removing duplicates, the result is
| Student | Major |
|---|---|
| Glauser | Biology |
| Marcus | Mathematics |
| Miller | Computer Science |
The original records differed only in a field that projection removed.
The SQL terminology can be confusing: the WHERE clause gives the selection condition, whereas SELECT specifies the projection fields. Mathematical relations use set semantics; SQL’s SELECT DISTINCT can enforce the corresponding removal of duplicate output rows.
Joins and Matching Fields
A join combines records whose shared components agree. Let have degree , have degree , and . Match the last components of with the first components of :
The lengths of are , respectively. Shared components are retained once, so the degree of the result is
Both joins and binary relation composition match an intermediate component, but a join keeps that component. Combining with by composition yields ; a join yields .
Example: Joining on two fields. The tables below contain teaching assignments and a class schedule , using anonymous instructor labels. Find by matching Department and Course Number. Explain what happens to records without a match, and determine the degree of the result.
Teaching assignments :
| Instructor | Department | Course Number |
|---|---|---|
| A | Zoology | 335 |
| A | Zoology | 412 |
| B | Psychology | 501 |
| B | Psychology | 617 |
| C | Physics | 544 |
| C | Physics | 551 |
| D | Computer Science | 518 |
| D | Mathematics | 575 |
Class schedule :
| Department | Course Number | Room | Time |
|---|---|---|---|
| Computer Science | 518 | N521 | 14:00 |
| Mathematics | 575 | N502 | 15:00 |
| Mathematics | 611 | N521 | 16:00 |
| Physics | 544 | B505 | 16:00 |
| Psychology | 501 | A100 | 15:00 |
| Psychology | 617 | A110 | 11:00 |
| Zoology | 335 | A100 | 09:00 |
| Zoology | 412 | A100 | 08:00 |
Show solution and steps
For example, the record (A, Zoology, 335) matches (Zoology, 335, A100, 09:00) on both shared fields, producing one five-component record. The complete result is
| Instructor | Department | Course Number | Room | Time |
|---|---|---|---|---|
| A | Zoology | 335 | A100 | 09:00 |
| A | Zoology | 412 | A100 | 08:00 |
| B | Psychology | 501 | A100 | 15:00 |
| B | Psychology | 617 | A110 | 11:00 |
| C | Physics | 544 | B505 | 16:00 |
| D | Computer Science | 518 | N521 | 14:00 |
| D | Mathematics | 575 | N502 | 15:00 |
Physics 551 has no matching class-schedule record, and Mathematics 611 has no matching teaching assignment. Neither enters the result. Matching Department alone would wrongly combine distinct courses; both shared fields must agree.
The degree is . This formula does not determine the number of rows. Depending on the matches, the row count may increase through multiple matches or decrease through unmatched records.
3. Transactions and Association Rules
Itemsets, Counts, and Support
An individual product is an item, a set of products is an itemset, and one purchase is a transaction. Different transactions with identical items still count separately; transaction IDs distinguish them.
For a collection of transactions, let be the itemset in transaction . The index identifies each transaction, so equal itemsets at different indices still count separately. The count and support of an itemset are
A qualifying transaction must contain every item in , but may contain other items too. Count transactions, not the number of product units. An itemset is frequent when its support is at least a specified threshold .
A transaction can be encoded as a zero-one tuple: fix an order for all products, use 1 for a purchased item and 0 otherwise, and include a transaction ID. With products, each such record has degree ; the number of records is the number of transactions.
Support and Confidence of an Association Rule
For disjoint itemsets and drawn from the set of products, the rule describes a statistical association between buying and buying . The implication symbol does not assert that every transaction containing must contain .
Confidence requires . Support divides by all transactions; confidence divides by transactions containing the antecedent . Support measures how often both itemsets occur together in the full data set. Confidence measures how often the consequent occurs among transactions containing the antecedent.
Example: Support and confidence from a complete transaction table. A market stand sells apples, pears, cider, donuts, and mangos. Its eight transactions are listed below. Find the count and support of the itemsets {apples} and {apples, cider}; find the support and confidence of {cider, donuts} → {apples}; and determine whether {apples, cider} is frequent at threshold 0.5.
| Transaction ID | Items |
|---|---|
| 1 | apples, pears, mangos |
| 2 | pears, cider |
| 3 | apples, cider, donuts, mangos |
| 4 | apples, pears, cider, donuts |
| 5 | apples, cider, donuts |
| 6 | pears, cider, donuts |
| 7 | pears, donuts |
| 8 | apples, pears, cider |
Show solution and steps
| Itemset | Transaction IDs | Count | Support |
|---|---|---|---|
| apples | 1, 3, 4, 5, 8 | 5 | |
| apples, cider | 3, 4, 5, 8 | 4 | |
| cider, donuts | 3, 4, 5, 6 | 4 | |
| cider, donuts, apples | 3, 4, 5 | 3 |
The rule has support and confidence . The confidence calculation considers only transactions 3, 4, 5, and 6, which contain the antecedent; the first three also contain apples. The support of {apples, cider} equals the threshold 0.5, so it is frequent.
Reversing the rule to {apples} → {cider, donuts} leaves support at , but changes confidence to . The joint count is unchanged, while the conditioning set differs.
High confidence alone does not imply that a rule applies to many transactions, nor does it establish causation. A rarely occurring rule may have high confidence but low support. A strong association rule typically must meet both a minimum support and a minimum confidence threshold.
Cider alone occurs in six of the eight transactions, so . The value belongs to {apples, cider}. A single-item support and a joint-itemset support refer to different counts.
4. Review and Self-Check
| Operation or measure | Essential check |
|---|---|
| Key | Distinguish current uniqueness from future constraints |
| Selection | Filter rows and retain all fields |
| Projection | Retain chosen columns and remove duplicate tuples |
| Join | Match every shared field and retain shared fields once |
| Support | Divide the joint itemset count by all transactions |
| Confidence | Divide the joint itemset count by the antecedent count |
- A relation of degree 5 and a relation of degree 8 are joined on three fields. What is the resulting degree?
- For , what is the projection onto the first component?
- In a nonempty transaction collection, if , what are the support and confidence of ?
Show self-check answers
- The degree is . This does not determine the number of rows.
- The unary relation is , containing one tuple, rather than two duplicate records.
- No transaction can contain , so support is zero. Confidence has a zero denominator and is undefined, rather than automatically zero.
Glossary and Sources
| Term | Meaning |
|---|---|
| Domain | The allowed values for a component |
| Degree | The number of components in each tuple |
| Primary key / composite key | The selected candidate key / a key containing more than one field; a primary key can be composite |
| Selection / projection / join | Filter rows / retain components / match and combine records |
| Itemset | A set of products or other items |
| Support / confidence | Frequency in the full collection / conditional frequency |
- Course slides.
- Rosen, 8th edition, Section 9.2: printed pages 611–619, PDF pages 634–642. The student table is on printed page 612; projection examples on 614; join inputs and output on 615; transactions on 618.
- Database terminology can be checked against the PostgreSQL documentation on primary keys.
- Tables preserve the source fields and record relationships. In the join example, personal names are replaced by anonymous labels A–D, and times use the 24-hour clock. The connection between keys and functions, the reversed-rule confidence calculation, and self-check explanations are supplementary. SQL terminology follows Section 9.2.5; the duplicate-removal clarification explains set semantics.
Assigned textbook exercises: Section 9.2, Exercise 8 on keys for book records; Exercise 13 on selection from a flight table; Exercise 17 on projection of that table; and Exercise 35 on the rule {beer} → {diapers}, using the six transactions in Exercise 33. All are on printed page 620 / PDF page 643. The flight table required by Exercises 13 and 17 is on printed page 616 / PDF page 639.
Updated: