0% found this document useful (0 votes)
43 views23 pages

Lecture #3-1. Relational Model

The document discusses relational databases and the relational model. It covers several key topics in 3 sentences or less: 1. The relational model structures data into relations (tables) with attributes and tuples (rows), and defines integrity constraints like primary keys and foreign keys. 2. Primary keys uniquely identify each tuple in a relation and cannot be null, while foreign keys in one relation must match the primary key values in another relation. 3. The relational model also defines relational algebra operations that can be performed on relations and rules defined by Codd for relational databases.

Uploaded by

Ronald
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views23 pages

Lecture #3-1. Relational Model

The document discusses relational databases and the relational model. It covers several key topics in 3 sentences or less: 1. The relational model structures data into relations (tables) with attributes and tuples (rows), and defines integrity constraints like primary keys and foreign keys. 2. Primary keys uniquely identify each tuple in a relation and cannot be null, while foreign keys in one relation must match the primary key values in another relation. 3. The relational model also defines relational algebra operations that can be performed on relations and rules defined by Codd for relational databases.

Uploaded by

Ronald
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Databases

Lecture #3. Relational model


Organizational issues
1. Use this form to confirm your presence at a lecture
2. Next lecture will be a test

2
The Retrospective of Lecture #2: ER-
modelling
1. The structure: entity, attribute, relationship.
2. Cardinality: 1:1, 1:N, M:N. Optional & mandatory.
3. Attributes: unary, binary, n-ary.
4. ER should not be like exact “tables”.
5. EER: generalization, specialization, aggregation.

3
Relational model
1. Structure (a relation only).
2. Integrity constraints (the accuracy and consistency of data).
3. Relational Algebra: functional aspect (relational operations).

Created by Edgar Codd in 1969, RM is the mathematical background of SQL.

Not only is it framework for constructing SQL operators like SELECT but also
this is important for DBMS optimizer to find an optimal execution plan.

It’s based on set theory primarily.

4
12 relational model rules by E.Codd (1)

0. Foundation Rule: For any system that is advertised as, or claimed to be, a
relational database management system, that system must be able to
manage databases entirely through its relational capabilities.

1. Information Rule: Data stored in Relational model must be a value of


some cell of a table.

2. Guaranteed Access Rule: Every data element must be accessible by table


name, its primary key and name of attribute whose value is to be determined.

3. Systematic Treatment of NULL values: NULL value in database must only


correspond to missing, unknown or not applicable values.
5
12 relational model rules by E.Codd (2)
4. Active Online Catalog: Structure of database must be stored in an online
catalog which can be queried by authorized users.

5. Comprehensive Data Sub-language Rule: A database should be


accessible by a language supported for definition, manipulation and
transaction management operation.

6. View Updating Rule: Different views created for various purposes should
be automatically updatable by the system. (*does not work in practice)

7. High level insert, update and delete rule: Relational Model should
support insert, delete, update etc. operations at each level of relations. Also,
set operations like Union, Intersection and Minus should be supported. 6
12 relational model rules by E.Codd (3)
8. Physical data independence: Any modification in the physical location of a table
should not enforce modification at application level.

9.Logical data independence: Any modification in logical or conceptual schema of a


table should not enforce modification at application level. For example, merging of two
tables into one should not affect application accessing it which is difficult to achieve.

10. Integrity Independence: Integrity constraints modified at database level should not
enforce modification at application level.

11. Distribution Independence: Distribution of data over various locations should not
be visible to end-users. (it is only 1 server for user view)

12. Non-Subversion Rule: Low level access (single-record-at-time) to data should not be
able to bypass integrity rule to change data (multiple-record-at-time). (modern RDBMS
violates by using bulk insertion and constraint disabling) 7
Structural aspect: Relation
The relation is defined as a subset of the Cartesian product.

In turn, the Cartesian product D1 × D2 ×,…, × Dn for given finite sets D1, D2,
…, Dn (optionally different) is called the set of products of the form

d1 × d2 ×… × dn, where n> 0, d1 ∈ D1, d2 ∈ D2,… dn ∈ Dn.

The sets D1, D2,…, Dn are called domains.

8
Cartesian product & relation example
Here is an example of a Cartesian product.

Suppose that two sets D1 = {1,2,3} and D2 = {a, b} are given, then the
Cartesian product D will be:

D = D1 × D2 = {1 × a, 1 × b, 2 × a, 2 × b, 3 × a, 3 × b} = cartesian product

Therefore, the relation defined on the sets D1, D2,…, Dn is a subset of D1 × D2


×,…, × Dn.

For this example, the relations are (1 × a, 3 × a), (2 × a), (1 × a, 1 × b, 2 × a, 2 ×


b), (1 × a, 1 × b, 2 × a, 2 × b, 3 × a, 3 × b), Ø and others. It should be noted that
the empty set Ø is also a correct relation. Elements of relations are tuples. 9
Cartesian product example #1

10
Cartesian product example #2 Books

Authors book_id book_name book_pages


