0% found this document useful (0 votes)
2 views51 pages

2 - Relational Model

Uploaded by

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

2 - Relational Model

Uploaded by

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

Relational

Model
28-Apr-25 Dept. of I&CT 1
Data Model
• Data Model is a collection of concepts that can be
used to describe the structure of a database
• Structure of a database:
• Data types,
• Relationships, and
• Constraints that apply to the data

• Data models also include

• Set of basic operations for specifying retrievals and


updates on the database
28-Apr-25 Dept. of I&CT 2
Categories of Data Models
1. High-level or conceptual data models provide concepts that are close to the way
many users perceive data
• (Also called entity - based or object - based or semantic data models)

2. Low-level or physical or internal data models provide concepts that describe the
details of how data is stored on the computer storage media, typically magnetic
disks
- Not for end users but for specialists

3. Implementation (representational) data models: Provide concepts that fall between


the above two, balancing user views with some computer storage details.

28-Apr-25 Dept. of I&CT 3


Entity, Attribute,
Relationships
• An entity represents a real-world object or concept,
• employee or a project from the miniworld that is described in the database

• An attribute represents some property of interest that further describes an


entity,
• such as the employee’s name or salary.

• A relationship among two or more entities represents an association among


the entities,
• works-on relationship between an employee and a project.

28-Apr-25 Dept. of I&CT 4


28-Apr-25 Dept. of I&CT 5
• Database Schema:
• Description of a database, includes
descriptions of the database structure;

Schemas, • The constraints that should hold on the

Instances database and is not expected to change


frequently.
and
Database • Schema Diagram:
State • A diagrammatic display of (some aspects of)
a database schema

• displays only some aspects of a schema, such


as the names of record types and data items,
and some types of constraints
28-Apr-25 Dept. of I&CT 6
Schemas, Instances and Database
State

28-Apr-25 Dept. of I&CT 7


Schemas, Instances and Database
State
Schema Construct: A component of the schema or an object
within the schema, e.g., STUDENT, COURSE
Database Instance: The actual data stored in a database
at a particular moment in time, also called database
state (or occurrence).
Initial Database State: Refers to the DB when it is
loaded or populated with first initial data.
Valid State: A state that satisfies the structure and
constraints of the database specified in the schema.
Schema is also called intension, whereas state is called
extension

28-Apr-25 Dept. of I&CT 8


Three – Schema Architecture

28-Apr-25 Dept. of I&CT 9


Three - Schema Architecture
• Goal: to separate the user applications from the
physical database.
• Proposed to support DBMS characteristics of:

- Program-data independence.
- Support of multiple views of the data
• Three levels:
1. Internal level has an internal schema that describes
the physical storage structure of the database.
- Uses a physical data model and describes the
complete details of data
28-Apr-25 Dept. ofstorage
I&CT and access paths for
10
Three - Schema Architecture
1. Internal level : internal schema that describes the
physical storage structure of the database.
• Uses a physical data model and describes the complete details of data
storage and access paths for the database.

2. Conceptual level: conceptual schema, which describes the


structure of the whole database for a community of users.
• Hides the details of physical storage structures and concentrates on
describing entities, data types, relationships, user operations, and
constraints

3. External or view level includes several external schemas


or user views.
• Each
28-Apr-25 external schema describes the
Dept. part
of I&CTof the database that a particular 11user
Structure of Relational
Databases
• A relational database consists of a collection of tables,
each of which is assigned a unique name
• A tuple is simply a sequence (or list) of values
• Relation instance is a specific instance of a relation, i.e.,
containing a specific set of rows
• For each attribute of a relation, there is a set of permitted
values, called the domain of that attribute.
• A domain is atomic if elements of that domain are considered
to be indivisible units.
• The null value is a special value that signifies that the
value
28-Apr-25is unknown or does not
Dept.exist.
of I&CT 13
Structure of Relational
Databases
• A relation (or relation state) r of the relation
schema
R(A1, A2, ..., An), also denoted by r(R), is a set
of n-tuples
r = {t1, t2, ..., tm}.

