阳光下的花园小径、高大的花株与沿阶梯行走的人物
韦特伊的画家花园 · Claude Monet / National Gallery of Art

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 A1,…,AnA_1,\ldots,A_n, an n-ary relation is a subset

R⊆A1×A2×⋯×An.R\subseteq A_1\times A_2\times\cdots\times A_n.

Each element is an ordered n-tuple (a1,…,an)(a_1,\ldots,a_n). The set AiA_i is the domain of the iith component, and nn is the degree of the relation. Degree counts components, not records.

For example, the ternary relation of integer arithmetic progressions is

R={(a,b,c)∈Z3∣b−a=c−b}.R=\{(a,b,c)\in\mathbb Z^3\mid b-a=c-b\}.

The triple (1,3,5)(1,3,5) belongs to RR, whereas (2,5,9)(2,5,9) does not: its consecutive differences are 3 and 4. All three domains are Z\mathbb Z, 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.

NameStudent IDMajorGPA
Ackermann231455Computer Science3.88
Adams888323Physics3.45
Chou102147Computer Science3.49
Goodfriend453876Mathematics3.45
Rao678543Mathematics3.90
Stevens786576Psychology2.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:

sC(R)={t∈R∣C(t) holds}.s_C(R)=\{t\in R\mid C(t)\text{ holds}\}.

If RR has degree nn, its selection still has degree nn, though it may have fewer rows.

Projection retains specified components and removes the others. For 1≤i1<⋯<im≤n1\le i_1<\cdots<i_m\le n,

Pi1,…,im(R)={(ai1,…,aim)∣(a1,…,an)∈R}.P_{i_1,\ldots,i_m}(R)= \{(a_{i_1},\ldots,a_{i_m})\mid(a_1,\ldots,a_n)\in R\}.

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 RR be the relation containing the six student records in the preceding section.

  1. Select the records whose major is Computer Science.
  2. Select the records whose GPA is greater than 3.5.
  3. Select the records satisfying both conditions.
  4. Project the result of part 3 onto Name and GPA.
Show solution and steps
ConditionComplete records retained
Major is Computer Science(Ackermann,231455,Computer Science,3.88)(Ackermann,231455,Computer\ Science,3.88); (Chou,102147,Computer Science,3.49)(Chou,102147,Computer\ Science,3.49)
GPA >3.5>3.5(Ackermann,231455,Computer Science,3.88)(Ackermann,231455,Computer\ Science,3.88); (Rao,678543,Mathematics,3.90)(Rao,678543,Mathematics,3.90)
Both conditions(Ackermann,231455,Computer Science,3.88)(Ackermann,231455,Computer\ Science,3.88)

Part 4 gives {(Ackermann,3.88)}\{(Ackermann,3.88)\}. The complete query can be expressed as

P1,4(sMajor=Computer Science ∧ GPA>3.5(R)).P_{1,4}\bigl(s_{\mathrm{Major}=\text{Computer Science}\ \land\ \mathrm{GPA}>3.5}(R)\bigr).

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 P1,2(E)P_{1,2}(E) for the following enrollment relation EE, and explain why the result does not contain eight rows.

StudentMajorCourse
GlauserBiologyBI 290
GlauserBiologyMS 475
GlauserBiologyPY 410
MarcusMathematicsMS 511
MarcusMathematicsMS 603
MarcusMathematicsCS 322
MillerComputer ScienceMS 575
MillerComputer ScienceCS 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

StudentMajor
GlauserBiology
MarcusMathematics
MillerComputer 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 RR have degree mm, SS have degree nn, and 1≤p≤min⁡(m,n)1\le p\le\min(m,n). Match the last pp components of RR with the first pp components of SS:

Jp(R,S)={(a,c,b)∣(a,c)∈R,(c,b)∈S}.J_p(R,S)=\{(\boldsymbol a,\boldsymbol c,\boldsymbol b) \mid(\boldsymbol a,\boldsymbol c)\in R, (\boldsymbol c,\boldsymbol b)\in S\}.

The lengths of a,c,b\boldsymbol a,\boldsymbol c,\boldsymbol b are m−p,p,n−pm-p,p,n-p, respectively. Shared components are retained once, so the degree of the result is

m+n−p.m+n-p.

Both joins and binary relation composition match an intermediate component, but a join keeps that component. Combining (a,c)(a,c) with (c,b)(c,b) by composition yields (a,b)(a,b); a join yields (a,c,b)(a,c,b).

