2. Relational Data Model

Formal Definitions

Formal Informal
Relation Table
Tuple Row
Attribute Column
Degree Num. attributes
Cardinality Num. rows

Relation: subset of the cartesian product of the domains i.e. any valid combination of values for all the attributes of the table. RR is the intension - the schema of the relation, and r of Rr\ of\ R is an extension - a specific valid value of the relation.

The ordering of tuples irrelevant, but the ordering of values within a tuple relevant.

Constraints

Conditions that hold for all valid relation instances.

Domain constraint

Specifies that all values in a relation must be inside their respective domains.

Key Constraint

Entity Integrity Constraint

Primary key values must not be null.

Referential Integrity Constraint

Referencing relation has foreign key attribute referencing the primary key of the referenced relation. The value can be null, and the primary key can itself be a foreign key.

Semantic Integrity Constraint

Constraints not expressed by the model and relating to the application semantics.

EER to Relation Mapping Algorithm

  1. Simple tables: make a new table, pick any key as the primary key
  2. Weak entity; make a new table, with the primary key being the combination of the owner and partial key. If the partial key is composite, use the components
  3. 1:N: Put the foreign key in the ‘N’ side
  4. 1:1: Put the foreign key on the side with total participation
  5. M:N: make a new table with two foreign keys; the combination of the two form the primary key
  6. Multivalued attributes: make a new table with a foreign key to the owner. The combination of the value of the attribute and foreign key is the primary key
  7. N-ary relationships: make a new table, with the combination of the foreign keys being the primary key.
  8. Specialization/generalization:
    • Superclass has its own table with general attributes; subclasses have their own table with specific attributes; the primary key is a foreign key to superclass
    • Total specialization: tables just for the subclasses (duplicating superclass attributes)
    • Disjoint specialization: single table with attributes for every subclass, plus attribute denoting which subclass it is (called the type)
    • Overlapping specializations: same as above, but Boolean for every subclass indicating which subclasses it is a member of
  9. Categories: primary key is an artificial, automatically generated ID; all superclasses of the category have this as a foreign key

Relational Algebra

Variables

VARNAMEoperation VARNAME \leftarrow operation

Select

Project

Rename

Set

Union-compatible relations: require the same degree and pairs of corresponding attributes to have the same domain.

Union and intersection are:

Joins

RconditionS R \Join_{condition} S

Equijoin:

Natural join:

θ\theta join:

The same type of joins are available for left, right and full outer joins (symbols have two horizontal lines sticking out of the top and bottom of the left/right end of the \Join symbol).

Division

‘For every’ queries.

If R(X)R(X) and S(Z)S(Z), R÷S=T(Y)R \div S = T(Y) where Y=XZY = X - Z; columns only in SS will not appear.

For a tuple tt to appear in TT, the values in tt must appear in RR in combination with every tuple in SS.

If SS contains a single key AA and RR contains an additional column BB, then TT will be a table with a single column BB. If a value bb exists in BB, there exists a tuple (a,b)(a, b) for all values of aa found in SS.

e.g. if S=(a1),(a2)S = {(a1), (a2)} and R=(a1,b1),(a2,b1),(a1,b2)R={(a1, b1), (a2, b1), (a1, b2)} then TT will contain (a1){(a1)}. If SS is a table and RR is a table with the exact same columns plus a primary key:

Equivalent to:

T1πY(R) (getting rid of columns only in S)T2πY((S×T1)R)T1T1T2 \begin{aligned} T1 &\leftarrow \pi_Y(R) \text{ (getting rid of columns only in S)} \\ T2 &\leftarrow \pi_Y((S \times T1) - R) \\ T1 &\leftarrow T1 - T2 \end{aligned}

If R×S=TR \times S = T, then T÷S=RT \div S = R.

Explanation of division:

Given δ=πAB(α)\delta = \pi_{A-B}(\alpha):

α÷β=δπAB((δ×β)α) \alpha \div \beta = \delta - \pi_{A-B}((\delta \times \beta) - \alpha)