0% found this document useful (0 votes)
33 views44 pages

Second Normal Form (2NF)

Uploaded by

vinashreemeshram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views44 pages

Second Normal Form (2NF)

Uploaded by

vinashreemeshram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Second Normal Form (2NF)

• In the 2NF, relational must be in 1NF.


• In the second normal form, all non-key
attributes are fully functional dependent
on the primary key
• Example: Let's assume, a school can store
the data of teachers and the subjects they
teach. In a school, a teacher can teach
more than one subject.
Teacher Table
TEACHER_ID SUBJECT TEACHER_AGE

25 Chemistry 30

25 Biology 30

47 English 35

83 Math 38

83 Computer 38
• In the given table, non-prime attribute
TEACHER_AGE is dependent on TEACHER_ID
which is a proper subset of a candidate key.
That's why it violates the rule for 2NF.

• To convert the given table into 2NF, we


decompose it into two tables:
TEACHER_DETAIL
table:

TEACHER_ID TEACHER_AGE

25 30

47 35

83 38
TEACHER_SUBJECT table:

TEACHER_ID SUBJECT

25 Chemistry

25 Biology

47 English

83 Math

83 Computer
• Second Normal Form (2NF) is a step in the database
normalization process used to organize data and eliminate
redundancy in relational databases. A table is in 2NF if it
satisfies the following two conditions:
• It is already in First Normal Form (1NF):
– The table must not contain repeating groups or arrays.
– Data is stored in a tabular format with atomic values in each cell.
• It eliminates partial dependency:
– In 2NF, all non-key attributes must be fully functionally
dependent on the entire primary key, not just a part of it.
– This condition is especially important for tables that have
composite primary keys (where the primary key consists of more
than one column). No non-key attribute should depend on only
one part of a composite key.
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 relation is in third normal form if it holds atleast one of
the following conditions for every non-trivial function
dependency X → Y.
• X is a super key.
• Y is a prime attribute, i.e., each element of Y is part of some
candidate key.
EMP_ID EMP_NAME EMP_ZIP EMP_STATE EMP_CITY

222 Harry 201010 UP Noida

333 Stephan 02228 US Boston

444 Lan 60007 US Chicago

555 Katharine 06389 UK Norwich

666 John 462007 MP Bhopal


• Super key in the table above:
• {EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_
NAME, EMP_ZIP}....so on
• Candidate key: {EMP_ID}
• Non-prime attributes: In the given table, all
attributes except EMP_ID are non-prime.
• Here, EMP_STATE & EMP_CITY dependent on
EMP_ZIP and EMP_ZIP dependent on EMP_ID. The
non-prime attributes (EMP_STATE, EMP_CITY)
transitively dependent on super key(EMP_ID). It
violates the rule of third normal form.
• That's why we need to move the EMP_CITY and
EMP_STATE to the new <EMPLOYEE_ZIP> table,
with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMP_ID EMP_NAME EMP_ZIP

222 Harry 201010

333 Stephan 02228

444 Lan 60007

555 Katharine 06389

666 John 462007


EMPLOYEE_ZIP table:

EMP_ZIP EMP_STATE EMP_CITY

201010 UP Noida

02228 US Boston

60007 US Chicago

06389 UK Norwich

462007 MP Bhopal
SQL Commands

• SQL commands are instructions. It is used to


communicate with the database. It is also
used to perform specific tasks, functions, and
queries of data.
• SQL can perform various tasks like create a
table, add data to tables, drop the table,
modify the table, set permission for users.
Types of SQL Commands

• There are four types of SQL commands:


• DDL
• DML
• DCL
• TCL
Data Definition Language (DDL)

• DDL changes the structure of the table like


creating a table, deleting a table, altering a
table, etc.
• All the command of DDL are auto-committed
that means it permanently save all the
changes in the database.
Following are the some commands that come under DDL:
Data Manipulation Language

• DML commands are used to modify the


