Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

RELATIONAL MODEL

This model uses the concept of a mathematical relation.


The first commercial implementations of the relational model became available in
the early 1980s.
The model has been implemented in a large number of commercial systems. Current
popular relational DBMSs (RDBMSs) include:
• DB2 and Informix Dynamic Server (from IBM),
• Oracle and Rdb
• Sybase DBMS
• SQLServer and Access (from Microsoft).
In addition, several open source systems are available, such as:
• MySQL,
• PostgreSQL.
• Sqlite
• Base (Libre Office)
• Data Base (Open Office)

1
The relational model represents the database as a collection of relations.
Informally, each relation resembles a table of values in which each row represents
a collection of related data values:
• a row represents a fact that typically corresponds to a real-world entity or
relationship. It is called “Tuple”
• a column name specifies how to interpret the data values in each row, based on
the column each value is in. All values in a column are of the same data type. A
column header” is called “Attribute”

2
Attributes and Domains

An attribute belongs to a Domain that describes the role played by the attribute.
A Domain is specified by:

• Name: For example Usa_phone_numbers (set of ten-digit phone numbers


valid in the United States) or Employee_ages (set of digits that represent the
age of persons)
• Data type: string of characters or integer or decimal
• Format: yyyy,mm,dd where yyyy represents the year, mm represents the
month and dd represents the day.
In the Relational model a
As we saw in Entity-Relation model, an attribute compound and multivalued
can be: attribute are not allowed. In a
Simple or atomic attributes: Compound attributes:
They can be split in other
relation can be only a single or
They cannot be split in other or attributes: atomic attribute.
attributes:
Name, age, salary etc

Single attributes: Multi-valued attributes: • multivalued attributes must be


They assume only a single They assume more values for
value for each instance. each instance.
represented by separate relations
An employee can be only an or An employee can have more • composite attributes are represented
age. tasks. only by their simple component
A car can have more colors
attributes 3
Relation and Tuples

A relation (or table), is a set of n-tuples. Each tuple is an ordered list of n values,
where each value is an element of domain or is a special NULL value.

Attribute

Tuple

Domain value

In the example, each tuple in the relation represents an individual employee.


The relation is displayed as a table, where each tuple is shown as a row and each
attribute corresponds to a column header indicating a role.

4
NULL VALUES

NULL values represent attributes whose values are unknown or do not exist for
some value in the tuple.
For example some EMPLOYEE tuples have NULL for their office phone because
they do not have an office.
Another employee has a NULL for home phone, presumably because either he
does not have a home phone or he has one but we do not know it (value is
unknown).
In general, we can have several meanings for NULL values, such as:
• value unknown,
• value exists but is not available,
• value undefined. The attribute does not apply to this tuple.

An attribute can be qualified with the relation’s name to which it belongs by using
the dot notation. For example, EMPLOYEE.Age or STUDENT.Age. This is because
the same name may be used for two attributes in different relations.
5
Relational model constraint
In a relational database, there will typically be many relations, and the tuples in those
relations are usually related in various ways. There are generally many restrictions or
constraints on the actual values in a database state. These constraints are derived
from the rules in the mini-world that the database represents.
The schema-based constraints include:
• domain constraint,
• key constraint,
• Constraint on NULLs,
• entity integrity constraint,
• referential integrity constraint, and foreign key.

Domain constraint
It specifies that within each tuple, the value of each attribute must be an atomic
value from the specified domain. As we saw a domain has to have a Data type (string
of characters or integer or decimal etc) and /or a Format (yyyy,mm,dd - etc).
For example in the Employee Relation the attribute EMPLOYEE.Age mast be an
integer between 19 and 67.

6
Key constraints
It specifies that two tuples cannot have the same combination of values for all their
attributes. For this reason the set of attributes belonging to a tuple is called a
superkey.
A superkey can have redundant (in excess) attributes, for this, we use the concept of
key.

A key of a relation is a superkey with the additional property that any redundant
attribute (in excess) is removed. A key satisfies two properties:

1. Two distinct tuples cannot have identical values for all the attributes in the key.
This first property also applies to a superkey.
2. It is a minimal superkey, that is a superkey from which we cannot remove any
attributes and still have the uniqueness constraint in condition 1 hold.

In general, a relation may have more than one key. In this case, each of the keys is
called a candidate key.

superkey

candidate key

candidate key 7
So, the key is one or a set of attributes of a relation that identifies uniquely a tuple.

It is common to designate one of the candidate keys as the primary key of the
relation. We use the convention that the attributes that form the primary key of a
relation schema are underlined.

If we assume that the License_number is the attribute that identifies uniquely a tuple
of the Car relation, this attribute becomes the Primary key.

Primary key

Constraints on NULLs

Another constraint on attributes specifies whether NULL values are or are not
permitted.
For example, if every Employee tuple must have a valid, non-NULL value for the Name
attribute, then Name of EMPLOYEE is constrained to be NOT NULL.
8
Entity integrity constraints

The entity integrity constraint states that no primary key value can be NULL. This is
because the primary key value is used to identify individual tuples in a relation.
Having NULL values for the primary key implies that we cannot identify any tuples.

Previous constraints: domain - key - on NULLs - entity integrity, are specified on a


single relation.

Referential integrity constraint

The referential integrity constraint is specified between two relations and is used to
maintain the consistency among tuples in the two relations.
It states that a tuple in one relation that refers to another relation, must refer to an
existing tuple in that relation.

9
We can better specified the referential integrity constraint by introducing the concept
of Foreign Key.

If in the relational model there are two connected relations (A and B), a Foreign Key is
one or a set of attributes of A that overlap the Primary Key of relation B.