• Each n-tuple t is an ordered list of n values t =


<v1, v2, ..., vn >, where each value vi , 1 ≤ i ≤ n,
is an element of dom(Ai ) or is a special NULL value.
28-Apr-25 Dept. of I&CT 14
Structure of Relational
Databases

28-Apr-25 Dept. of I&CT 15


Keys
• There must be a way to specify how tuples within a given relation are
distinguished, expressed in terms of the attributes.

• The attribute values of a tuple must be such that they can uniquely identify
the tuple i.e., no two tuples in a relation are allowed to have exactly the
same value for all attributes.

• A superkey is a set of one or more attributes that, taken collectively,


allow us to identify uniquely a tuple in the relation.

• If K is a superkey, then so is any superset of K.

• A key K of a relation schema R is a superkey of R with the additional


property that removing any attribute A from K leaves a set of attributes K’
that is not a superkey of R anymore
28-Apr-25 Dept. of I&CT 16
Keys
• A key satisfies two properties:

• Two distinct tuples in any state of the relation cannot have identical
values for (all) the attributes in the key.

• It is a minimal superkey/candidate key - that is, a superkey from which


we cannot remove any attributes and still have the uniqueness constraint in
condition 1.
• STUDENT(Name, Ssn, Home phone, Address, Office phone, Age, Gpa)
• For the given relation STUDENT, {Ssn} is the key; {Ssn, Name, Age} - is a
superkey but it is not a minimal superkey!

• Any superkey formed from a single attribute is also a key whereas a key with
multiple attributes must require all its attributes together to have the
uniqueness
28-Apr-25 property. Dept. of I&CT 17
Keys
• There is a possibility of having more than one
candidate key for a relation
• Term primary key is used to denote the chosen
candidate key
• Primary key attributes are also underlined
• Other candidate keys are designated as unique keys

28-Apr-25 Dept. of I&CT 18


Keys
• A relation, say r1, may include among its attributes the
primary key of another relation, say r2, and this
attribute is called foreign key from r1, referencing r2.

• Relation r1 is also called the referencing relation of


the foreign key dependency, and r2 is called the
referenced relation of the foreign key

• Referential integrity constraint requires that the


values appearing in specified attributes of any tuple in
the referencing
28-Apr-25 relation
Dept. ofalso
I&CT appear in specified
19
Schema Diagram
• A database schema, along with primary key and foreign key dependencies, can be
depicted by schema diagrams

28-Apr-25 Dept. of I&CT


Schema For University Mini World

28-Apr-25 Dept. of I&CT


Example of a Database(with a Conceptual
Data Model)
• Mini-world for the example:
• Part of a UNIVERSITY environment.
• Some mini-world entities:
• STUDENTs
• COURSEs
• SECTIONs (of COURSEs)
• (academic) DEPARTMENTs
• INSTRUCTORs

28-Apr-25 Dept. of I&CT 22


Example of a Database (with a Conceptual Data Model)
• 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

• Note: The above entities and relationships are typically


expressed in a conceptual data model, such as the ENTITY-
RELATIONSHIP data model (see Chapters 3, 4)

28-Apr-25 Dept. of I&CT 23


Example of a simple database

28-Apr-25 Dept. of I&CT 24


Schema Diagram for University Mini World Database

28-Apr-25 Dept. of I&CT


• TEACHES

28-Apr-25 Dept. of I&CT 26


Relational Query Languages
• A query language is a language in which a user
requests information from the database.
• Categorized as procedural or non-procedural
- Procedural: The user instructs the system to
perform a sequence of operations on the database to
compute the desired result.
- Nonprocedural: user describes the desired
information without giving a specific procedure for
obtaining that information.

28-Apr-25 Dept. of I&CT 27


