19CSE202
Database Mangement Systems
Relational Data Model
Click to add text
R.Manjusha
Department of CSE
Amrita Vishwa Vidyapeetham
1
● Contents
○ Structure of Relational Databases
○ Keys
○ Schema Diagrams
○ Relational Query Languages
○ Relational Operations
2
● Contents
○ Structure of Relational Databases
○ Keys
○ Schema Diagrams
○ Relational Query Languages
○ Relational Operations
3
Introduction
● Relational data model is the commercial
data model for todays applications.
● It is simple and ease of use for programmer
compared to other models.
4
Structure of Relational Model
attributes
(or columns)
tuples
(or rows)
• Relational DB is a collection of
Tables.
• Refer the Instructor Table.
• There are four columns.
• Each row is storing the values for ID,
name, dept_name and salary of an
Instructor.
• Each row is identified by ID. 5
• Another Example - Refer the Course Table.
• There are four columns.
• Each row is storing the values for
course_id, title, dept_name and
credits of a course.
• Each course is identified by its
course_id.
6
• There are two columns.
• In Each row, the second course is the prerequisite course
for the first course.
• Thus, each row indicates two course are related.
• Similar way, in Instructor table ID is related to name,
dept_name and Salary of an instructor.
• and in course table the course_ide is related to title,
dept_name and credit.
7
Relational Model
• In general, table represents • In Mathematics …
relationship among set of Tuple is a sequence of values.
values.
A relationship between n values is
• Table is a collection of represented as and n-tuple of values.
relationships.
• In Relational Model …
• ‘Table’ in Database design is Relation refers to a table.
similar to ‘relation’ in Tuple refers to a row.
Mathematics…. Thus the term Attribute refers to a column.
‘Relational Model’.
8
9
What is the degree and cardinality of this relation?
10
Relation Instance
● Refer the Instructor relation instance, has
12 rows corresponding to 12 instructors.
● The order in which the row are arranged is
irrelevant. It can be sorted or unsorted.
● Both the instances in right side are the
same.
11
Concepts underlying Relational Model - domain
● Domain of salary – Set of salary of
● The set of allowed values instructors.
for each attribute is ● Domain of name – Set of names of
called the domain of the instructors.
attribute.
salary = {65000, 900000, 40000, 95000,
60000….80000}
Each element in the domain a value for the
attribute.
40000 is a value for the salary attribute.
12
Concepts underlying Relational Model - Atomic
● Suppose we have ‘Phone-Number’
attribute in instructor relation, and
● For each relation (r), the domain
allows multiple phone numbers for
of all attributes of r be atomic.
each instructor,
● that is, indivisible.
● A sample domain of ‘Phone-Number’
={(0422-2685000,+91-8967563421),
(8934765478), (1278347865, 0433-
salary = {65000, 900000, 40000, 95000, 783456)……}
60000….80000}
Is salary attribute atomic? First and third elements of this domain
Answer: yes. Why? has two phone numbers, hence it is non-
atomic domain. 13
Concepts underlying Relational Model - Atomic
● A sample domain of ‘Phone-Number’
={0422-2685000, 0433-783456, +91-8967452312}
Is it Atomic domain?
Answer : No..
Why? – each phone number is divisible – country code, city code and phone
number.
● A sample domain of ‘Phone-Number’
={3456789023, 3948576819, 7829345678}
Is it Atomic domain?
Answer: Yes
Why? – each element in this domain is single value ie., indivisible. 14
Concepts underlying Relational Model - ‘null’
● The special value null is a member of
every domain. Indicated that the value is
● If an instructor does not
“unknown” or does not exist.
have phone, the value for his
phone-number attributed to
● The null value causes complications at the
be stored ‘null’
time of accessing the data from database,
hence use of null to be restricted.
15
Concepts underlying Relational Model -
Database schema Vs Database Instance. Database Instance
● Database Schema – the logical design of the
database.
● Database Instance – the content of the
database at an instance of time.
Database Schema
16
Concepts underlying Relational Model -
relation Schema Vs relation instance
Relation Schemas Relation Instance
In General relation schema contains the
list of attributes and their domains.
Eg Student {Name: String;
StudentNumber: String; Age: integer} 17
Concepts underlying Relational Model -
relation schema & instance…
● int marks [5]; ----- Schema
● Marks[78, 93, 90, 89, 83] ----- instance
● Instance – changes.
● Schema – does not.
18
Let us proceed further with University Database
19
University Database – Relation Schemas
20
University Database – Relation Instances department
instructor course prerequisite
Instructor is related to department
by dept_name attribute.
section teaches
Instructor is related to sections by
course_id, sec_id and semester and
the association is stored in a new
relation teaches.
… 21
Relation Schema and Instance
● If 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 which are the domains A1,
A2, …, An, 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 22
● Contents
○ Structure of Relational Databases
○ Keys
○ Schema Diagrams
○ Relational Query Languages
○ Relational Operations
23
Keys
● No two relations in a relation is allowed to have same values for all the
attributes.
● Tuples within a given relation can be distinguished, by the values of the
attributes.
Super Keys, Candidate Key, Primary Key, Foreign
Key 24
Superkey
● 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. {name} is not a superkey, because more
instructors may have same name.
25
Candidate Keys
● A superkey may have extraneous attribute.
Eg. In {ID, name}, name is extraneous.
● Superkey K is a candidate key if K is
minimal
Example: {ID} is a candidate key for
Instructor, but not {ID, name}.
● Candidate keys are selected from the set of
super keys, the only thing we take care while
selecting candidate key is that the candidate
key should not have any redundant attributes.
There can be many possible candidate keys for
a relation
● Candidate Keys are not allowed to have NULL Eg. {ID} and {name, dept_name} for instructor.
values. 26
Primary keys.
● Several distinct set of attributes could
serve as candidate keys.
● The candidate key which is primarily
chosen by the database designer is
the primary key.
{ID} in instructor is the primary key.
27
Primary Keys
● Must be chose with care.
● Their attribute values are never or very rarely
changed.
Eg. RollNumber.
● The primary key of a relation to be listed
before other attributes.
Refer the department relation.
28
Can you identify Super, candidate, primary and
alternate keys ?
29
Super keys: ● {Roll_no, Name}: This key cannot be considered as
● {Roll_no} a candidate key because when we take the subset of
● {Registration_no} this key we get two attributes i.e. Roll_no or Name.
● {Roll_no, Registration_no}, Roll_no is a candidate key. So, it is not a minimal
● {Roll_no, Name} super key. Hence, this key is not a candidate key.
● {Name, Registration_no} ● {Name, Registration_no}: This key cannot be
● {Roll_no, Name, Registration_no} considered as a candidate key because when we
Candidate keys: take the subset of this key we get two attributes i.e
● {Roll_no}: This key doesn't have any redundant or Registration_no or Name. Registration_no is a
repeating attribute. So, it can be considered as a candidate key. So, it is not a minimal super key.
candidate key. Hence, this key is not a candidate key.
● ● {Roll_no, Name, Registration_no}: This key cannot
{Registration_no}: This key also doesn't have any
repeating attribute. So, it can be considered as a be considered as a candidate key because when we
candidate key. take the subset of this key we get three attributes i.e
● {Roll_no, Registration_no}: This key cannot be Roll_no, Registration_no and Name. Two of these
considered as a candidate key because when we take the attributes i.e Roll_no and Registration_no are the
subset of this key we get two attributes i.e Roll_no or candidate key. So, it is not a minimal superkey.
Registration_no. Each of these attributes is the candidate Hence, this key is not a candidate key.
● So, from the above discussion, we conclude that we
key. So, it is not a minimal super key. Hence, this key is
not a candidate key. can have only 2 out of above 6 super keys as the
candidate key. i.e. (Roll_no) and(Registration_no).
30
Alternate key:
Primary key: ● Since we have made ‘Roll_no’ as the
● There are two candidate keys i.e (Roll_no) Primary Key our Alternate Key would be
and (Registration_no). From this set, we ‘Registration_no’.
can select any key as the primary key for
our table. It depends upon our
requirement. Here, if we are talking about
class then selecting ‘Roll_no’ as the
primary key is more logical instead of
‘Registration_no’.
31
Foreign Keys
● A relation, say r1, may include among its
attributes the primary key of another relation, say
r2.
● This attribute is called as foreign key from r1,
referencing to r2.
● The relation r1 is called as the referencing
relation and r2 is called the referenced relation.
● The foreign key forms a dependency between r1
and r2.
32
● Eg. dept_name attribute, from instructor relation
33
elow example the Stu_Id column in Course_enrollment table is a foreign key as it points to the primary key of the Studen
Foreign Key
● A FOREIGN KEY is a key used to link two tables together.
● A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in
another table.
Course enrollment Student In the below example
the Stu_Id column
Course_id Stud_id Stud_id Name Age
in Course_enrollment table
C01 101 101 John 21 is a foreign key as it points
to the primary key of the
CO2 102 102 Arya 18 Student table.
CO3 101 103 Bran 19
CO5 102 104 Amanda 18
CO6 103
CO7 102
34
● Contents
○ Structure of Relational Databases
○ Keys
○ Schema Diagrams
○ Relational Query Languages
○ Relational Operations
35
Schema Diagram
● The database schema and its keys can be visualized in Schema Diagram.
36
● Contents
○ Structure of Relational Databases
○ Keys
○ Schema Diagrams
○ Relational Query Languages
○ Relational Operations
37
Relational Query Language
● A query language is a language in which a user requests information from the database.
● They are on a lever higher than that of standard programming languages.
● Structured Query Language (SQL)
○ Predominant application-level query language
○ Declarative
● Relational Algebra
○ Intermediate language used within DBMS
○ Procedural
38
Relational Query Language
❑ A query language is a language in which a user requests information from the
database.
❑ They are on a lever higher than that of standard programming languages.
❑ Two mathematical Query Languages form the basis for “real” languages (e.g.
SQL), and for implementation:
❑ Relational Algebra: More operational(procedural), very useful for
representing execution plans.
❑ Relational Calculus: Lets users describe what they want, rather than how
to compute it. (Non-operational, declarative.)
Relational
Procedural
Algebra
Relational Query
Language
Relational
Non Procedural
Calculus
39
Relational Query Language
Two Categories
Procedural Languages non-procedural language
User instructs the system to perform the user describes the desired results
a sequence of operations on the database without giving a specific procedure
to compute desired result. for obtaining it.
* Query languages in practise use both the approaches.
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.
40
● Contents
○ Structure of Relational Databases
○ Keys
○ Schema Diagrams
○ Relational Query Languages
○ Relational Operations
41
Relational Algebra
Optimized
Relational Relational Query Executable
SQL algebra execution code
algebra
que expression plan Code
expression
ry parser
generator
Query optimizer
DBMS
Dept. of CSE., Amrita School of Engineering, Coimbatore July 2020 42
Relational Algebra
❑ Domain: set of relations
❑ Basic operators: select, project, union, set
difference, Cartesian product
❑ Derived operators: set intersection, division, join
❑ Procedural: Relational expression specifies query
by describing an algorithm (the sequence in which
operators are applied) for determining the result
of an expression
43
Relational Operations
❑ The procedural query languages provide a set of operations
that can be applied
one or more relations.
❑ These operations have desired properties.
❑ These operations can be combined in a modular way.
❑ Common operations –
⮚ Selection
⮚ Projection
⮚ Union
⮚ Difference
⮚ Intersection
⮚ Join (natural join and Cartesian Product)
44
Relational Operations
Basic operations:
● Selection ( σ ) Selects a subset of rows from
relation.
● Projection ( π ) Deletes unwanted columns from
relation.
● Cross-product ( X ) Allows us to combine two
relations.
● Set-difference ( - ) Tuples in relation. 1, but not in
relation. 2.
● Union ( ⋃ ) Tuples in relation. 1 and in relation. 2.
45
Relational Operations
Additional operations:
● Intersection(∩)
● join(⨝)
● division(÷)
● Renaming ρ
Since each operation returns a relation, operations can be
composed!
(Relations are closed under the operators of the
relational algebra.)
46
47
Selection Condition
● Operators: <, , , >, =,
● Simple selection condition:
○ <attribute> operator <constant>
○ <attribute> operator <attribute>
● <condition> AND <condition>
● <condition> OR <condition>
● NOT <condition>
48
Selection Condition - Examples
❑ Display all persons whose id greater than 3000 or
hobby is hiking
❑ Display all persons whose id between 3000 and
4000
❑ Display all persons whose hobby is not hiking
❑ Display all persons whose hobby is hiking
Person
Id Name Address Hobby
1123 John 123 Main stamps
1123 John 123 Main coins
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
49
Selection Condition - Examples
❑ Display all persons whose id greater than 3000
or hobby is hiking
σ Id>3000 v Hobby=‘hiking’ (Person)
Person Person
Id Name Address Hobby Id Name Address Hobby
1123 John 123 Main stamps 5556 Mary 7 Lake Dr hiking
1123 John 123 Main coins 9876 Bart 5 Pine St stamps
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
50
Selection Condition - Examples
❑ Display all persons whose id between 3000 and 4000
σ Id>3000 ʌ Id <3999 (Person)
Person
Id Name Address Hobby
1123 John 123 Main stamps
1123 John 123 Main coins
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
51
Selection Condition - Examples
❑ Display all persons whose hobby is not hiking
σ (Hobby!=‘hiking’) (Person)
Person Person
Id Name Address Hobby Id Name Address Hobby
1123 John 123 Main stamps 1123 John 123 Main stamps
1123 John 123 Main coins 1123 John 123 Main coins
5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps
9876 Bart 5 Pine St stamps
52
Selection Condition - Examples
❑ Display all persons whose hobby is hiking
σ Hobby=‘hiking’ (Person)
Person Person
Id Name Address Hobby Id Name Address Hobby
1123 John 123 Main stamps 5556 Mary 7 Lake Dr hiking
1123 John 123 Main coins
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
53
Select Operation – selection of rows (tuples)
Relation r
A=B ^ D > 5 (r)
54
55
56
Project Operation – selection of columns (Attributes)
● Relation r:
A,C (r)
57
58
59
60
Union of two relations
● Relations r, s:
r s:
61
Set difference of two relations
● Relations r, s:
r – s:
62
Set intersection of two relations
● Relation r, s:
● rs
Note: r s = r – (r – s)
63
64
joining two relations -- Cartesian-product
Relations r, s:
r x s:
65
Cartesian-product – naming issue
Relations r, s: B
r x s: r.B s.B
66
Renaming a Table
● Allows us to refer to a relation, (say E) by more than one name.
x (E)
returns the expression E under the name X
67
Composition of Operations
● Can build expressions using multiple operations
● Example: A=C (r x s)
● rxs
● A=C (r x s)
68
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 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
69
Natural Join Example
● Relations r, s:
Natural Join
r s
A, r.B, C, r.D, E ( r.B = s.B ˄ r.D = s.D (r x s)))
70
Example of evaluation plans
● Select the records of EMP with DEPT_ID = 10 and EMP_LAST_NAME = ‘Joseph’ first then join
them with DEPT table to get all matching records of EMP and DEPT. Then select only the columns
EMP_ID and DEPT_NAME to display the result.
71
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.
72
Review Terms
73
RelaX Tool:
● https://dbis-uibk.github.io/relax/calc.htm#
https://www.menti.com/gzgxhtaz4m
https://www.menti.com/gzgxhtaz4m
To Try:
● SELECT Name,PersNr FROM Professoren where Rang='C3‘
● SELECT PersNr FROM Professoren UNION select distinct MatrNr from
Studenten
● SELECT * FROM Professoren
74
THANK YOU
75