author_id author_name 1 C++ 300
1 a1 2 C# 800
2 a2 3 Java 700

author_id author_name book_id book_name book_pages

1 a1 1 C++ 300 Cartesian product:

1 a1 2 C# 800 Rows = rows (Authors) *


rows (Books) = 2*3 = 6
1 a1 3 Java 700
Columns =
2 a2 1 C++ 300
Columns(Authors) +
2 a2 2 C# 800 Columns(Books) = 2+3 = 5

2 a2 3 Java 700
11
Relation: graphical representation

12
Properties of Relations
● Name of the relation is distinct from all other relations.
● Each attribute contains a distinct name.
● No Duplicate Tuples – A relation cannot contain two or more tuples
which have the same values for all the attributes. i.e., In any relation,
every row is unique.
● Tuples are unordered – The order of rows in a relation is immaterial.
● Attributes are unordered – The order of columns in a relation is
immaterial.
● Attribute Values are Atomic – Each tuple contains exactly one value for
each attribute.
13
Integrity constraints
1. Entity Integrity Constraints (primary key)
2. Referential Integrity Constraints (foreign key)
3. Domain Constraints (not null, default, check, unique)
4. User-defined, operational Constraints (eg., “The begin date must be
before the end date”)

14
Keys
Super Key: The set of attributes which can uniquely identify a tuple is known
as Super Key.

Candidate Key: The minimal set of attributes which can uniquely identify a
tuple is known as candidate key.

Primary Key: There can be more than one candidate key in relation out of
which one can be chosen as the primary key.

Alternate Key: The candidate key other than the primary key is called an
alternate key

Foreign Key: If an attribute can only take the values which are present as
values of some other attribute (primary key), it will be a foreign key to the
15
attribute to which it refers.
Keys examples
Users
id name email birth_date gender boss

1 n1 [email protected] 01.01.2000 m 2

2 n2 [email protected] 01.02.2002 w

Super Key: (id), (email), (name, email, gender) ...

Candidate Key: (id), (email)

Primary Key: (id)

Alternate Key: (email)

Foreign Key: (boss ==> id) 16


Entity Integrity Rule: Primary key
Primary Key (>=1 attributes) uniquely identifies each instance (tuple) of the
entity (relation).

Properties of primary keys: id1 id2

1 1
- “unique together”
- NULL is disallowed 1 2

1 2
Every relation must have PK
NULL 5
Try to use “natural” PK

17
NULL value
The special value in relational databases is NULL.

This value is not identical to a logical "False", a numeric "0" or an empty


string.

By the definition of the primary key, the attributes included in it cannot take
the value NULL.

Other default attributes can be set to this value (although it is possible to


disable this feature by specifying a NOT NULL constraint).

18
NULL value
Logical operations NOT, AND and OR are generally performed in three-digit
logic, where: 1 = True, 0.5 = NULL, 0 = False.

The calculation of these functions is performed according to the general rules:


AND as MIN (a, b), OR as MAX (a, b) and NOT as 1-a, where a, b are the
arguments of the functions.

For example, 1 AND NULL = NULL, 0 OR NULL = NULL, 1 OR NULL = 1, NOT


(NULL) = NULL. However, NULL ≠ NULL, and the expression NULL = NULL
returns NULL instead of True. IS NULL =NULL

Recommendation: avoid or minimize “storing” of NULL (count(*), count(field)


example) 19
Referential Integrity Constraints: foreign
key
Another type of constraint is the integrity of references, which connects two
relationships according to the scheme "main" - "subordinate" or as a
relationship "one to many", where "one" corresponds to the main
relationship, and "many" - the subordinate.

In this case, for each value of the foreign key, which is defined in the
subordinate relation, there must be a tuple with the same value of the
primary key in the main relation (see lecture #2: ER to Relational examples).

Delete/Update issues when Primary tuple is being Deleted/Updated:

Perform cascade operation or disable operation or set NULL (if there is


20
no NOT NULL constraint)
Domain Constraints
A domain is the set of possible values of an attribute. All values entered into a
column must be from the same domain.

The constraints on a domain include its domain name, description, data type,
size, and the set of allowable values. Other domain constraints may include
default value, number of decimal places, format, maximum and minimum
value. Each attribute is assigned to one domain constraint. Generally:
DEFAULT, UNIQUE, NOT NULL, CHECK.

Example. “Username” attribute:

Domain Name: UserName, Description: Name of user. May be First or/and


Surname. Data Type: Character, Max Length: 40 characters, Allowable
21
Values: 'A'-'Z', 'a'-'z', and '-', Default Value: ' ', NOT NULL
User-defined, operational Constraints
There may be a number of business rules relating to the Offering entity.

Usually implemented with SQL or database application logic.

Examples:

An library log item must have a begin date and an end date.

The begin date must be before the end date.

Course must have a minimum number of students equals to 5.

22
Relational algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output.

It uses operators to perform queries.

An operator can be either unary or binary.

They accept relations as their input and yield relations as their output.

23

You might also like