Relational Model
Relational Model
The table name and column names are helpful to interpret the
meaning of values in each row. The data are represented as a set of
relations. In the relational model, data are stored as tables. However,
the physical storage of the data is independent of the way the data are
logically organized.
Example:
Example:
EmpSSN EmpNum Empname
9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James
Example:
Example: In the given table Stud ID, Roll No, and email are candidate keys
which help us to uniquely identify the student record in the table.
Example:
DeptCode DeptName
001 Science
002 English
005 Computer
In this key in dbms example, we have two table, teach and department in a
school. However, there is no way to see which search work in which
department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can
create a relationship between the two tables.
Relational Algebra
RELATIONAL ALGEBRA is a widely used procedural query
language. It collects instances of relations as input and gives
occurrences of relations as output. It uses various operations to
perform this action. SQL Relational algebra query operations are
performed recursively on a relation. The output of these operations is
a new relation, which might be formed from one or more input
relations.
SELECT (symbol: σ)
PROJECT (symbol: π)
RENAME (symbol: ρ)
UNION (υ)
INTERSECTION ( ),
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
JOIN
SELECT (σ)
The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition. Sigma(σ)Symbol denotes it.
It is used as an expression to choose tuples which meet the selection
condition. Select operator selects tuples that satisfy a given predicate.
σp(r)
σ is the predicate
p is prepositional logic
Example 1
σ topic = "Database" (Tutorials)
Example 2
σ topic = "Database" and author = "guru99"( Tutorials)
Example 3
σ sales > 50000 (Customers)
Projection(π)
The projection eliminates all attributes of the input relation but those
mentioned in the projection list. The projection method defines a
relation that contains a vertical subset of Relation.
Example of Projection:
Rename (ρ)
Rename is a unary operation used for renaming attributes of a
relation.
Example
Table A Table B
column column column column
1 2 1 2
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∪ B
column 1 column 2
1 1
1 2
1 3
Example
A-B
Table A - B
column 1 column 2
1 2
Intersection
An intersection is defined by the symbol ∩
A∩B
Defines a relation consisting of a set of all tuple that are in both A and
B. However, A and B must be union-compatible.
Visual
Definition of Intersection
Example:
A∩B
Table A ∩ B
column 1 column 2
1 1
σ column 2 = '1' (A X B)
Output – The above example shows all rows from relation A and B
whose column 2 has value 1
σ column 2 = '1' (A
X B)
column 1 column 2
1 1
1 1
NATURAL JOIN (⋈)
Natural join can only be performed if there is a common attribute
(column) between the relations. The name and type of the attribute
must be same.
Example
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria,
we also include some or all tuples that do not match the criteria.
A B
A⋈B
Num Cube Square
2 8 4
3 18 9
5 75 -