Dbms 3 Notes
Dbms 3 Notes
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SELECT MIN(Price) AS SmallestPrice
FROM Products;
SELECT MAX(Price) AS LargestPrice
FROM Products;
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
• SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Produ
cts.SupplierID = Suppliers.supplierID AND Price < 20);
The SQL ANY and ALL Operators
The ANY and ALL operators allow you to perform a comparison between
a single column value and a range of other values.
The SQL ANY Operator
The ANY operator:
returns a boolean value as a result
returns TRUE if ANY of the subquery values meet the condition
ANY means that the condition will be true if the operation is true for any
of the values in the range.
• SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
• SQL SELECT INTO Examples
• The following SQL statement creates a backup copy of Customers:
• SELECT * INTO CustomersBackup2017
FROM Customers;
Normalization
Normalization is the process of organizing the data in the database.
Normalization is used to minimize the redundancy from a relation or set
of relations. It is also used to eliminate the undesirable characteristics
like Insertion, Update and Deletion Anomalies.
Normalization divides the larger table into the smaller table and links
them using relationship.
The normal form is used to reduce redundancy from the database table.
Purpose of Normalization
Normalization is the process of structuring and handling the
relationship between data to minimize redundancy in the relational
table and avoid the unnecessary anomalies properties from the database
like insertion, update and delete.
It helps to divide large database tables into smaller tables and make a
relationship between them. It can remove the redundant data and ease
to add, manipulate or delete table fields.
Objective of Normalization
It is used to remove the duplicate data and database anomalies from the
relational table.
Normalization helps to reduce redundancy and complexity by examining
new data types used in the table.
It is helpful to divide the large database table into smaller tables and
link them using relationship.
It avoids duplicate data or no repeating groups into a table.
It reduces the chances for anomalies to occur in a database.
Types of Anomalies
2. Insert Anomaly:
An insert anomaly occurs in the relational database when some
attributes or data items are to be inserted into the database without
existence of other attributes.
For example, In the Student table, if we want to insert a new courseID,
we need to wait until the student enrolled in a course. In this way, it is
difficult to insert new record in the table. Hence, it is called insertion
anomalies.
3. Update Anomalies:
The anomaly occurs when duplicate data is updated only in one place
and not in all instances. Hence, it makes our data or table inconsistent
state.
For example, suppose there is a student 'James' who belongs to Student
table. If we want to update the course in the Student, we need to update
the same in the course table; otherwise, the data can be inconsistent.
And it reflects the changes in a table with updated values where some of
them will not.
4. Delete Anomalies:
An anomaly occurs in a database table when some records are lost or
deleted from the database table due to the deletion of other records. For
example, if we want to remove Trent Bolt from the Student table, it also
removes his address, course and other details from the Student table.
Therefore, we can say that deleting some attributes can remove other
attributes of the database table.
So, we need to avoid these types of anomalies from the tables and
maintain the integrity, accuracy of the database table. Therefore, we use
the normalization concept in the database management system.
Example:
R(A B C D)
AB->D
B->C
Find essential attribute and C.key
(AB)=ABCD
Prime attribute—A B
Non Prime attribute --- C D
• R(ABCDE)
• AB->C
• D->E
• AB->C IS PD
• D->E Is PD
• So not in 2NF
• So decompose
R(ABCDE)
AB->C
D->E.
Try This
R(ABCDE)
A->B
B->E
C->D
NO NOT IN 2NF
R(ABC
DEFGH
IJ)
AB->C
AD->GH
BD->EF
A->I
H->J
Third Normal Form (3NF)
A relation will be in 3NF if it is in 2NF and not contain any transitive
partial dependency.
3NF is used to reduce the data duplication. It is also used to achieve the
data integrity.
If there is no transitive dependency for non-prime attributes, then the
relation must be in third normal form.
A Table is said to be in 3NF only when it is in 2NF and should not have
any transitive dependency.
TD means a non prime attribute depending on non prime attribute( like
an irregular stud depends on another irregular stud)
R(ABC)
A->B
B->C
A- CKEY PA=A NPA=BC
3NF
R(ABCBDE)
A->B
B->E
C->D
AC-ESSENTIAL
(AC)=ABCDE---C.KEY
AB->C --- NO TD
C->B----NO TD
If there exists FD from a->b
Then a should be super key and b can be anything
A prime attribute shud not depend on prime attribute
Decompose—R1(ab) R2(AC) R3(BC)
R(ABCDE)---CHECK THIS
AB->CD
D->A
BC->DE
Normalization
If a database design is not perfect, it may contain anomalies,
which are like a bad dream for any database administrator.
Managing a database with anomalies is next to impossible.
indivisible units.
We re-arrange the relation (table) as below, to convert it to First
Normal Form.
To bring this relation into third normal form, we break the relation
into two relations as follows −
Stu_ID → Stu_Name,
Zip and
Zip → City
SUBJECT -->
LECTURER
SUBJECT-->BOOKS