L3 RDBMS
L3 RDBMS
It includes:
Table definitions( name of tables, their columns, and the data types allowed in
each column)
Constraints(Rules that govern the data, such as primary keys, foreign keys, and
data validation rules)
Relationships between tables( how tables are connected through shared
columns)
LET’S START WITH DBMS :)
Intension and Extension in DataBase
Intension in Database
Example:
customer(
id INT PRIMARY KEY,
name VARCHAR(50))
LET’S START WITH DBMS :)
Intension and Extension in DataBase
Extension in Database :
The extension is the actual data stored in the database at a given instance in time.
Basically the data which is stored in tuples/rows at a given instance of time.
When there are more tuples added the data can change .
Employee
Employee Employee
id name departmen
id name departmen id name departmen
1 Rahul 'IT'
1 Rahul 'IT' 1 Rahul 'IT'
2 Afsara 'HR'
2 Afsara 'HR' 2 Afsara 'HR'
3 Abhimanyu 'IT'
3 Abhimanyu 'IT' 3 Abhimanyu 'IT'
4 Aditya 'Marketing'
4 Aditya 'Marketing'
5 Raj 'Finance'
Data at instance t1 Data at instance t2
Data at instance t3
LET’S START WITH DBMS :)
RDBMS
What is RDBMS ?
Database
LET’S START WITH DBMS :)
RDBMS
What is RDBMS ?
Database
MySQL MongoDb
Oracle
MariaDB,
LET’S START WITH DBMS :)
RDBMS
What is RDBMS ?
These databases structure data into organized tables that have predefined
connections between them.
Data manipulation and querying are performed using SQL (Structured Query
Language).
Database
LET’S START WITH DBMS :)
Normalisation and its types
Normalisation
Normalization is a process in which we organize data to reduce
redundancy(duplicacy) and improve data consistency. It involves dividing a database
into two or more tables
When there is same set of data repeated each and every time it results in
duplicacy of data (either in row or column)
Now row level duplicacy can be remove by using primary key for unique
values.
LET’S START WITH DBMS :)
Normalisation and its types
Now when we have same data for some set of columns , it leads to different
anomalies (inconsistencies or errors that occur when manipulating or
querying data in a database)
1. Insertion Anomaly
2. Updation Anomaly
3. Deletion Anomaly
Also it also increases the size of database with the same data.
LET’S START WITH DBMS :)
Normalisation and its types
data into the database due to the 2 Afsara 26 'HR' Avinash 1000
Abhimany
3 27 IT
u
4 Aditya 25 HR
5 Raj 24 HR
LET’S START WITH DBMS :)
Normalisation and its types
Types of Normalisation
Consider if you wish to find the 2 Afsara 26 HR 1 Rahul 25 'IT' Raj 1500
salary of Rahul
3 27 IT
u
3 Abhimanyu 27 'IT' Raj 1500
4 Aditya 25 HR
5
Aditya
Raj
25
24
'HR'
'HR'
Avinash
Avinash
1000
1000
IT Raj 1500
table to find the salary of Rahul HR Avinash 1000
LET’S START WITH DBMS :)
Denormalization
Benifits
Faster Queries : It can reduce the need for complex joins between tables
during queries which can eventually improve the speed of retrieving frequently
accessed data.
Simpler Queries: It can simplify queries by allowing them to be executed on a
single table instead of requiring joins across multiple tables.
Disadvantages
R
FD : X(determinant) -> Y(dependent)
X Y
1 Riti Kumari
2 Rahul Kumar
3 Suraj Singh
LET’S START WITH DBMS :)
Functional Dependecy
Trivial dependency
A functional dependency X -> Y is trivial if Y is a subset of X
We can also say it as X->X .
{EmpID, EmpFirstName} -> {EmpID}
is trivial because {EmpID} is a subset of {EmpID, EmpFirstName}.
X Y =Y
EmpID EmpFirstName EmpLastNmae
1 Riti Kumari
2 Rahul Kumar
3 Suraj Singh
LET’S START WITH DBMS :)
Functional Dependecy
Non-Trivial dependency
A functional dependency X -> Y is non-trivial if Y is not a subset of X i.e X Y=
{EmpID} -> {EmpFirstName}
is trivial because {EmpFirstName} is not a subset of {EmpID}.
X Y = empty
EmpID EmpFirstName EmpLastNmae
1 Riti Kumari
2 Rahul Kumar
3 Suraj Singh
LET’S START WITH DBMS :)
Attribute closure/closure set
Attribute closure helps us for identifying candidate keys, checking for functional
dependencies, and in normalisation.
1. Now according to the rule of Reflexivity all the attributes can determine theirself.
A->A, B->B , C->C , D->D, E->E
3. Now according to the rule of UNION if as the determinant is same we can combine
dependent
For A For C For E
A-> B , A->C, A-> D, A->E , A->A C-> D, C->E , C->C E->E
A->ABCDE C->DEC
For B For D
B-> C , B->D , B->E , B->B D-> E , D->D
B->CDEB D->ED
LET’S START WITH DBMS :)
Attribute closure
Ques : Consider we have a relation R with atrributes A,B,C,D,E and FD are
A-> B, B-> C, C-> D, D-> E
A- {A,B,C,D,E}
B- {B,C,D,E}
C-{C,D,E}
D- {D,E}
E- {E}
AB - {A,B,C,D,E}
LET’S START WITH DBMS :)
Attribute closure
5. Now lets find candidate key, super key , prime and non-prime attributes
Super key : Set of attributes whose closure contains all the attributes given in a relation
Super set of any candidate key is super key. A key(combination of all possible attributes)
which can uniquely identify two tuples.
C+ ={C}
Clsoure of A gives or determins all the attributes in the table so we can say its s
auper key.
A power set is the set of all subsets of a given set, including the empty set and
the set itself. If you have a set X, the power set of X is denoted as 2^X
Example:
We can also say to find max number of super keys we can use the formula
where 2^n-k
k- candidate key with k attributes (k < n) in a relation
n- total no of attributes in a relation
Q. Find no of candidate and super key for the given relation R (A,B,C,D) and functional
dependency A->B, B->C, C->D , B->A
First normal form is the first step in the normalisation process which helps us to
reduce data redudancy. Every table should have atomic values i.e there shouldn’t be
any multivalued attributes
Atomicity: Each column contains only indivisible (atomic) values, meaning each
attribute holds a single value.
ID PersonName Order
1 Raj Muffin,Sugar
2 Riti Muffin
3 Rahul Sugar,Egg
1. Repeat the values in id and PersonName column twice to store single value of
multivauled attribute order
ID PersonName Order
1 Raj Muffin
3 Rahul Sugar
3 Rahul Egg
LET’S START WITH DBMS :) ID PersonName Order
PK - ID
LET’S START WITH DBMS :) ID PersonName Order
3. Divide the table into student(base) and order(referencing) table based on the
multivalued attribute order.
pk fk
ID PersonName ID Order
1 Raj 1 Muffin
2 Riti 1 Sugar
3 Rahul 2 Muffin
3 Sugar
3 Egg
LET’S START WITH DBMS :)
Normalisation and its types
1 1 Muffin
Candidate key : CustomerId+OrderId
2 1 Muffin
Prime attribute :{CustomerId,OrderId}
Non-prime attribute : {OrderName} 1 2 Sugar
4 2 Sugar
1 1 Muffin
2NF
2 1 Muffin
1 2 Sugar
4 2 Sugar
1 1 1 Muffin
2 1 1 Muffin
1 2 2 Sugar
4 2 2 Sugar
LET’S START WITH DBMS :)
Normalisation and its types
Consider there is a relation R(A,B,C,D) with FD : AB->C, AB->D, B->C. Find if this is in
2NF?
Not in 2NF.
LET’S START WITH DBMS :)
Normalisation and its types
For any functional dependency X→Y, one of the following conditions must be true to be in
3rd normal form.
X is a superkey or candidate key(LHS)
Y is a prime attribute (i.e., part of some candidate key). (RHS)
LET’S START WITH DBMS :)
Normalisation and its types
Consider there is a relation R(A,B,C,D) with FD : AB->C, C->D. Find if this is in 3NF?
Not in 3NF.
LET’S START WITH DBMS :)
Normalisation and its types
Consider there is a relation R(A,B,C,D) with FD : AB->C, AB->D. Find if this is in BCNF?
It is in BCNF.
LET’S START WITH DBMS :)
Dependency Preserving decomposition
Consider a relation R(A, B, C) with FD : A->B, B->C, find if its dependency preserving when
divided into R1(AB) and R2(BC)
R1(AB) : A → B
R2(BC) : B → C
The decomposition is dependency preserving because the functional dependencies A → B
and B → C are preserved in R1 and R2
LET’S START WITH DBMS :)
Dependency Preserving decomposition
Lossy Decomposition
Lossy decomposition 4 2 6
Step 1: Lets decompose the relation based on any attribute and keep that
attribute as common, for now lets use B as common attribute
Decomposed relations: R1(A,B) and R2(B,C)
A B B C
1 2 2 3
4 2 2 6
R1 R2
LET’S START WITH DBMS :)
A B C
Lossy decomposition 4 5 6
A B C
We can see some additional
1 2 3
tuples that were not in the
1 2 6 original relation R (lossy
decomposition)
4 2 3
4 2 6
R1 natural join R2
LET’S START WITH DBMS :)
Lossy and Lossless decomposition
Lossy decomposition
How to ensure a decomposition is lossless
1. Divide or decompose the table on basis of CK or SK present in the relation
so that there is no duplicacy
2. For a decomposition to be lossless
a. R1 U R2= R
b. R1 R2 = common attribute
3. To ensure that a decomposition is lossless, a common approach is to use
the dependency preservation property
LET’S START WITH DBMS :)
Lossy and Lossless decomposition
Lossless Decomposition
Lossless Decomposition
So if the table is decomposed and we want to query the attributes present in both the
tables we will use the join operation.
Natural Join :
The natural join operation combines tuples(rows) from two relations based on
common attributes.
It only includes those combinations of tuples that have the same values for the
common attributes.
LET’S START WITH DBMS :)
A B C
Lossless decomposition 4 5 6
A B A C
1 2 1 3
4 5 4 6
R1 R2
LET’S START WITH DBMS :)
A B C
Lossless decomposition 4 5 6
A B C
1 2 3
4 5 6
R1 natural join R2
LET’S START WITH DBMS :)
Normalisation and its types
Multivalued dependency :
A multi-valued dependency X→→Y X→→Z in a relation R(X,Y,Z) implies that for each value
of X, there is a set of values for Y and a set of values for Z that are independent of each
other.
LET’S START WITH DBMS :)
Normalisation and its types
1 Sci 123
2 Hin 910
2 Eng 910
LET’S START WITH DBMS :)
StudentId Course
1 Sci 345
2 Hin 678
StudentId PhoneNbr
2 Eng 678
1 123
2 Hin 910
1 345
2 Eng 910
2 678
2 910
4NF
LET’S START WITH DBMS :)
Normalisation and its types
A B A C
1 2 1 3
4 5 4 6
R1 R2
LET’S START WITH DBMS :)
A B C
A B C
1 2 3
4 5 6
R1 natural join R2
LET’S START WITH DBMS :)
Normalisation and its types
Step 1: Identify the candidate key for the given relation using FD and
closure method.
Step 2: Find the prime and non-prime attributes.
Step 3: Start checking for normal forms one by one according to their rule
BCNF
3NF
2NF
1NF
LET’S START WITH DBMS :)
Normalisation and its types
A→BC = A is a CK
B→C = B is a CK
A→B= A is a CK
AB→C= AB is a combination of candidate keys, Its SK
B→A= B is a CK , R is in BCNF.
The highest normal form for the given relation R(A,B,C,D) is BCNF.
LET’S START WITH DBMS :)
How to normalise table
Step 2: ABCDE, Since we are assuming our relation R is in a standard relational model, it is
already in 1NF
Step 1: Decompose FDs (RHS) i.e X->AB can be written as X->A, X->B
Find Minimal Cover FD: A→BC , B→C, A→B, AB→C all the attributes of a table, if yes you can remove that, if no jump to the next
one.
Step 1 : A->B, A->C, B->C, A->B, AB->C Step 3: Remove unnecessary attributes from LHS, if the determinant is a super
key, it can be reduced to CK (minimal super key)
FD: A->B, A->C, B->C, AB->C
3. For B->C
Step 2 : FD: A->B, AB->C
1. For A->B B+={B},since B+ doesn’t have all the
FD: A->C, B->C, AB->C attributes we shouldn’t discard this
A+={A,C} since A+ doesn’t have all the attributes we
shouldn’t discard this 4. For AB->C
FD: A->B, B->C
2. For A->C
AB+={A,B,C} since AB+ have all the
FD: A->B, B->C, AB->C
attributes we can discard this
A+={A,B,C}, since A+ have all the attributes we can
discard this
LET’S START WITH DBMS :) Step 3: Ensure that every functional dependency in set2 is in set1 closure
Step 4: If both subset checks pass, then set1 and set2 are equivalent.
F={A→B,B→C} G={A→C,A→B}
Closure of F, attributes -> A,B,C Closure of G, attributes -> A,B,C
A+={A,B,C} A+={A,C,B}
B+={B,C} B+={B}
C+={C} C+={C}