2. Relational Model
• Primary data model for commercial data processing
applications.
• Consists of relational databases.
• Advantages:
Simplicity
Easier for accessing data
3. Structure of Relational Databases
• Relational database collection of tables with unique
names
• Tables called as relations
• Rows of the table records or tuples
• Columns of the table attributes or fields
4. Contd..
• Set of permitted values of the attributes domain
values
• Null value special value that signifies that the
value is unknown or does not exist
6. Database Schema and Instance
• Database schema:
Logical design or overall design of the database
Does not change frequently
Eg: university (instructor, student)
• Database instance:
Snapshot of the data in the database at a given
instant in time.
Changes frequently
7. Relation Schema and Instance
Relation schema:
• Consists of a list of attributes and their corresponding
domains.
• Similar to a variable in programming language.
• Does not change frequently
• Eg: instructor = (ID, name, dept_name, salary)
Relation instance:
• Set of domain values in a relation at a particular point of
time or current values
• Similar to value of a variable in programming language
• Changes frequently
10. Primary Key
• Main reference key of the table
• One of the candidate keys that is most appropriate
to uniquely identify a record in a table.
11. Candidate Key
• Subset of super key
• Minimal super key
• Single field or least combination of fields that
uniquely identifies each record in the table
12. Super Key
• A super key is a set of attributes of any combination
that uniquely identifies a record within a table.
• Given table:
STUDENT{StudentId, firstName, lastName,
courseId}
• Possible superkeys are:
o {StudentId}
o {StudentId, firstName}
o {firstName, lastName}
o (StudentId, firstName, lastName, courseId}
13. Foreign key
• Used to relate two tables
• Primary key of one table (parent table) acts as a key
for another table (child table) in order to provide
relationship between these two tables
• That non-primary key attribute is said to be the
foreign key for the parent table
• Child table is called as the referencing relation and
the parent table is called as the referenced relation
15. Referential Integrity Constraints
• Set of constraints applied to foreign key which
prevents entering a row in child table (where you
have foreign key) for which you don't have any
corresponding row in parent table.
• Referential Integrity prevents your table from
having incorrect or incomplete relationship.
16. Schema Diagrams
• Pictorial representation of database schemas
• A database schema along with primary key and
foreign key dependencies can be depicted by
schema diagrams
19. Introduction
• Query retrieval of information
• Query language language that is used for
information retrieval
• Formal query languages based upon which SQL
is developed
• Types:
Relational Algebra
Relational Calculus
Tuple Relational Calculus
Domain Relational Calculus
20. Relational Algebra
• Procedural query language
• Consists of a set of operations that take one or two
relations as input and produce a new single relation as
their output
21. Fundamental Operations
• Select
• Project
• Union
• Set difference
• Cartesian product
• Rename
Note: select, project, rename – called as ‘unary’
operators and the remaining are ‘binary’ operators
22. Select Operation
• The select operation display tuples that satisfy a
given predicate (condition).
• We use the lowercase Greek letter sigma to
denote selection.
• It produces a “horizontal” subset.
• Syntax: sC(R)
– where C is a selection condition(=, <, >, =<, >=,
<>)
– and R is the relation over which the selection
takes place
23. Example of Select
Student
Find all students whose id is above 300.
Query: s sid > 300(Student)
Resulting Relation:
sid name addr
123 Fred 3 Oxford
345 John 6 Hope Rd.
567 Ann 5 Garden
sid name addr
345 John 6 Hope Rd.
567 Ann 5 Garden
24. Project Operation
• The project operation display attributes that
satisfy a given predicate (condition).
• We use the lowercase Greek letter pi to
denote selection.
• It produces a “vertical” subset.
• Eliminates duplicated values.
• Syntax: ПA(R)
– where A is a set of attributes of R
– and R is the relation over which the project
takes place
25. Example of Project
Enrollment
Display all sid values
Query: Пsid(Enrollment)
sid name addr
123 Fred 3 Oxford
345 John 6 Hope Rd.
567 Ann 5 Garden
sid
123
345
567
26.
27. SELECTION & PROJECTION Example
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
Id Name Address Hobby
1123 John 123 Main stamps
9876 Bart 5 Pine St stamps
σ Hobby=‘stamps’(Person)
Person
∏Name, Hobby(Person)
Name Hobby
John stamps
John coins
Mary Hiking
Bart stamps
28. 28
Examples
• Id>3000 OR Hobby=‘hiking’ (Person)
• Id>3000 AND Id <3999 (Person)
• NOT(Hobby=‘hiking’) (Person)
• Hobby‘hiking’ (Person)
• Id (Person)
• Hobby (Person)
30. Union Operation
• Creates a relation that contains all the values in both
the relations.
• Two relations are union compatible if:
– Both have same number of columns
– Names of attributes are the same in both
• Represented using the symbol ‘∪’.
• Eliminate duplicates
• Syntax: S1 ∪ S2
– where S1, S2 are separate relations
32. The Set-Difference Operation
• Allows us to find tuples that are in one relation but
are not in another.
• Denoted by the symbol ‘−’.
• Syntax: S1 – S2
• where S1, S2 are separate relations
35. SUMMARY
INTRODUCTION TO RELATIONAL MODEL
• Structure of Relational Databases
o Tables
o Records or tuples
o Attributes or fields
o Domain values
o Null values
• Difference b/w database schema and database instance
• Difference b/w relation schema and relation instance
• Keys
o Super key
o Candidate key
o Primary key
o Foreign key
• Referential integrity constraint
• Fundamental database operations