Relational Query Languages
• In a procedural query language, like Relational
Algebra, you write a query as an expression
consisting of relations and Algebra Operators, like
join, cross product, projection, restriction, and so
on.
• On the contrary, query SQL are called “non-
procedural” since they express the expected result
only through its properties, and not the order of
the operators to be performed to produce it.

28-Apr-25 Dept. of I&CT 28


Relational Algebra
• Created by Edgar F Codd at IBM in 1970
• Procedural language
• Six basic operations
 Select: σ
 Project: П
 Union: ᴗ / U
 Set difference: -
 Cartesian Product: x
 Rename: ρ
 Join:
• The operators take one or two relations as inputs and
produce a new relation as a result
28-Apr-25 Dept. of I&CT 29
28-Apr-25 Dept. of I&CT 30
Selection of Tuples
• Notation:  p(r)
• p is called the selection predicate A B C D
• Defined as:
  1 7
p(r) = {t | t  r and p(t)}   5 7

Where p is a formula in propositional calculus   12 3


consisting of terms connected by :  (and),    23 10
(or),  (not)

• Each term is one of:  A=B ^ D > 5 (r)


<attribute> op <attribute> or
<constant> A B C D
where op is one of: =, , >, . <. 
  1 7
• Example of selection:   23 10

28-Apr-25  branch_name=“Manipal”(account)
Dept. of I&CT 31
Project Operation – Example
• Relation r: A B C

 10 1
 20 1
 30 1
 40 2

A,C (r)
A C A C

 1  1

 1 =  1

 1  2

 2
28-Apr-25 Dept. of I&CT 32
Project Operation
• Notation
 A1 , A2 ,, Ak (r )
where A1, A2 are attribute names and r is a
relation name.
• The result is defined as the relation of k columns
obtained by erasing the columns that are not
listed
• Duplicate rows removed from result, since
relations are sets
• Example: To eliminate the branch_name attribute of
account

28-Apr-25 account_number,Dept. of I&CT


balance (account) 33
Union Operation
• Notation: r  s
• Defined as:
r  s = {t | t  r or t  s}
• For r  s to be valid.
1. r, s must have the same arity (same number of
attributes)
2. The attribute domains must be compatible (example:
2nd column
of r deals with the same type of values as does
the 2nd
column of s)
• Example:
28-Apr-25 to find all customers
Dept. of I&CT with either an account
34
Union Operation – Example
• Relations r, A B A B
s:
 1  2
 2  3
 1
s
r

A B
 r  s:
 1
 2
 1
 3
28-Apr-25 Dept. of I&CT 35
Set Difference Operation
• Notation r – s
• Defined as:
r – s = {t | t  r and t  s}

• Set differences must be taken between compatible


relations.
• r and s must have the same arity
• attribute domains of r and s must be compatible

28-Apr-25 Dept. of I&CT 36


Set Difference Operation
– Example
• Relations r, A B A B
s:
 1  2
 2  3
 1
s
r

A B

r – s:  1
 1

28-Apr-25 Dept. of I&CT 37


Cartesian-Product Operation
• Notation r x s
• Defined as:
r x s = {t q | t  r and q  s}

• Assume that attributes of r(R) and s(S) are disjoint.


(That is, R  S = ).
• If attributes of r(R) and s(S) are not disjoint, then
renaming must be used.

28-Apr-25 Dept. of I&CT 38


Cartesian-Product Operation –
Example
 Relations r, s:
 r x s:
A B C D E
A B C D E

 1
 10 a
 2  10 a
 20 b  1  10 a
 10 b  1  10 a
r  1  20 b
 1  10 b
 2  10 a
s  2  10 a
 2  20 b
 2  10 b

28-Apr-25 Dept. of I&CT 39


Composition of Operations
• Can build expressions using multiple operations
• Example: A=C(r x As)B C D E
•r x s  1  10 a
 1  10 a
 1  20 b
 1  10 b
 2  10 a
 2  10 a
 2  20 b
 2  10 b

A B C D E

  10 a
• A=C(r x s) 
1
 10 a
2
 2  20 b
