DBMSUNIT2
DBMSUNIT2
PRASHANT TOMAR
Relational Model
• The relational model represents the database as a collection of relations. A
relation is nothing but a table of values. Every row in the table represents a
collection of related data values. These rows in the table denote a real-world
entity or relationship.
• 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.
Relational Model Concepts
• Attribute
Each column in a Table. Attributes are the properties which define a relation. e.g.,
Student_Rollno, NAME etc.
• Tables
In the Relational model the, relations are saved in the table format. It is stored
along with its entities. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
Tuple
It is nothing but a single row of a table, which contains a single record.
• Relation Schema
A relation schema represents the name of the relation with its attributes.
• Degree
The total number of attributes which in the relation is called the degree of the
relation.
• Cardinality
• Column
Relation instance is a finite set of tuples in the RDBMS system. Relation instances
never have duplicate tuples.
• Relation key
Every row has one, two or multiple attributes, which is called relation key.
• Attribute domain
Every attribute has some pre-defined value and scope which is known as attribute
domain
Relational Integrity constraints
• Relational Integrity constraints is referred to conditions which must be present
for a valid relation. These integrity constraints are derived from the rules in the
mini-world that the database represents.
1. Domain constraints
2. Key constraints
Example: In the given table, CustomerID is a key attribute of Customer Table. It is most likely to
have a single key for one customer, CustomerID =1 is only for the CustomerName =" Google".
Modify allows you to change the values of some attributes in existing tuples.
UPDATE table
SET column1 = expression1,
column2 = expression2,
column_n = expression_n
[WHERE conditions];
Delete Operation
• To specify deletion, a condition on the attributes of the relation selects the tuple to be
deleted.
The Delete operation could violate referential integrity if the tuple which is deleted is
referenced by foreign keys from other tuples in the same database.
Select Operation
• To specify selection, all and condition on the attributes of the relation selects the
tuple.
SELECT expressions
FROM tables
[WHERE conditions];
Best Practices for creating a Relational Model
• Data need to be represented as a collection of relations
• Complex relational database systems may lead to isolated databases where the
information cannot be shared from one system to another.
Functional Dependencies
Functional Dependency
• Functional Dependency is when one attribute determines another attribute in a
DBMS system. Functional Dependency plays a vital role to find the difference
between good and bad database design.
• By this, we can say that the city, Employee Name, and salary are functionally
depended on Employee number.
Axiom
Axioms is a set of inference rules used to infer all the functional dependencies on a relational
database.
Decomposition
It is a rule that suggests if you have a table that appears to contain two entities which are
determined by the same primary key then you should consider breaking them up into two
different tables.
Dependent
It is displayed on the right side of the functional dependency diagram.
Determinant
It is displayed on the left side of the functional dependency Diagram.
Union
It suggests that if two tables are separate, and the PK is the same, you should consider putting
them. together
Rules of Functional Dependencies
• Reflexive rule
If X is a set of attributes and Y is subset of X, then X holds a value of Y.
• Augmentation rule
When x -> y holds, and c is attribute set, then ac -> bc also holds. That is adding
attributes which do not change the basic dependencies.
• Transitivity rule
This rule is very much similar to the transitive rule in algebra if x -> y holds
and y -> z holds, then x -> z also holds. X -> y is called as functionally that
determines y.
Types of Functional Dependencies
• Multivalued dependency
• Transitive dependency
Multivalued dependency in DBMS
• Multivalued dependency occurs in the situation where there are multiple
independent multivalued attributes in a single table. A multivalued dependency
is a complete constraint between two sets of attributes in a relation. It requires
that certain tuples be present in a relation.
car_model-> colour
Trivial Functional dependency
• The Trivial dependency is a set of attributes which are called a trivial if the set of
attributes are included in that attribute.
• So, X -> Y is a trivial functional dependency if Y is a subset of X.
Emp_id Emp_name
AS555 Harry
AS811 George
AS999 Kevin
• (Company} -> {CEO} (if we know the Company, we knows the CEO name)
• But CEO is not a subset of Company, and hence it's non-trivial functional
dependency.
Transitive dependency
• A transitive is a type of functional dependency which happens when t is indirectly formed by two
functional dependencies.
Company CEO Age
Microsoft Satya Nadella 51
Google Sundar Pichai 46
Apple Tim Cook 57
• {Company} -> {CEO} (if we know the compay, we know its CEO's name)
• { Company} -> {Age} should hold, that makes sense because if we know the company name, we can know his age.
• Note: You need to remember that transitive dependency can only occur in a relation of three or more attributes.
Advantages of Functional Dependency
σp(r)
• σ - is the predicate
•r - stands for relation which is the name of the table
•p - is prepositional logic formula which may use connectors like: AND OR and
NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
• For example: LOAN Relation
• OUTPUT
CustomerName Status
Google Active
Amazon Active
Apple Inactive
Alibaba Active
Union operation (υ)
• UNION is symbolized by ∪ symbol. It includes all tuples that are in tables A or in
B. It also eliminates duplicate tuples. So, set A UNION set B would be expressed
as:
The result <- A ∪ B
A ∪ B gives:
Table A ∪ B
Column 1 Column 2
1 1
1 2
1 3
Set Difference (-)
• (-) Symbol denotes it. The result of A - B, is a relation which includes all tuples
that are in A but not in B.
• The attribute name of A has to match with the attribute name in B.
• The two-operand relations A and B should be either compatible or Union compatible.
• It should be defined relation consisting of the tuples that are in relation A, but not in B.
• 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.
• Example:
A∩B
Table A ∩ B
Column 1 Column 2
1 2
Cartesian product(X)
• This type of operation is helpful to merge columns from two relations. Generally,
a Cartesian product is never a meaningful operation when it performs alone.
However, it becomes meaningful when it is followed by other operations.
σ 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
Join Operations
• Join operation is essentially a cartesian product followed by a selection criterion.
• Join operation denoted by ⋈.
• JOIN operation also allows joining variously related tuples from different relations.
Types of JOIN:
Various forms of join operation are:
• Inner Joins:
Theta join
EQUI join
Natural join
• Outer join:
Left Outer Join
Right Outer Join
Inner Join:
In an inner join, only those tuples that satisfy the matching criteria are included,
while the rest are excluded. Let's study various types of Inner Joins:
Theta Join
• The general case of JOIN operation is called a Theta join. It is denoted by
symbol θ
• Example A ⋈θ B
• Theta join can use any conditions in the selection criteria. For example:
1 2
EQUI join
• When a theta join uses only equivalence condition, it becomes a equi join. For
example:
A ⋈ A.column 2 = B.column 2 (B)
1 1
C D
Num Square Num Cube
2 4 2 8
3 9 3 9
•C⋈D C⋈D
Num Square Cube
2 4 8
3 9 18
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.
In the left outer join, operation allows keeping all tuple in the left relation.
However, if there is no matching tuple is found in right relation, then the attributes
of right relation in the join result are filled with null values.
Left Table (Courses) Right Table (HoD)
A B C D
100 Database 100 Alex
101 Mechanics 102 Maya
102 Electronics 104 Mira
Courses HoD
A B C D
•A B
Left Table (Courses) Right Table (HoD)
A B C D
100 Database 100 Alex
101 Mechanics 102 Maya
102 Electronics 104 Mira
Courses HoD
A B C D
•A B
Courses HoD
A B C D