You are on page 1of 33

Relational Model

Presented By:-
Dr. Sanjeev Sharma
Introduction
• Data: Known facts that can be recorded and have an
implicit meaning.

• Database: A collection of related data.

• DBMS contains information about a particular


enterprise
– Collection of interrelated data
– Set of programs to access the data
– An environment that is both convenient and efficient to use
• Database Applications:
– Banking: all transactions
– Airlines: reservations, schedules
– Universities: registration, grades
– Sales: customers, products, purchases
– Online retailers: order tracking, customized
recommendations
– Manufacturing: production, inventory, orders, supply chain
– Human resources: employee records, salaries, tax
deductions
• Databases touch all aspects of our lives
Purpose of Database Systems
• In the early days, database applications were built
directly on top of file systems
• Drawbacks of using file systems to store data:
– Data redundancy and inconsistency
• Multiple file formats, duplication of information in different files
– Difficulty in accessing data
• Need to write a new program to carry out each new task
– Data isolation — multiple files and formats
– Integrity problems
• Integrity constraints (e.g. account balance > 0) become “buried” in
program code rather than being stated explicitly
• Hard to add new constraints or change existing ones
– Atomicity of updates
• Failures may leave database in an inconsistent state with
partial updates carried out
• Example: Transfer of funds from one account to another
should either complete or not happen at all
– Concurrent access by multiple users
• Concurrent accessed needed for performance
• Uncontrolled concurrent accesses can lead to inconsistencies
– Example: Two people reading a balance and updating it at the
same time
– Security problems
• Hard to provide user access to some, but not all, data
• Database systems offer solutions to all the above problems
Manages Interaction Between end
Users and Database
Example of a Database
Mini-world for the example: Part of a UNIVERSITY environment.
Some mini-world entities (Data elements):
- STUDENTs
- COURSEs
- SECTIONs (of COURSEs)
- (academic) DEPARTMENTs
- INSTRUCTORs
Some mini-world relationships:
- SECTIONs are of specific COURSEs
- STUDENTs take SECTIONs
- COURSEs have prerequisite COURSEs
- INSTRUCTORs teach SECTIONs
- COURSEs are offered by DEPARTMENTs
- STUDENTs major in DEPARTMENTs
Relational Model
• The relational model uses a collection of tables to represent
both data and the relationships among those data.

• A table is a collection of rows and columns. Each column has


a unique name.

• Each row in the table represents a collection of related data


values. In the relational model, a row is called a tuple, a
column header is called an attribute and the table is called a
relation.
Attribute and Domain
• Each attribute of a relation has a name
• The set of allowed values for each attribute is called the
domain of the attribute
• Attribute values are (normally) required to be atomic; that is,
indivisible
– E.g. the value of an attribute can be an account number,
but cannot be a set of account numbers
• Domain is said to be atomic if all its members are atomic
• The special value null is a member of every domain.
Relation Schema
• Formally, given domains D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn

Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai 


Di
• Schema of a relation consists of
– attribute definitions
• name
• type/domain
– integrity constraints
Database
• A database consists of multiple relations

• Information about an enterprise is broken up into parts, with


each relation storing one part of the information

• E.g.
account : information about accounts
depositor : which customer owns which account
customer : information about customers
Integrity Constraints
• These are the rules or constraints applied to the database to
keep data stable, accurate or consistent. To keep database
consistent we have to follow some rules known as integrity
rules or integrity constraints.
– Entity Integrity Rule (Integrity Rule 1)
– Referential Integrity Rule (Integrity Rule 2)
– Domain Constraints
– Key Constraints
Entity Integrity Rule
• Primary key or a part of it in any relation cannot be null.
Suppose A be the attribute in relation R which is taken as
primary key then A must not be null.
Referential Integrity Rule
• A foreign key can be either null or it can have only those
values which are present in the primary key with which it is
related.

• Suppose A be the attribute in relation R1, which is also the


primary key in relation R2, then value of A in R1 is either null
or same as in relation R 2.
Domain Constraints
• The restrictions which we applied on domain are known as
domain constraints. These restrictions are applied to every
value of attribute. By following these constraints you can keep
consistency in database. These restrictions include data types
(integer, varchar, char, time format, date format etc.), size of
variable, checks (like value not null etc.) etc.
• Ex. create table employee
Key Constraints
• In any relation R, if attribute A is primary key then A must
have unique value or you can say that primary key attribute
must have unique value.
• Duplicate values in primary key are invalid.
Relational Algebra
• The relational algebra is a procedural theoretical language with
operations that work on one or more relations to define another
relation without changing the original relation.

• Thus, both the operands and the results are relations; hence the
output from one operation can become the input to another
operation.
• This allows expressions to be nested in the relational algebra.
This property is called closure. Relational algebra is an
abstract language, which means that the queries formulated in
relational algebra are not intended to be executed on a
computer.
Relational Algebra Operations
• Operations may be unary or binary.
• The functional operations in the relational algebra are :
– Select
– Project
– Union
– Set difference
– Cartesian product
– Rename
– Intersection
– Division
– Join
– Natural Join
Selection Operation
• The selection operation works on a single relation R and
defines a relation that contains only those tuples of R that
satisfy the specified condition (Predicate).

• Selection operation can be considered as row wise filtering.

• The syntax of selection operation is:


σ Predicate (R)
• Here R refers to relation and predicate refers to condition.
Example database
• Ex. If you want all the employees having salary more than
9,000 from relation Employee. The query, is
• Note: We can use following predicate.
– = (equal to)
– < (less than)
– > (greater than)
– ≠(not equal to)
– ≤ (less than or equal to)
– ≥(greater than equal to)

– We can use AND and OR to combine predicate


– ˅ OR
– ˄ AND
Project
• The projection operation is a unary operation which applies
only on a single relation at a time. Project operation is used to
select vertical subset of relation (i.e., columns of table). It is
denoted by pi (π).
• Consider If you want all the names of employees and their
salary from relation employee. Then query, is

π name, salary (Employee)

Output of the query is:-


Basic Set-oriented Operations
• The union operation : The union operation is a binary
operation that is used to find union of relations. Here relations
are considered as sets. So, duplicate values are eliminated. It is
denoted by (∪).
• Conditions for union operation : There are two necessary
conditions for union operation.
– (i) Both the relations have same number of attributes.
– (ii) Data types of their corresponding attributes must be same.
• Two relations are said to be union compatible if they follow
the above two conditions.
• Ex. If you want to find the names of all employees and
names of all students together
• then the query is
πName (Employee) ∪ πName (Student)
Intersection
• Set intersection operation : Set intersection is used to find
common tuples between two relations. It is denoted by (∩).
• If you want to find all the employees from Relation Employee
those are also students. Rules of set union operations are also
applicable here. Then the query, is
πName (Employee) ∩ πName (Student)
Difference
• Set-difference operation : Set-difference operation is a binary
operation which is used to find tuples that are present in one
relation but not in other relation. It is denoted by (—). It
removes the common tuples of two relations and produce a
new relation having rest of the tupels of first relation.
• Ex. If you want the names of those employees that are not
students, then the query, is
πName (Employee) — πName (Student)
• Cartesian product operation : Cartesian product is a binary
operation which is used to combine information of any two
relations. Suppose a relation R1 is having m tuples and other
relation R2 is having n tuples then R1 × R2 has m × n tuples. It
is denoted by (X).

• Consider the Figure. Cartesian product of relation Employee


and Job is shown in Figure .

Query is → Employee × Job

You might also like