1.2 Introduction To DBMS-Relational Model
1.2 Introduction To DBMS-Relational Model
Relational Model
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
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
2.10
Super Key
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
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.
The textual context or execution sequence is There is no need to consider textual context or
considered. execution sequence.
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'.
2.20
Select Operation (σ)
● Relation r
2.21
Project Operation (∏)– selection of columns (Attributes)
It projects column(s) that satisfy a given predicate.
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}
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
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.
Notation − ρ x (E),
2.32
Renaming Operation (ρ)
● Allows us to refer to a relation, (say E) by more than one name.
ρ x (E)
● Relations r
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:
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