Adobe Scan 20 Jun 2022
Adobe Scan 20 Jun 2022
A JOIN clause is used to combine rows from two or more tables, based on a related column between
them.
1. INNER JOIN: The INNER JOIN keyword selects records that have matching values in both tables
Syntax:
select column_name(s) from tablel inner join table2
on tablel.column_name =
table2.column_name
2. LEFT JOIN: The LEFT JOIN keyword returns all records from the left table (tablel), and the matched
records from the right table (table2). The result is NULL from the right side, if there is no match.
Syntax:
select column_namefs}from table lleft join table2
on tablel.column_name = lable2.column_name;
3. RIGHT JOIN: The RIGHT JOIN keyword returns all records from the right table (table2), and the
matched records from the left table (table1). The result is NULL from the left side, when there is no match.
Syntax:
select column_name(s)from table lright join table2
on tablel.column_name table2.column_name;
4. FULL JOIN: The FULL OUTER JOIN keyword return all records when there is a match in either left
(table 1) or right (table2) table records.
Syntax:
select column_namets)from table lfull outer join table2
on tablel.column_name = table2.column_name;
5. SQL SELF JOIN: A self JOIN is a regular join, but the table is joined with itself.
Syntax:
select column_name(s) from tablel tl, tablel t2 where condition;
EXAMPLES:
Consider the following two tables.
Table 1-CUSTOMERS Table isas follows.
InnerJoin
SQL> SELECT id, name, amount, date FROM customers
INNER JOIN orders
ON customers.id = orders.customer_id;
Left Join:
SQL> SELECT id, name, amount, date FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id;
Right Join
SQL> SELECT id, name, amount, date FROM customers
RIGHT JOIN orders
ON customers.id =
orders.customer _id;
This would produce the following result-
ID NAME AMOUNT |DATE
3 Kaushik | 3000 2009-10-08-00:00:00
3 Kaushik1500 2009-10-08 00:00:00
2 Khilan 1560 2009-11-20 00:00:00
4Chaitali 2060 2008-05-20 00:00:00
Full Join:
SQL> SELECT id, name, amount, date FROM customers
FULL JON orders
ON customers.id = orders.customer_id;
ID NAMEAMOUNT | DATE
RameshNULL NULL
Khilan 1560 2009-11-20 00:00:00
Kaushik 3000 2009-10-08-00:00:00
Kaushik 1500 2009-10-08 00:00:00
Chaitali2060 2008-05-20 00:00:00
5 Hardik NULL NULL
6 Komal NULL NULL
7 Muffy NULL NULL
Kaushik 3000 2009-10-08-00:00:00
3Kaushik 1500 2009-10-08 00:00:00
2 Khilan 1560 2009-11-20 00:00:00
4Chaitali 2060 | 2008-05-20 00:00:00
VIEWS:
Aview is nothing more than a SQLstatement that is stored in the database with an associated name. A
viewis actually a compositionof atable in the form of a predefinedSQL query. Aview can containall rows of
atable or select rowsfrom a table. A view canbe createdfrom one or many tables which dependson the
writtenSQL query to create aview.
Views, which areatypeof virtualtables allow users todo thefollowing -
Syntax:
<compound query
UNION |UNION ALL| MINUS | INTERSECT
compound query>
UNION: Combines the result of 2 select statements into one result set, and then estimates any duplicate rows
from that result set.
UNION ALL: Combines the result of2 SELECT statements into one result set including the duplicates.
INTERSECT: Returns only the rows that are returned by each of two SELECT statements.
MINUS: Takes the result set of each SELECT statement, and removes those rows that are also recommended
by a second SELECT statement.
Q. What is Normal Form? What are steps in Normal Form2
NORMALIZATION: Normalization is the process of decomposing relations to produce
Smaller, well-structured relation.
To produce smaller and well structured relations, the needs to
user follow six
normal forms.
Steps in Normalization
normal form is state of relation that result from applying simple rules from
regarding functional dependencies ( relationships between attributes to that relation. The
normal form are
1 First normal form 2. Second normal form
3. Third normal form 4. Boyce/codd normal form
5. Fourth normal form 6. Fifth normal form
1) First Normal Form: Any multi-valued attributes (also called repeating groups) have
been removed,
2) Second Normal Form: Any partial functional dependencies have beenremoved.
3) Third Normal Form: Any transitive dependencies have been removed.
4) Boyce/Codd Normal Form: Any remaining anomalies that result from functional
dependencies have been removed.
5) Fourth Normal Form: Any multi-valued dependencies have beenremoved.
6) Fifth Normal Form: Any remaining anomalies have beenremoved.
pation Anamoly : 1f data items are scattered and are not linked to cach other properly, then it
Coulo to strange situations. For example, when we try to update one data item having its copies
instances get updated properly while a few others left wath old
Tedover several
places, a few are
Professor
ID Name Basic TA
TA DA HRA INF
Rama 40000 10000 15000 10000
ww
SECOND NORMAL EORM (2NE):
t denends on the concept of Full Functional
ndencies(PFD) i.e., for a relation that has Dependences(FFD)
and disallows Partial
concatenated Functional
f the primary key must depend upon the entire primary key, each attribute in the table that is
concatenated not
ly on one part of the concatenated key, then the relation fails key for its existence. If any attribute depends
on Second normal form.
A relation with Partial Functional
dependencies can be made as in 2NF by removing them.
PFD: part of key Non Key
FFD: Key Non key
Example:
Medication
Patient No. | Drug
Dependencies
No. of units Pname
P1 D 10 Kiran Patient No. Pname
P1 D2 20 Kiran
P1 D3 15 Patient
Kiran No.,Drug No. of units
P2 D4 15 Raj
The above relation is in 1NF but not in
2NF. for the relation is Patient No. and
eliminating the Partial functional dependencies, the Key Drug. After
decomposed relations are
Patient
Parent Relation Dependencies
Patient No. Pname
P1 Kiran Key: Patient No. Patient No. > Pname
P2
Raj
Pl D3
referring Patient No. in the
15
Parent Relation (Patient)
P2 D4 15
The above 2 relations satisfy 2NF. They don't have partial functional dependencies.
Note: If key is only one attribute then the relation is always in 2NF.
awewweeoeneonaewreeraoo****A**** *****
wwww.eonaaM n
THIRD NORMAL FORM (3NE
Third Normal form applies that
every non-prime attribute of a relation must be
key, or we can say that, there should not be the case that a dependent on primary
prime attribute. non-prime attribute is determined by another
So this transitive functional
relation must be in Second Normal form. dependency should be removed from the relation and alsonon-
Simply, It is based on the concept of transitive the
disallows the transitive dependencies. dependencies. 3NF
Dependency: Key » Non Key
Transitive Dependency: Non Key Non
-
Key
A relation
satisfying 2NF and with Transitive Functional
dependencies can be made in 3NF
transitive functional dependencies. as
by removing the
Example:
Contains
Key
Patient No. Pname Ward No. Ward Name Patient No. and Pname
PI Kumar WI ICU Dependencies
P2 Kiran WI ICU Patient No., Pname Ward No.
P3 Kamal W1 ICU Ward No.» Ward Name (Transitive
P4 Sharath| W2 General Dependency)
Ward No. Ward Name is transitive because
transitive dependency must be removed Ward No. is not a key. To make this relation
from it. It can be done by satisfy 3NF
relations are as follows: decomposing the relation. The decomposed
Ward Key Dependencies
Ward No. | Ward Name
W1 ICU Ward No. Ward No. » Ward Name
W2 General
Contains
Primary Key Dependencies
Patient No. | Pname Ward No. Patient No. and Pname
PI Kumar W1
P2 Kiran W1
Patient No.,Pname Ward No.
Foreign Key
P3 Kamal WI Word No. refers Ward
P4
Sharath No. in relation Ward
W2
A multi valued dependency exists here because all the attributes depend upon the other and yet none of them is
V1 | I1 P3
Vi 12 P3
v2 12 P1
V2 13 P1
V3 I1 P2
V3 I1 P3
Vendor_Supply Vendor_Project
Vendor Code Item Code Vendor Code Project No.
V1 I1 Vi P1
V1 12 V P3
V2 12 V2 P1
V2 13 V3 P2
V3
SUMA
BOYCE-CODD NORMAL FORMBCNF:
This form deals with certain type of anamoly that is not|
BCNF is a higher version of the Third Normal form.
candidate keys is said to be in
handled by 3NF. A 3NF relation which does not have multiple overlapping
BCNF. For a relation to be in BCNF, following conditions must be satisfied:
PI Physics Ghosh 50
P1 Mathematics Krishnan 50
P2 Chemistry Rao 25
P2 Physics Ghosh 75
The given relation is in 3NF. Observe, however, that the names of Dept. and Head of
duplicated. Further, if Professor P2 resigns, rows 3 and 4 are deleted. We lose the Dept. are
Head of Department of Chemistry.
information that Rao is the
The normalization of the relation is done by
creating a relation for Dept. and Head of
Head of Dept. form the given relation. The normalized
new
Dept. and deleting
relations are shown in the following.
6
Professor_work Dependencies
Professor Code Department Percent Time
Department,Professor Code Percent
P1 Physics 50 time
P Mathcmatics 50 Department is
foreign key referring
P2 Chemistry 25 Department in the Deartment_Details
P2
relation
Physics 75
P3 Mathematics 100
Depatrment_Details Dependencies
Department Head of Dept. Department» Head of the Department
Physics Ghosh
Mathematics Krishnan
Chemistry Rao
schema Rennement (Nomalization)
4.23
FTH NORMAL FORM (5NF)
18
plain about Fith Normal Fom (5NF).
xpla
This table contns paur wse cyclic dependencies in the primary key. To detemmine
Emp_ad
the performance, must d one
and
student and
its Smary determine emp Address student,oDe identify student, performance
to and must
respectively.Ihis can de done by decomposing the table into three tables as follows,
Kumar Delhi
Table: Stdent-Emp Addross
Student Performance
Ravi Good
Kumar Good
Ravi Average
Kumar Average
Ravi
Toble: StudestPorformance
Delni Best
Teble: Emp_Address-rerformance
STUDENTS
POR EvoiNEERING
sPECTROM AuANONE JOaRNAL
Q914. Explain about lossless join decomposiuon.
Answer t
Model Paper4, Q(o)
Lossless-joln Decomposition
or
is onc of the properlies
Lossless-join decomposition
dccompositions. This dependency is also called as non-addilnve
In more specific way, lossiess
Or non-los Join dependency.
a
no
can be defined as the one which generates
Join dependency
additional tuples when the natural "join" operation is pertomed
Example
Consider the "STUDENT" relation.
STUDENT (Std id, Name, Location)
This relation can be broken-down into two relations as
follows,
(a). Location (Std_id, Location) and
Location Name
Std
012 Hyderabad Radha
013 Meena
Secunderabad
014 Hyderabad Pinky
015 Secunderabad Rani
No additional tuples are generated and neither data is
duplicated nor data is lost. Therefore the relation STUDENT
is lossless.
.18 DATABASE MANAGEE
Example
Given thal,
R(A, B, C. D, E. F)
he FDs ar
A BC
FA
CA
DE
EA
eromposed Relatlons
8, (4. C, D), R(B, C, D). R,(6, F, D)
In order to check **
whether the given relations are lossless or not, the following three conditions are to be satisied.
Condition
Every attnbute in R should be in at least one R, where 1 SiSn
Conditlon 2
Condition3
0)R,R,~R,
or R, nR, R
1e, the attnbutes common to both
R, and R, functionally detemine the attribute in either R, or R,
(n) Find the closure
of the intersection clement and verify if at least one of the functional dependencies are in the closure
Based on the three conditions, the
given example is solved as,
Condition1
Conditlon 2
Condition3
(CD)-CDAEBC
(D-DEADBC
) The functional dependencies in (CDy' C-A, DE and in (D)' areD-E,
are ~E
A- BC
Since, all the three conditions satisfied, the given decomposed relations
are
are lossless.
Requlrements of Lossless Decomposition
Let Rbe arelation schema. Aset of
relation
every attribute in Rshould present in at lcast on R0 schemas (R,. R KIS decomposition ofR ifR a
TRIVIALFUNCTIONAL DEPENDENCY
.Trivial-If a functional dependency (FD) X »
Y holds, where Y subset of X, then
is a it is called a
trivial FD. Trivial FDs always hold.
Non-trivial If an FD X- Y holds, where Y is not a subset ofX, then called
it is a non-trivial FD.
Completely non-trivial -
is also tunctional
functional dependency then by augmentation. Xz-YZ
a
Augmentation:_If X-Y is a
dependency
3.Transitivity; IF X-Y and Y-Z are two functional dependencies then by transitivity, X-Z is also a
functional depcndency.
Union: If X-Y and XZ are two functional dependencies then, XYZ is alsoa functional dependency.
S. Decomposition: If x-YZ is a functional dependcncy then XY and X-Z are also functional
dependencies.
CLOSURE SET OF A FUNCTIONAL DEPENDENCY (F) It is
It is a set of all functional dependencies that can be determined using the given set of dependencies.
denoted by F.
of all the attributes that can be dctermined using X. It is denoted by X*,
Attribute Closure (X:It is a set
Example:
R(A.B.C) F:{A-B, B-C}
A {A.B.C} B {B.C} C {C}
ABC={A.B.C}
AB {A,B.C} AC°={A.C,B} BC"={B,C}
associated with it
ldentifving keysin the given relation based on Functional Dependencies
set X of attributes.
X is a set of attributes that can be determined using given
the
relation.
IfX contains all the attributes of a relation, then X is called "Super key" of that
relation.
I f X is minimal set, then X is called "Candidate Key" of that
attributes of that
If noclosure contains all the elements then in such a case we can find independent
If the closure of the Independent
relation i.e., the attributes that which are not in the R.H.S. of any dependency.
attributes contains all the elements then it can be treated as a candidate key.
elements then we try to find the key
If the closure of independent attributes also doesn't contain all the
attributes one by one. If couldn't find key then we can add groups of dependent
by adding dependent we