28-Apr-25 Dept. of I&CT 40
Rename Operation
• Allows us to name, and therefore to refer to, the results
of relational - algebra expressions.
• Allows us to refer to a relation by more than one name.
• Example:  x (E)
returns the expression E under the name X
• If a relational-algebra expression E has arity n, then

 x ( A ,A ,...,A ) (E )
returns the result of expression E under the name X, and
1 2 n

with the
attributes renamed to A , A2 , …., An . 1

• List the salary except the highest salary


∏28-Apr-25
instructor.salary (σinstructor.salary < d.salary (instructor
Dept. of I&CT
× ρd (instructor))) 41
Set-Intersection Operation –
Example
• Relation r, s:
A B A B

 1
 2
 2
 3
 1

r s

•r  s
A B

 2

28-Apr-25 Dept. of I&CT 42


Natural-Join Operation
 Notation: r s
• Let r and s be relations on schemas R and S
respectively.
Then, r s is a relation on schema R  S
obtained as follows:
• Consider each pair of tuples tr from r and ts from s.
• If tr and ts have the same value on each of the attributes
in R  S, add a tuple t to the result, where
• t has the same value as tr on r
• t has the same value as ts on s
• Example:
R = (A, B, C, D)
S = (E, B, D)
• Result schema = (A, B, C, D, E)
• 28-Apr-25
r s is defined as: Dept. of I&CT 43
Natural Join Operation –
Example
• Relations
r, s:  r s

B D E
A B C D A B C D E

 1  a 1 a   1  a 
 2  a 3 a   1  a 
 4  b 1 a   1  a 
 1  a 2 b   1  a 
 2  b 3 b   2  b 

r s

28-Apr-25 Dept. of I&CT 44


Banking Example
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street,
customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)

28-Apr-25 Dept. of I&CT 45


Example Queries
• Find all loans of over $1200
amount > 1200 (loan)

• Find the loan number for each loan of an amount greater than $1200

loan_number (amount > 1200 (loan))

28-Apr-25 Dept. of I&CT 46


Example Queries
• Find the names of all customers who have a loan, an
account, or both, from the bank
customer_name (borrower)  customer_name (depositor)

• Find the names of all customers who have a loan and an account at bank.

customer_name (borrower)  customer_name (depositor)

28-Apr-25 Dept. of I&CT 47


Example Queries
• Find the names of all customers who have a loan at
the Manipal branch.
customer_name (branch_name=“Manipal”
(borrower.loan_number = loan.loan_number(borrower x loan)))

 Find the names of all customers who have a loan at the


Manipal branch but do not have an account at any branch of
the bank.
customer_name (branch_name = “Manipal”

(borrower.loan_number = loan.loan_number(borrower x loan))) –


customer_name(depositor)

28-Apr-25 Dept. of I&CT 48


Example Queries
• Find the names of all customers who have a loan at
the Manipal branch.
 Query 1

customer_name (branch_name = “Manipal” (


borrower.loan_number = loan.loan_number (borrower x loan)))

 Query 2

customer_name(loan.loan_number = borrower.loan_number (
(branch_name = “Manipal” (loan)) x borrower))

28-Apr-25 Dept. of I&CT 49


Example Queries
• Find the largest account balance
• Strategy:
• Find those balances that are not the largest
• Rename account relation as d so that we can compare
each account balance with all others
• Use set difference to find those account balances that
were not found in the earlier step.
• The query is:

balance(account) - account.balance
(account.balance < d.balance (account x
d (account)))

28-Apr-25 Dept. of I&CT 50


Exercise
1.Select all the tuples for which runs are
greater than or equal to 15000.
2.Select all the players whose runs are
greater than or equal to 6000 and age
is less than 25
3. List all the countries in Player
relation.

28-Apr-25 Dept. of I&CT 51


Exercise
1. Find all the customers having an account but not the
loan.
2. Find all the customers having a loan but not the
account.
3. Rename Player relation to PlayerList.

28-Apr-25 Dept. of I&CT 52

You might also like