database. It is responsible for all form of
changes in the database.
• The command of DML is not auto-committed
that means it can't permanently save all the
changes in the database. They can be rollback.
• Following are the some commands that come
under DML:
• a. CREATE It is used to create a new table in the database.
• Syntax:
• <
• CREATE TABLE TABLE_NAME (COLUMN_NAMES DATATYPES
[ ...]);
• In above statement, TABLE_NAME is the name of the table,
COLUMN_NAMES is the name of the columns and
DATATYPES is used to define the type of data.
• Example:
• CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VAR
CHAR2(100), DOB DATE);
• DROP: It is used to delete both the structure and record stored
in the table.
• Syntax: To DROP a table permanently from memory
• DROP TABLE table_name [cascade constraint];
• The cascade constraint is an optional parameter which is used
for tables which have foreign keys that reference the table
being dropped. If cascade constraint is not specified and used
attempt to drop a table that has records in a child table, then
an error will occur. So by using cascade constraints, all child
table foreign keys are dropped.
• Example
• DROP TABLE EMPLOYEE;
• c. ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.
• Following are the list of modifications that can be done using ALTER command.
• With the use of ALTER commands we can add or drop one or more columns form existing
tables.
• Increase or decrease the existing column width by changing the data type
• Make an existing mandatory column to optional.
• Enable or disable the integrity constraints in a table. We can also add, modify or delete
the integrity constraints from a table.
• We can also specify a default value for existing column in a table.
• Adding new columns in Table:
• With the use of ALTER table command we can add new columns existing table.
• Syntax: To add a new column in the table
• ALTER TABLE table_name ADD column_name column-definition;
• In the above syntax, where table_name corresponds to the name of the table, column-
definition corresponds to the valid specifications for a column name and data type.
• EXAMPLE:
• ALTER TABLE STU_DETAILS ADD (ADHAR_NUM VARCHAR2 (15));
• Syntax: To ADD a multiple column from a table.
• ALTER TABLE table_name ADD column_name1, column_name2;
• Example:
• ALTER TABLE STU_DETAILS ADD ADHAR_NUM, NAME;
• TRUNCATE: It is used to delete all the rows
from the table and free the space containing
the table.
• Syntax:
• TRUNCATE TABLE table_name;
• Example:
• TRUNCATE TABLE EMPLOYEE;
• Rename: It is used to rename the table.
• Syntax:
• Rename <OLD_TABLENAME> to <NEW_TABLENAME>;

• In the above syntax, Rename is a command,


<OLD_TABLENAME> is the name of the table and
<NEW_TABLENAME> is the name that you have
changed.
• Example:
• Rename STU_NAME to STUDENT_NAME;
DML Commands in SQL

• DML is an abbreviation of Data Manipulation


Language.
• The DML commands in Structured Query
Language change the data present in the SQL
database. We can easily access, store, modify,
update and delete the existing records from
the database using DML commands.
DML commands in SQL
• Following are the four main DML commands in SQL:
• SELECT Command
• INSERT Command
• UPDATE Command
• DELETE Command
• SELECT DML Command
• SELECT is the most important data manipulation command
in Structured Query Language. The SELECT command shows
the records of the specified table. It also shows the
particular record of a particular column by using the
WHERE clause.
Syntax of SELECT DML command

• SELECT column_Name_1, column_Name_2, ….


., column_Name_N FROM Name_of_table;
• Here, column_Name_1, column_Name_2,
….., column_Name_N are the names of those
columns whose data we want to retrieve from
the table.
If we want to retrieve the data from all the columns of the table, we
have to use the following SELECT command:
1.SELECT * FROM table_name;
Examples of SELECT Command
Example 1: This example shows all the values of every column
from the table.
2.SELECT * FROM Student;
This SQL statement displays the following values of the student table:

Student_ID Student_Name Student_Marks


BCA1001 Abhay 85
BCA1002 Anuj 75
BCA1003 Bheem 60
BCA1004 Ram 79
BCA1005 Sumit 80
INSERT DML Command
• INSERT is another most important data
manipulation command in Structured Query
Language, which allows users to insert data in
database tables.
• Syntax of INSERT Command
• INSERT INTO TABLE_NAME ( column_Name1 , colu
mn_Name2 , column_Name3 , .... column_NameN
) VALUES (value_1, value_2, value_3, .... value_N )
;
• Eg.INSERT INTO Student (Stu_id, Stu_Name, Stu_M
arks, Stu_Age) VALUES (104, Anmol, 89, 19);
UPDATE DML Command
• UPDATE is another most important data
manipulation command in Structured Query
Language, which allows users to update or
modify the existing data in database tables.
• Syntax of UPDATE Command
• UPDATE Table_name SET [column_name1= val
ue_1, ….., column_nameN = value_N] WHERE
CONDITION;
• Eg.
UPDATE Product SET Product_Price = 80 WHE
RE Product_Id = 'P102' ;
DELETE DML Command

• DELETE is a DML command which allows SQL users to


remove single or multiple existing records from the
database tables.
• This command of Data Manipulation Language does
not delete the stored data permanently from the
database. We use the WHERE clause with the DELETE
command to select specific rows from the table.
• Syntax of DELETE Command
• DELETE FROM Table_Name WHERE condition;
• Eg. DELETE FROM Student WHERE Stu_Marks > 70 ;
DCL
• DCL is an abbreviation for Data Control
Language in SQL. It is used to provide different
users access to the stored data. It enables the
data administrator to grant or revoke the
required access to act as the database. When
DCL commands are implemented in the
database, there is no feature to perform a
rollback. The administrator must implement
the other DCL command to reverse the action.
Types of DCL Commands in SQL