The table containing the foreign key is the table that references.
The connected table, containing the primary key, is the referenced table.

A foreign key is a key of access to the connected relation.

Primary key Foreign key

Primary key

10
A foreign key can also refer to its own relation. A foreign key of this type is also
called auto-referencing or recursive.

For example, the attribute Super_ssn in EMPLOYEE refers to the supervisor of an


employee; this is another employee, represented by a tuple in the same
EMPLOYEE relation. Hence, Super_ssn is a foreign key that references the
EMPLOYEE relation itself.

In the example the tuple for employee ‘John Smith’ references the tuple for
employee ‘Franklin Wong,’ indicating that ‘Franklin Wong’ is the supervisor of
‘John Smith.’

11
We can diagrammatically display referential integrity constraints by drawing a
directed arc from each foreign key to the relation it references. For clarity, the
arrowhead may point to the primary key of the referenced relation.

All integrity constraints should be specified.

Most relational DBMSs support key, entity integrity, and referential integrity
constraints.
12
Other Types of Constraints

Other types of constraint are not included in the data definition language, are
semantic integrity constraints.

Examples of such constraints are:


• the salary of an employee should not exceed the salary of the employee’s
supervisor
• The maximum number of hours an employee can work on all projects per
week is 56.

Such constraints can be specified and enforced within the application programs
that update the database, or by using a general-purpose constraint specification
language.

13
Update Operations on relations and Constraint Violations

There are three basic operations that can change the states
of relations in the database:

• Insert: Used to insert one or more new tuples in a relation

• Delete: Used to delete tuples

• Modify (or update): used to change the values of some attributes in existing
tuples

If these operations are applied, the integrity constraints specified on the relational
database schema must not be violated.

14
Insert
It can violate any of the five types of constraints discussed.

• Domain constraints can be violated if an attribute value does not


appears in the corresponding domain or it is not of the appropriate
data type.
• Key constraints can be violated if a key value in the new tuple already exists

• The constraint on NULL can be violated if an attribute cannot be NULL and you want to insert a
NULL value for that attribute.

• Entity integrity can be violated if any part of the primary key of the new tuple is NULL.

• Referential integrity can be violated if the new value assigned to a foreign key, refers to a tuple
that does not exist in the referenced relation.

15
If an insertion violates one or more constraints, the default option is to reject the
insertion.

In this case, it would be useful if the DBMS could provide a reason to the user as to
why the insertion was rejected.

Another option is to attempt to correct the reason for rejecting the insertion, but
this is typically not used for violations caused by Insert;

16
Delete

The Delete operation can violate only referential


integrity. This occurs if the tuple being deleted is
referenced by a foreign key belonging to one or
more than one tuple of other tables in the
database.

Examples 1
Operation: Delete the WORKS_ON tuple with Essn =
‘999887777’ and Pno = 10.
Result: This deletion is acceptable and deletes exactly one
tuple.

foreign keys

17
Delete

Examples 2

Operation: Delete the EMPLOYEE tuple with Ssn = ‘999887777’.


Result: Not acceptable. There are tuples in WORKS_ON that
refer to this tuple. Hence, if the tuple in EMPLOYEE is deleted,
referential integrity violations will result.

foreign keys

18
Delete

Examples 3

Operation: Delete the EMPLOYEE tuple with Ssn = ‘333445555’.


Result: Not acceptable. The tuple involved is referenced by
tuples from the EMPLOYEE, DEPARTMENT,WORKS_ON, and
DEPENDENT relations.

foreign keys

19
Modify (or Update)

The Update (or Modify) operation is used to change


the values of one or more attributes in a tuple.
It is necessary to specify a search condition on the
attributes of the relation for selecting the tuple to
be modified.

Examples 1
Operation: Update the salary of the EMPLOYEE tuple with Ssn =
‘999887777’ (search condition) to 28000.
Result: Acceptable.

In the update operation is not involved any key attribute.

20
Modify (or Update)

Examples 2

Operation: Update the Dno of the EMPLOYEE tuple with Ssn =


‘999887777’ to 1.
Result: Acceptable.

In the update operation is involved a foreign key attribute, but its


new value references a value that is already present in the linked
Department table.

21
Modify (or Update)

Examples 3

Operation: Update the Dno of the EMPLOYEE tuple with Ssn =


‘999887777’ to 7.
Result: Unacceptable, because it violates referential integrity.

In the update operation is involved a foreign key attribute, but its


new value doesn’t reference any value in the linked Department
table.

22
Modify (or Update)

Examples 4

Operation: Update the Ssn of the EMPLOYEE tuple with Ssn =


‘999887777’ to ‘987654321’.

Result: Unacceptable.

The update operation violates primary key constraint by repeating


a value that already exists as a PK in another tuple in the same
table.

23
Several options are available if an operation causes
a violation. These options are:
Restrict, Cascade and Set Null option.

• Restrict: is to reject the deletion.

• Cascade: is to attempt to cascade (or propagate)


the deletion by deleting tuples that reference
the tuple that is being deleted.

24
• Set null or set default: used to modify the values of the reference attributes
that cause the violation; each of these values is set to NULL value or changed to
a default value to reference an existing tuple.

1 example: Delete Department with Dnumber = 5 and Set default = 4


Result: Update each record of employee with Dno 5 to 4

2 example: Delete Department with Dnumber = 5 and Set NULL


Result: Update each record of employee with Dno 5 to NULL

25
The Transaction Concept

A transaction is an executing program that includes some database operations,


such as:

• reading from the database


• applying insertions,
• deletions,
• updates on 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.
A large number of commercial applications running against relational databases
in online transaction processing systems (OLTP), can execute transactions at
rates that reach several hundred per second.
26

You might also like