0% found this document useful (0 votes)
8 views38 pages

1.2 Introduction To DBMS-Relational Model

The document provides an introduction to database management, focusing on the relational model, including concepts such as attributes, tuples, keys (primary, candidate, and foreign), and operations in relational algebra. It explains the structure of relations, the significance of keys in uniquely identifying records, and various relational operations like selection, projection, union, and natural join. Additionally, it contrasts procedural and non-procedural languages used in database systems.

Uploaded by

Devanshu Ajmera
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)
8 views38 pages

1.2 Introduction To DBMS-Relational Model

The document provides an introduction to database management, focusing on the relational model, including concepts such as attributes, tuples, keys (primary, candidate, and foreign), and operations in relational algebra. It explains the structure of relations, the significance of keys in uniquely identifying records, and various relational operations like selection, projection, union, and natural join. Additionally, it contrasts procedural and non-procedural languages used in database systems.

Uploaded by

Devanshu Ajmera
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/ 38

Introduction to Database Management

Relational Model

Database System Concepts, 6th Ed.


©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Example of a Relation

attributes
(or columns)

tuples
(or rows)

2.2
Attribute Types
● 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
● The special value null is a member of every domain. Indicated that the
value is “unknown”
● The null value causes complications in the definition of many
operations

2.3
Relation Schema and Instance
● A1, A2, …, An are attributes

● R = (A1, A2, …, An ) is a relation schema


Example:
instructor = (ID, name, dept_name, salary)
● Formally, given sets 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

● The current values (relation instance) of a relation are specified by a table


● An element t of r is a tuple, represented by a row in a table

2.4
Relations are Unordered
● Order of tuples is irrelevant (tuples may be stored in an arbitrary order)
● Example: instructor relation with unordered tuples

2.5
Types of key

2.6
Keys
● Let K ⊆ R
● K is a superkey of R if values for K are sufficient to identify a unique tuple of
each possible relation r(R)
● Example: {ID} and {ID,name} are both superkeys of instructor.
● Superkey K is a candidate key if K is minimal
Example: {ID} is a candidate key for Instructor
● One of the candidate keys is selected to be the primary key.
● {ID}
● Foreign key constraint: Value in one relation must appear in another
● Referencing relation
● Example – dept_name in instructor is a foreign key from instructor
referencing department

2.7
Keys (cont)
● Used to uniquely identify any record or row of data from the table.
● Used to establish and identify relationships between tables.
● For example:
● Student table, ID is used as a key because it is unique for each student.
● PERSON table, passport_number, license_number, SSN are keys since
they are unique for each person.

2.8
Primary Key
● Primary key
● The first key which is used to identify one and
only one instance of an entity uniquely.
● An entity can contain multiple keys
● The key which is most suitable from those lists
become a primary key.
● E.g. EMPLOYEE table, ID can be primary key
since it is unique for each employee.
License_Number and Passport_Number as
primary key since they are also unique.
● For each entity, selection of the primary key is
based on requirement and developers.

2.9
Candidate Key

● A candidate key is an attribute or set of an


attribute which can uniquely identify a tuple.
● Thus, from the number of obtained
candidate keys, we can identify the
appropriate primary key.
● E.g. EMPLOYEE table:
● {employee_id}, {Pass_no},
{L_num},{SSN}

2.10
Super Key

● A set of an attribute which can uniquely


identify a tuple.
● Super key is a superset of a candidate key.
● EMPLOYEE table,
● (EMPLOEE_ID, EMPLOYEE_NAME)
the name of two employees can be the
same, but their EMPLYEE_ID can't be
the same. Hence, this combination can
also be a key.
● The super key would be EMPLOYEE-ID,
(EMPLOYEE_ID,
EMPLOYEE-NAME), etc.

2.11
Foreign Key
● The column of the table which is used to point to the primary key of another table.
● E.g. In a company, every employee works in a specific department, and employee and
department are two different entities.
● The information of the Employee and Department are in the employee table.
● Link these two tables through the primary key of one table.
● Steps:
● Add the primary key of the DEPARTMENT table, Department_Id as a new attribute in the
EMPLOYEE table.
● Now in the EMPLOYEE table, Department_Id is the foreign key, and both the tables are
related.

2.12
Student Table
Student_Number Student_Name Student_Phone Subject_Number
1 Andrew 6615927284 10
2 Sara 6583654865 20
3 Harry 4647567463 10

● The Super Keys −


{Student_Number} {Student_Phone} {Student_Number,Student_Name}
{Student_Number,Student_Phone} {Student_Number,Subject_Number}
{Student_Phone,Student_Name} {Student_Phone,Subject_Number}
{Student_Number,Student_Name,Student_Phone}
{Student_Number,Student_Phone,Subject_Number}
{Student_Number,Student_Name,Subject_Number}
{Student_Phone,Student_Name,Subject_Number}
● The Candidate Key
{Student_Number}
{Student_Phone}
{Student_name, Subject_Number}
● The Primary Key : {Student_Number}

2.13
Relational Schema Diagram for University
Database

2.14
Relational Schema Diagram for Bank
Database

2.15
Relational Schema Diagram for Hospital
Management

2.16
Procedural Languages Non-Procedural Languages
The procedural languages are command-driven or
The non-procedural languages are fact-oriented.
statement-oriented.
Non-Procedural languages are used in RDBMS,
Procedural languages are used for application and
expert systems, natural language processing, and
system programming.
education.

It is complex. It is simpler than procedural.