• Two types of DCL commands can be used by


the user in SQL. These commands are useful,
especially when several users access the
database. It enables the administrator to
manage access control. The two types of DCL
commands are as follows:
• GRANT
• REVOKE
Implementing GRANT Statement

• Consider a scenario where you are the


database administrator, and a student table is
in the database. Suppose you want a specific
user Aman to only SELECT (read)/ retrieve the
data from the student table. Then you can use
GRANT in the below GRANT statement.
• GRANT SELECT ON student TO Aman;
Implementing REVOKE Command

• Consider a scenario where the user is the database


administrator. In the above implementation of the GRANT
command, the user Aman was provided permission to
implement a SELECT query on the student table that allowed
Aman to read or retrieve the data from the table. Due to
certain circumstances, the administrator wants to revoke the
abovementioned permission. To do so, the administrator can
implement the below REVOKE statement:
• REVOKE SELECT ON student FROM Aman;
• This will stop the user Aman from implementing the SELECT
query on the student table. The user may be able to
implement other queries in the database.
TCL Commands in SQL

• In SQL, TCL stands for Transaction control language.


• A single unit of work in a database is formed after
the consecutive execution of commands is known as
a transaction.
• There are certain commands present in SQL known
as TCL commands that help the user manage the
transactions that take place in a database.
• COMMIT. ROLLBACK and SAVEPOINT are the most
commonly used TCL commands in SQL.
1. COMMIT

• COMMIT command in SQL is used to save all


the transaction-related changes permanently
to the disk. Whenever DDL commands such as
INSERT, UPDATE and DELETE are used, the
changes made by these commands are
permanent only after closing the current
session.
2.SAVEPOINT
• We can divide the database operations into parts. For
example, we can consider all the insert related queries
that we will execute consecutively as one part of the
transaction and the delete command as the other part of
the transaction. Using the SAVEPOINT command in SQL,
we can save these different parts of the same transaction
using different names. For example, we can save all the
insert related queries with the savepoint named INS. To
save all the insert related queries in one savepoint, we
have to execute the SAVEPOINT query followed by the
savepoint name after finishing the insert command
execution.
• Eg. SAVEPOINT savepoint_name;
• ROLLBACK
• While carrying a transaction, we must create savepoints to
save different parts of the transaction. According to the
user's changing requirements, he/she can roll back the
transaction to different savepoints. Consider a scenario: We
have initiated a transaction followed by the table creation
and record insertion into the table. After inserting records,
we have created a savepoint INS. Then we executed a delete
query, but later we thought that mistakenly we had
removed the useful record. Therefore in such situations, we
have an option of rolling back our transaction. In this case,
we have to roll back our transaction using
the ROLLBACK command to the savepoint INS, which we
have created before executing the DELETE query.
• Syntax:
• ROLLBACK TO savepoint_name;
• mysql> USE school;
Boyce Codd normal form (BCNF)
BCNF is the advance version of 3NF. It is stricter than 3NF.
A table is in BCNF if every functional dependency X → Y, X is the super
key of the table.
For BCNF, the table should be in 3NF, and for every FD, LHS is super
key.
Example: Let's assume there is a company where employees work in
more than one department.
EMPLOYEE table:
EMP_ID EMP_COUN EMP_DE DEPT_TY EMP_DEPT
TRY PT PE _NO
264 India Designing D394 283
264 India Testing D394 300
364 UK Stores D283 232
Developi
364 UK D283 549
ng
• In the above table Functional dependencies
are as follows:
• EMP_ID → EMP_COUNTRY
• EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
• Candidate key: {EMP-ID, EMP-DEPT}
• The table is not in BCNF because neither
EMP_DEPT nor EMP_ID alone are keys.
• To convert the given table into BCNF, we
decompose it into three tables:
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY

264 India

264 India
EMP_DEPT table:
EMP_DEPT DEPT_TYPE EMP_DEPT_NO

Designing D394 283

Testing D394 300

Stores D283 232

Developing D283 549


EMP_DEPT_MAPPING table:

EMP_ID EMP_DEPT

D394 283

D394 300

D283 232

D283 549
Functional dependencies:

• EMP_ID → EMP_COUNTRY
• EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
• Candidate keys:
• For the first table: EMP_ID
For the second table: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}
• Now, this is in BCNF because left side part of both the
functional dependencies is a key.

You might also like