Example: Joining on two fields. The tables below contain teaching assignments RR and a class schedule SS, using anonymous instructor labels. Find J2(R,S)J_2(R,S) by matching Department and Course Number. Explain what happens to records without a match, and determine the degree of the result.

Teaching assignments RR:

InstructorDepartmentCourse Number
AZoology335
AZoology412
BPsychology501
BPsychology617
CPhysics544
CPhysics551
DComputer Science518
DMathematics575

Class schedule SS:

DepartmentCourse NumberRoomTime
Computer Science518N52114:00
Mathematics575N50215:00
Mathematics611N52116:00
Physics544B50516:00
Psychology501A10015:00
Psychology617A11011:00
Zoology335A10009:00
Zoology412A10008: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

InstructorDepartmentCourse NumberRoomTime
AZoology335A10009:00
AZoology412A10008:00
BPsychology501A10015:00
BPsychology617A11011:00
CPhysics544B50516:00
DComputer Science518N52114:00
DMathematics575N50215: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 3+4−2=53+4-2=5. 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 k>0k>0 transactions, let tit_i be the itemset in transaction ii. The index identifies each transaction, so equal itemsets at different indices still count separately. The count and support of an itemset II are

σ(I)=∣{i∈{1,…,k}∣I⊆ti}∣,support⁡(I)=σ(I)k.\sigma(I)=|\{i\in\{1,\ldots,k\}\mid I\subseteq t_i\}|,\qquad \operatorname{support}(I)=\frac{\sigma(I)}{k}.

A qualifying transaction must contain every item in II, 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 ss.

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 nn products, each such record has degree n+1n+1; the number of records is the number of transactions.

Support and Confidence of an Association Rule

For disjoint itemsets II and JJ drawn from the set of products, the rule I→JI\to J describes a statistical association between buying II and buying JJ. The implication symbol does not assert that every transaction containing II must contain JJ.

support⁡(I→J)=σ(I∪J)k,confidence⁡(I→J)=σ(I∪J)σ(I).\operatorname{support}(I\to J)=\frac{\sigma(I\cup J)}{k}, \qquad \operatorname{confidence}(I\to J)=\frac{\sigma(I\cup J)}{\sigma(I)}.

Confidence requires σ(I)>0\sigma(I)>0. Support divides by all transactions; confidence divides by transactions containing the antecedent II. 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 IDItems
1apples, pears, mangos
2pears, cider
3apples, cider, donuts, mangos
4apples, pears, cider, donuts
5apples, cider, donuts
6pears, cider, donuts
7pears, donuts
8apples, pears, cider
Show solution and steps
ItemsetTransaction IDsCountSupport
apples1, 3, 4, 5, 855/85/8
apples, cider3, 4, 5, 844/8=0.54/8=0.5
cider, donuts3, 4, 5, 644/84/8
cider, donuts, apples3, 4, 533/83/8

The rule has support 3/8=0.3753/8=0.375 and confidence 3/4=0.753/4=0.75. 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 3/83/8, but changes confidence to 3/53/5. 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 support⁡({cider})=6/8\operatorname{support}(\{cider\})=6/8. The value 4/84/8 belongs to {apples, cider}. A single-item support and a joint-itemset support refer to different counts.

4. Review and Self-Check

Operation or measureEssential check
KeyDistinguish current uniqueness from future constraints
SelectionFilter rows and retain all fields
ProjectionRetain chosen columns and remove duplicate tuples
JoinMatch every shared field and retain shared fields once
SupportDivide the joint itemset count by all transactions
ConfidenceDivide the joint itemset count by the antecedent count
  1. A relation of degree 5 and a relation of degree 8 are joined on three fields. What is the resulting degree?
  2. For R={(a,1),(a,2)}R=\{(a,1),(a,2)\}, what is the projection onto the first component?
  3. In a nonempty transaction collection, if σ(I)=0\sigma(I)=0, what are the support and confidence of I→JI\to J?
Show self-check answers
  1. The degree is 5+8−3=105+8-3=10. This does not determine the number of rows.
  2. The unary relation is {(a)}\{(a)\}, containing one tuple, rather than two duplicate records.
  3. No transaction can contain I∪JI\cup J, so support is zero. Confidence has a zero denominator and is undefined, rather than automatically zero.

Glossary and Sources

TermMeaning
DomainThe allowed values for a component
DegreeThe number of components in each tuple
Primary key / composite keyThe selected candidate key / a key containing more than one field; a primary key can be composite
Selection / projection / joinFilter rows / retain components / match and combine records
ItemsetA set of products or other items
Support / confidenceFrequency 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:

RelationsDatabasesAssociation Rules