Module 2 part 1: The Relational Data Model

The relational model

Relations:

Relations are not tables

Relations have specific properties, based on the mathematical set theory

alt text

Relational Components:

alt text

Component type Example and description
Domain Types A domain D is a set of atomic values. An atomic value is indivisible (as far as the relational data model is concerned)

Examples:

- Integers
- Numbers and currency
- Fixed or variable length character strings
- Date, timestamp
- Sub-range from a data type
- e.g.,1 £ grade £ 7
- Enumerated data type
- e.g. Gender in {‘Male’, ‘Female’, ‘Other’}
- Australian telephone numbers
- Format: the digits “61” followed by 9 digits 0 - 9
- Car registration numbers
- Format: 6 characters (either alpha or digits but no ‘Q’s allowed)
Attributes Each attribute AA is the name of a role played by some domain DD in the relation named RR.

Example: salary is an attribute name
alt text
Degree The number of attributes in a relation RR is called the degree of RR
Example - Exmployee relation of degree 4
alt text
Tuples Each tuple t is an ordered list of n values:
t=<v1,v2,...vn>t = <v_{1}, v_{2}, ... v_{n}>
where each value vi(1in)v_{i}(1\le i \le n) is an element of the corresponding domain of attribute A i or a special value called “NULL”

Domain attribute restrictions:

Relation Schema and Instance

Relation Schema

Relation Instance

Relation Schema Relation Instance
alt text alt text

Primary Keys

A schema may have more than one key

Foreign Keys

To preserve relationships, you may need to create a foreign key (FK). A foreign key is a primary key from one table placed into another table.

alt text

Example

THIS MEANS

alt text


Integrity Constraints

Domain constraint

Key constraint

alt text

Entity Integrity Constraint

An entity integrity constraint violation occurs when a tuple is inserted or modified such that part of its primary key contains the value NULL.

For primary keys that consists of multiple attributes, no part of the primary key can be null.

alt text

Referential Integrity Constraint

A referential integrity constraint can be utilised to guarantee that a department with department number 2 exists before the “Grace Mills” tuple is stored.

alt text

Semantic Integrity Constraint

Semantic integrity constraints are generally defined by the business or organization during client consultation.

Semantic constraints can be used to enforce organisation policies such as:

alt text


Constraints and Operations

Enforcement of integrity constraints ensures that the database remains consistent.

Changes to the database such as insert, modification and deletion must not violate integrity constraints (leave the database in an inconsistent state).

If a database update is submitted to the DBMS that would violate integrity, it must be rejected.

The Transaction Concept

A transaction is an executing program that includes some database operations, such as reading from the database, or applying insertions, deletions, or updates to the database. At the end of the transaction, it must leave the database in a valid or consistent state that satisfies all the constraints specified on the database schema.

alt text

Issues

alt text

Problem: Cannot create a new department since it has no employees.

Solution: Use a transaction to insert information about a new department and its employee at the same time.