The textual context or execution sequence is There is no need to consider textual context or
considered. execution sequence.

The logic programs that use the only resolution face


Machine efficiency is good.
serious problems of machine efficiency.
The procedural paradigm leads to a large number
There are no such connections present in the
of the probable network between functions and
non-procedural paradigm.
data if there are many functions and many global
data items.
An example of procedural languages is C, Pascal, An example of non-procedural languages is Prolog,
C++, etc. USP, SQL Scheme, etc.

2.17
Relational Query Languages
● “Pure” languages:
● Relational algebra
● Tuple relational calculus
● Domain relational calculus
● The above 3 pure languages are equivalent in computing power

2.18
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.
● Relational algebra is performed recursively on a relation and intermediate
results are also considered relations.
● The fundamental operations of relational algebra are as follows –
● Select
● Project
● Union
● Set difference
● Cartesian product
● Rename

2.19
Select Operation (σ) – selection of rows (Tuples)
It selects tuples that satisfy the given predicate from a relation.

Notation − σp(r), Where σ stands for selection predicate and r stands for
relation. p is prepositional logic formula which may use connectors like and,
or, and not. These terms may use relational operators like − =, ≠, ≥, < , >, ≤.

For example −
σsubject = "database"(Books)
output − Selects tuples from books where subject is 'database'.

σsubject = "database" and price = "450"(Books)


output − Selects tuples from books where subject is 'database' and 'price' is 450.

σsubject = "database" and price = "450" or year > "2010"(Books)


output − Selects tuples from books where subject is 'database' and 'price' is 450
or those books published after 2010.

2.20
Select Operation (σ)
● Relation r

◼ σA=B ^ D > 5 (r)

2.21
Project Operation (∏)– selection of columns (Attributes)
It projects column(s) that satisfy a given predicate.

Notation − ∏A1, A2, An (r),


Where A1, A2 , An are attribute names of relation r.

Duplicate rows are automatically eliminated, as relation is a set.


For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.

2.22
Project Operation – selection of columns (Attributes)

● Relation r:

∏A,C (r)

2.23
Union (∪) Operation
It performs binary union between two given relations and is defined as r
∪ s = { t | t ∈ r or t ∈ s}

Notation − r U s, Where r and s are either database relations or relation


result set (temporary relation).

For a union operation to be valid, the following conditions must hold −


● r, and s must have the same number of attributes.
● Attribute domains must be compatible.
● Duplicate tuples are automatically eliminated.
E.g.,
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a
book or an article or both.

2.24
Union of two relations
● Relations r, s:

● r ∪ s:

2.25
Set difference (-) Operation
● The result of set difference operation is tuples, which are present in one
relation but are not in the second relation.

● Notation − r − s
Finds all the tuples that are present in r but not in s.

E.g.,
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not
articles.

2.26
Set difference of two relations
● Relations r, s:

● r – s:

2.27
Set intersection of two relations

● Relation r, s:

● r∩s

2.28
Cartesian product (Χ)
Combines information of two different relations into one.

Notation −
rΧs

Where r and s are relations and their output will be defined as −


r Χ s = { q t | q ∈ r and t ∈ s}

σauthor = 'tutorial'(Books Χ Articles)

Output − Yields a relation, which shows all the books and articles written by
tutorial.

2.29
joining two relations -- Cartesian-product
● Relations r, s:

● r x s:

2.30
Cartesian-product – naming issue
● Relations r, s: B

● r x s: r.B s.B

2.31
Renaming Operation (ρ)

The results of relational algebra are also relations but without any name.

The rename operation allows us to rename the output relation. 'rename'


operation is denoted with small Greek letter rho ρ.

Notation − ρ x (E),

Where the result of expression E is saved with name of x.

2.32
Renaming Operation (ρ)
● Allows us to refer to a relation, (say E) by more than one name.
ρ x (E)

returns the expression E under the name X

● Relations r

● r x ρ s (r) r.A r.B s.A s.B


α 1 α 1
α 1 β 2
β 2 α 1
β 2 β 2

2.33
Composition of Operations
● Can build expressions using multiple operations
● Example: σA=C (r x s)

● rxs

● σA=C (r x s)

2.34
Joining two relations – Natural Join
● Let r and s be relations on schemas R and S respectively.
Then, the “natural join” of relations R and S is a relation on schema R ∪
S obtained as follows:

● Consider each pair of tuples t from r and t from s.


r 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 t on r


r
▪ t has the same value as t on s
s

2.35
Natural Join Example
● Relations r, s:

● Natural Join
● r s

2.36
Notes about Relational Languages
● Each Query input is a table (or set of tables)
● Each query output is a table.
● All data in the output table appears in one of the input tables
● Relational Algebra is not Turning complete

2.37
Summary of Relational Algebra Operators
Symbol (Name) Example of Use
σ
σ
(Selection) salary > = 85000 (instructor)
Return rows of the input relation that satisfy the predicate.
Π
Π
(Projection) ID, salary (instructor)
Output specified attributes from all rows of the input relation. Remove duplicate
tuples from the output.
x
(Cartesian Product) instructor x department
Output pairs of rows from the two input relations that have the same value on all
attributes that have the same name.

Π ∪ Π
(Union) name (instructor) name (student)
Output the union of tuples from the two input relations.
-
Π
(Set Difference) name (instructor) -- Π
name (student)
Output the set difference of tuples from the two input relations.

(Natural Join) instructor ⋈ department
Output pairs of rows from the two input relations that have the same value on all
attributes that have the same name.

2.38

You might also like