0% found this document useful (0 votes)
116 views29 pages

Implicit Data Type Conversion: Query-1

Oracle can implicitly convert data types like VARCHAR2, CHAR, NUMBER and DATE during operations like comparisons. For example, it will treat the strings '15000' and 15000 as the same NUMBER value. SQL conversion functions like TO_CHAR, TO_NUMBER and TO_DATE allow explicit conversions between character, numeric and date data types. TO_CHAR formats values to strings using format models. TO_NUMBER converts strings to numbers. TO_DATE converts character strings to dates using format models that specify elements like year, month, day etc. Scalar and aggregate functions operate on single or multiple rows respectively and are used to manipulate and retrieve data in SQL Server.

Uploaded by

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

Implicit Data Type Conversion: Query-1

Oracle can implicitly convert data types like VARCHAR2, CHAR, NUMBER and DATE during operations like comparisons. For example, it will treat the strings '15000' and 15000 as the same NUMBER value. SQL conversion functions like TO_CHAR, TO_NUMBER and TO_DATE allow explicit conversions between character, numeric and date data types. TO_CHAR formats values to strings using format models. TO_NUMBER converts strings to numbers. TO_DATE converts character strings to dates using format models that specify elements like year, month, day etc. Scalar and aggregate functions operate on single or multiple rows respectively and are used to manipulate and retrieve data in SQL Server.

Uploaded by

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

IMPLICIT DATA TYPE CONVERSION

 A VARCHAR2 or CHAR value can be implicitly converted to NUMBER or


DATE type value by Oracle.
 Similarly, a NUMBER or DATA type value can be automatically converted to
character data by Oracle server.
 Note that the impicit interconversion happens only when the character
represents the a valid number or date type value respectively.
For example, examine the below SELECT queries. Both the queries will give the same
result because Oracle internally treats 15000 and '15000' as same.
Query-1
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > 15000;

Query-2
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > '15000';
EXPLICIT DATA TYPE CONVERSION
SQL Conversion functions are single row functions which are capable of typecasting
column value, literal or an expression.
TO_CHAR, TO_NUMBER and TO_DATE are the three functions which perform
cross modification of data types.
TO_CHAR function
TO_CHAR function is used to typecast a numeric or date input to character type with
a format model (optional).
Syntax
TO_CHAR(number1, [format], [nls_parameter])

 For number to character conversion, nls parameters can be used to specify


decimal characters, group separator, local currency model, or international
currency model.
 It is an optional specification - if not available, session level nls settings will be
used. For date to character conversion, the nls parameter can be used to specify
the day and month names, as applicable.
 Format models are case sensitive and must be enclosed within single quotes.

The query format the HIRE_DATE and SALARY columns of EMPLOYEES table
using TO_CHAR function.
SELECT first_name,
TO_CHAR (hire_date, 'MONTH DD, YYYY') HIRE_DATE,
TO_CHAR (salary, '$99999.99') Salary
FROM employees
WHERE rownum < 5;

FIRST_NAME HIRE_DATE SALARY


-------------------- ------------------ ----------
Steven JUNE 17, 2003 $24000.00
Neena SEPTEMBER 21, 2005 $17000.00
Lex JANUARY 13, 2001 $17000.00
Alexander JANUARY 03, 2006 $9000.00
The first TO_CHAR is used to convert the hire date to the date format MONTH DD,
YYYY i.e. month spelled out and padded with spaces, followed by the two-digit day
of the month, and then the four-digit year. If you prefer displaying the month name in
mixed case (that is, "December"), simply use this case in the format argument: ('Month
DD, YYYY').
The query format the HIRE_DATE and SALARY columns of EMPLOYEES table
using TO_CHAR function.
SELECT first_name,
TO_CHAR (hire_date, 'MONTH DD, YYYY') HIRE_DATE,
TO_CHAR (salary, '$99999.99') Salary
FROM employees
WHERE rownum < 5;

FIRST_NAME HIRE_DATE SALARY


-------------------- ------------------ ----------
Steven JUNE 17, 2003 $24000.00
Neena SEPTEMBER 21, 2005 $17000.00
Lex JANUARY 13, 2001 $17000.00
Alexander JANUARY 03, 2006 $9000.00

The first TO_CHAR is used to convert the hire date to the date format MONTH DD,
YYYY i.e. month spelled out and padded with spaces, followed by the two-digit day
of the month, and then the four-digit year. If you prefer displaying the month name in
mixed case (that is, "December"), simply use this case in the format argument: ('Month
DD, YYYY').
The second TO_CHAR function in Figure 10-39 is used to format the SALARY to
display the currency sign and two decimal positions.
Oracle offers comprehensive set of format models. The below table shows the list of
format models which can be used to typecast date and number values as character using
TO_CHAR.

Format Description
Model
,(comma) It returns a comma in the specified position. You can specify
multiple commas in a number format model. Restrictions:A
comma element cannot begin a number format model. A comma
cannot appear to the right of a decimal character or period in a
number format model.

.(period) Returns a decimal point, which is a period (.) in the specified


position. Restriction: You can specify only one period in a number
format model

$ Returns value with a leading dollar sign

0 Returns leading zeros. Returns trailing zeros.

9 Returns value with the specified number of digits with a leading


space if positive or with a leading minus if negative. Leading zeros
are blank, except for a zero value, which returns a zero for the
integer part of the fixed-point number.

TO_NUMBER function
The TO_NUMBER function converts a character value to a numeric datatype. If the
string being converted contains nonnumeric characters, the function returns an error.
Syntax
TO_NUMBER (string1, [format], [nls_parameter])

The below table shows the list of format models which can be used to typecast
character values as number using TO_NUMBER.

Format Description
Model

CC Century

SCC Century BC prefixed with -

YYYY Year with 4 numbers


SYYY Year BC prefixed with -

YY Year with 2 numbers

YEAR Year in characters

SYEAR Year in characters, BC prefixed with -

Q Quarter in numbers (1,2,3,4)

MM Month of year 01, 02...12

MONTH Month in characters (i.e. January)

MON JAN, FEB

WW Week number (i.e. 1)

W Week number of the month (i.e. 5)

DDD Day of year in numbers (i.e. 365)

DD Day of the month in numbers (i.e. 28)

D Day of week in numbers(i.e. 7)

DAY Day of the week in characters (i.e. Monday)

DY Day of the week in short character description (i.e. SUN)

J Julian Day (number of days since January 1 4713 BC, where


January 1 4713 BC is 1 in Oracle)
HH,H12 Hour number of the day (1-12)

HH24 Hour number of the day with 24Hours notation (0-23)

AM, PM AM or PM

MI, SS Number of minutes and seconds (i.e. 59) ,

SSSSS Number of seconds this day.

SELECT TO_NUMBER('1210.73', '9999.99')


FROM DUAL;

TO_NUMBER('1210.73','9999.9')
------------------------------
1210.7

TO_DATE function
The function takes character values as input and returns formatted date equivalent of
the same. The TO_DATE function allows users to enter a date in any format, and then
it converts the entry into the default format used by Oracle 11g.
Syntax:
TO_DATE( string1, [ format_mask ], [ nls_language ] )

A format_mask argument consists of a series of elements representing exactly what


the data should look like and must be entered in single quotation marks.

Format Description
Model

YEAR Year, spelled out

YYYY 4-digit year


YYY,YY,Y Last 3, 2, or 1 digit(s) of year.

IYY,IY,I Last 3, 2, or 1 digit(s) of ISO year.

IYYY 4-digit year based on the ISO standard

RRRR Accepts a 2-digit year and returns a 4-digit year.

Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

MONTH Name of month, padded with blanks to length of 9 characters.

RM Roman numeral month (I-XII; JAN = I).

WW Week of year (1-53) where week 1 starts on the first day of the
year and continues to the seventh day of the year.

W Week of month (1-5) where week 1 starts on the first day of the
month and ends on the seventh.

IW Week of year (1-52 or 1-53) based on the ISO standard.

D Day of week (1-7).

DAY Name of day.

DD Day of month (1-31).

DDD Day of year (1-366).


DY Abbreviated name of day.

J Julian day; the number of days since January 1, 4712 BC.

HH12 Hour of day (1-12).

HH24 Hour of day (0-23).

MI,SS Minute (0-59).

SSSSS Seconds past midnight (0-86399).

AM,PM Meridian indicator

AD,BC AD, BC indicator


TYPES OF SQL SERVER FUNCTIONS:

 System Defined Functions

 User Defined Functions

And in system defined function we have 2 types of functions.

 Scalar Functions

 Aggregate Functions

Scalar Functions: Which operators on single value and returns single value, below is
the list of some scale functions used in sql server.

Scalar Function Description

round (9.56785) This will round the give number to 3 places of decimal, 9.567

rand (8) This will generate random numbers between 0 and 0.8
rand( ) This will generate random number between 0 and 1

upper (‘sql’) This will return upper case of given string, ‘SQL’

lower (‘SQL’) This will return lower case of given string, ‘sql’

abs (-20.75) This will return absolute number of a given number, 20.75

Convert (int, 20.56) This will convert given float value to integer, 20

ltrim (‘ sql’) This will remove the spaces from left hand side, ‘sql’

rtrim (‘sql ‘) This will remove the spaces from right hand side, ‘sql’

Substr () This will extract characters from a text field

ASCII (char_Exp) This will return ASCII code of the given character expression
Aggregate Functions: Aggregates the values and return a single value, below is the list
of some aggregate values in sql server.

Aggregate Function Description

Min () This will return Minimum value

Max() This will return Maximum value

Avg () This will return Average value

Count () This will return number of counts

Sum () This will return total sum of numeric value


SYNONYMS
A synonym is an alternative name for objects such as tables, views, sequences, stored
procedures, and other database objects.
You generally use synonyms when you are granting access to an object from another
schema and you don't want the users to have to worry about knowing which schema
owns the object.
Create Synonym (or Replace)
You may wish to create a synonym so that users do not have to prefix the table name
with the schema name when using the table in a query.
Syntax
The syntax to create a synonym in Oracle is:

CREATE [OR REPLACE] [PUBLIC] SYNONYM [schema .] synonym_name


FOR [schema .] object_name [@ dblink];

OR REPLACE
Allows you to recreate the synonym (if it already exists) without having to
issue a DROP synonym command.
PUBLIC
It means that the synonym is a public synonym and is accessible to all users.
Remember though that the user must first have the appropriate privileges to the
object to use the synonym.
schema
The appropriate schema. If this phrase is omitted, Oracle assumes that you are
referring to your own schema.
object_name
The name of the object for which you are creating the synonym. It can be one
of the following:
 table
 view
 sequence
 stored procedure
 function
 package
 materialized view
 java class schema object
 user-defined object
 synonym
Example
Let's look at an example of how to create a synonym in Oracle.
For example:

CREATE PUBLIC SYNONYM suppliers


FOR app.suppliers;

This first CREATE SYNONYM example demonstrates how to create a synonym


called suppliers. Now, users of other schemas can reference the table
called suppliers without having to prefix the table name with the schema named app.
For example:

SELECT *
FROM suppliers;

If this synonym already existed and you wanted to redefine it, you could always use
the OR REPLACE phrase as follows:

CREATE OR REPLACE PUBLIC SYNONYM suppliers


FOR app.suppliers;

Drop synonym
Once a synonym has been created in Oracle, you might at some point need to drop the
synonym.
Syntax
The syntax to drop a synonym in Oracle is:

DROP [PUBLIC] SYNONYM [schema .] synonym_name [force];

PUBLIC
Allows you to drop a public synonym. If you have specified PUBLIC, then you
don't specify a schema.
force
It will force Oracle to drop the synonym even if it has dependencies. It is
probably not a good idea to use force as it can cause invalidation of Oracle
objects.
Example
Let's look at an example of how to drop a synonym in Oracle.
For example:

DROP PUBLIC SYNONYM suppliers;

This DROP statement would drop the synonym called suppliers that we defined
earlier
MODIFYING A VIEW
ALTER VIEW - change the definition of an existing view
ALTER VIEW uses the exact same syntax as CREATE VIEW and will succeed only
if the view already exists.
SYNTAX:
ALTER VIEW view-name [(column-commalist)] AS query

EXAMPLE:
ALTER VIEW MyView (MyViewCol1,MyViewCol2,MyViewCol3) AS
SELECT TableCol1, TableCol2, TableCol3 FROM MyTable

DROP VIEW
The DROP VIEW command removes a view, but does not removes the underlying
tables or data.
SYNTAX :
DROP VIEW view-name
EXAMPLE :
DROP VIEW MyView
SUB QUERIES
A subquery is a SQL query nested inside a larger query.
 A subquery may occur in :
o - A SELECT clause
o - A FROM clause
o - A WHERE clause
 The subquery can be nested inside a SELECT, INSERT, UPDATE, or
DELETE statement or inside another subquery.
 A subquery is usually added within the WHERE Clause of another SQL
SELECT statement.
 You can use the comparison operators, such as >, <, or =. The comparison
operator can also be a multiple-row operator, such as IN, ANY, or ALL.
 A subquery is also called an inner query or inner select, while the statement
containing a subquery is also called an outer query or outer select.
 The inner query executes first before its parent query so that the results of an
inner query can be passed to the outer query.
You can use a subquery in a SELECT, INSERT, DELETE, or UPDATE statement to
perform the following tasks:
 Compare an expression to the result of the query.
 Determine if an expression is included in the results of the query.
 Check whether the query selects any rows.

SELECT a.studentid, a.name, b.total_marks


FROM student a, marks b
WHERE a.studentid = b.studentid AND b.total_marks >
(SELECT total_marks
FROM marks
WHERE studentid = 'V002');
Type of Subqueries

 Single row subquery : Returns zero or one row.


 Multiple row subquery : Returns one or more rows.
 Multiple column subqueries : Returns one or more columns.
 Correlated subqueries : Reference one or more columns in the outer SQL
statement. The subquery is known as a correlated subquery because the
subquery is related to the outer SQL statement.
 Nested subqueries : Subqueries are placed within another subquery.
NORMALIZATION
Normalization is a set of rules/guidelines/technique that is used while designing a
database. These rules help to remove all the anomalies and distribute the data among
different related tables and query them efficiently and effectively. It removes all the
duplication issues and incorrect data issues, helping to have a well-designed
database. Normalization is divided into following normal forms:
1. First Normal Form (1NF)
2. Second Normal Form (2NF)
3. Third Normal Form (3NF)
4. Boyce-Codd Normal Form (3.5NF)
5. Forth Normal Form (4NF)
6. Fifth Normal Form (5NF)

First Normal Form (1NF)


A table is said to be in First Normal Form (1NF) if and only if each attribute of the
relation is atomic. That is,
 Each row in a table should be identified by primary key (a unique column
value or group of unique column values)
 No rows of data should have repeating group of column values.
A relation is in first normal form if every attribute in that relation is singled
valued attribute
Let's consider the STUDENT table with his ID, Name address and 2 subjects that he
has opted for.

 Look at Chris entry. He has only subject. Hence Subject2 for him is NULL.
Here storage space for second entry is simply wasted.
 In the case of Joseph, he has two subjects, Mathematics and Physics, both the
columns have values. Imagine if he opts for third subject? There is no
column for his third entry. In this case, whole table needs to be altered,
which is not good at this stage. Once database is designed, it should be a
perfect one. We should not be modifying it as we start adding/updating data.
 One of the requirements of 1NF is, each table should have primary key. This
key in the table makes each record unique. In our example we have it
already- STUDENT_ID.
 Here, SUBJECT1 and SUBJECT2 are same set of columns, i.e.; it has same
kind of information stored - Subject, which is a violation of first rule of 1NF.
As it states, there should not be any repeating columns. We have to remove
such columns. But think how?
In order to have STUDENT in 1NF, we have to remove multiple SUBJECT columns
from STUDENT table. Instead, create only one SUBJECT column, and for each
STUDENT enters as many rows as SUBJECT he has. After making this change, the
above table will change as follows:

Now STUDENT_ID alone cannot be a primary key, because it does not uniquely
determines each record in the table. If we want to records for Joseph, and we query by
his ID,100, gives us two records. Hence Student_ID is no more a primary key. When
we observe the data in the table, all the four field uniquely determines each record.
Hence all four fields together considered as primary key.
Thus, the above table is in 1NF form.

Second Normal Form (2NF)


A relation is said to be in a second normal form if and only if,
 it's in first normal form
 Every non-key attributes are identified by the use of primary key
A relation is in 2NF iff it has No Partial Dependency
 All subset of data, which applies to have multiple rows in a table must be
removed and placed in a new table. And this new table and the parent table
should be related by the use of foreign key.
In the 1NF STUDENT table above, Joseph and Allen have multiple rows because of
their SUBJECTS. Although it is in 1NF form, it wastes storage space by repeating
whole of their information - name and address in each row. In addition, Student ID
alone is strong enough to be a primary key. If we make Student_ID as primary, all other
attributes in the table cannot be uniquely identified. This is because of multiple rows
exists for single ID. Hence it does not satisfy second condition of 2NF.
So what we can do here is, apply the third condition of 2NF. Remove Subject from the
STUDENT table and create a separate table for it. So the two tables are - STUDENT
and SUBJECT. Now the STUDENT table will have only STUDENT information -
STUDENT_ID, STUDENT_NAME and ADDRESS. New SUBJECT table will have
STUDENT_ID and SUBJECT_NAME.

Now there is no repeating group of columns in STUDENT table and STUDENT_ID is


the primary key of STUDENT table. It uniquely identifies the Student name and address
which are non key attributes of this table. Hence it satisfies both 1NF and 2NF.
In the new SUBJECT table, Subject names opted by each student. Since same student
cannot opt for same subject multiple times in academic year, there will not be any
duplicity of data. But Student_ID alone is not unique; hence it cannot be a primary key.
Both Student_ID and Subject_Name is unique in this table. Hence both of them together
become a primary key. Hence SUBJECT table satisfies 1NF.
There is no non-key attributes in SUBJECT table. Hence we cannot verify second
condition of 2NF. According to the third condition of 2NF, we have removed the data
which is forming multiple rows and put those details in new table. It also states that
there should be relationship between the original table and new table by using foreign
key constraint. In the SUBJECT table, STUDENT_ID is derived from STUDENT
table. In this table, STUDENT _ID is part of primary key as well as it is a foreign key.
Hence we can easily relate both STUDENT and SUBJECT table by using
STUDENT_ID. Hence satisfies the third condition of 2NF.
3. Third Normal Form –
A relation is in third normal form, if there is no transitive dependency for non-prime
attributes and it is in second normal form.
A relation is in 3NF iff at least one of the following condition holds in every non-
trivial function dependency X –> Y
1. X is a super key.
2. Y is a prime attribute (each element of Y is part of some candidate key).

Transitive dependency – If A->B and B->C are two FDs then A->C is called
transitive dependency.
 Example 1 – In relation STUDENT given in Table 4,
FD set: {STUD_NO -> STUD_NAME, STUD_NO -> STUD_STATE,
STUD_STATE -> STUD_COUNTRY, STUD_NO -> STUD_AGE,
STUD_STATE -> STUD_COUNTRY}
Candidate Key: {STUD_NO}
For this relation in table 4, STUD_NO -> STUD_STATE and STUD_STATE ->
STUD_COUNTRY are true. So STUD_COUNTRY is transitively dependent on
STUD_NO. It violates third normal form. To convert it in third normal form, we
will decompose the relation
STUDENT (STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE,
STUD_COUNTRY_STUD_AGE) as:

1. STUDENT (STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE,


STUD_AGE)

2. STATE_COUNTRY (STATE, COUNTRY)


Boyce-Codd Normal Form (BCNF)
Boyce-Codd Normal Form or BCNF is an extension to the third normal form, and is
also known as 3.5 Normal Form.
Rules for BCNF
For a table to satisfy the Boyce-Codd Normal Form, it should satisfy the following
two conditions:
1. It should be in the Third Normal Form.
2. And, for any dependency A → B, A should be a super key.
The second point sounds a bit tricky, right? In simple words, it means, that for a
dependency A → B, A cannot be a non-prime attribute, if B is a prime attribute.

Time for an Example


Below we have a college enrolment table with
columns student_id, subject and professor.

student_id subject professor

101 Java P.Java

101 C++ P.Cpp

102 Java P.Java2


103 C# P.Chash

104 Java P.Java

As you can see, we have also added some sample data to the table.
In the table above:
 One student can enrol for multiple subjects. For example, student
with student_id 101, has opted for subjects - Java & C++
 For each subject, a professor is assigned to the student.
 And, there can be multiple professors teaching one subject like we have for Java.
What do you think should be the Primary Key?
Well, in the table above student_id, subject together form the primary key, because
using student_id and subject, we can find all the columns of the table.
One more important point to note here is, one professor teaches only one subject, but
one subject may have two different professors.
Hence, there is a dependency between subject and professor here,
where subject depends on the professor name.
This table satisfies the 1st Normal form because all the values are atomic, column
names are unique and all the values stored in a particular column are of same domain.
This table also satisfies the 2nd Normal Form as their is no Partial Dependency.
And, there is no Transitive Dependency, hence the table also satisfies the 3rd
Normal Form.
But this table is not in Boyce-Codd Normal Form.

Why this table is not in BCNF?


In the table above, student_id, subject form primary key, which means subject column
is a prime attribute.
But, there is one more dependency, professor → subject.
And while subject is a prime attribute, professor is a non-prime attribute, which is
not allowed by BCNF.

How to satisfy BCNF?


To make this relation(table) satisfy BCNF, we will decompose this table into two
tables, student table and professor table.
Below we have the structure for both the tables.
Student Table

student_id p_id

101 1

101 2

and so on...

And, Professor Table

p_id professor subject

1 P.Java Java

2 P.Cpp C++

and so on...

And now, this relation satisfy Boyce-Codd Normal Form. In the next tutorial we will
learn about the Fourth Normal Form.

A more Generic Explanation


In the picture below, we have tried to explain BCNF in terms of relations.
Fourth Normal Form (4NF)
Fourth Normal Form comes into picture when Multi-valued Dependency occur in
any relation. In this tutorial we will learn about Multi-valued Dependency, how to
remove it and how to make any table satisfy the fourth normal form.
Rules for 4th Normal Form
For a table to satisfy the Fourth Normal Form, it should satisfy the following two
conditions:
1. It should be in the Boyce-Codd Normal Form.
2. And, the table should not have any Multi-valued Dependency.
Let's try to understand what multi-valued dependency is in the next section.

What is Multi-valued Dependency?


A table is said to have multi-valued dependency, if the following conditions are true,
1. For a dependency A → B, if for a single value of A, multiple value of B exists,
then the table may have multi-valued dependency.
2. Also, a table should have at-least 3 columns for it to have a multi-valued
dependency.
3. And, for a relation R(A,B,C), if there is a multi-valued dependency between, A
and B, then B and C should be independent of each other.
If all these conditions are true for any relation(table), it is said to have multi-valued
dependency.
Time for an Example
Below we have a college enrolment table with columns s_id, course and hobby.

s_id course hobby

1 Science Cricket

1 Maths Hockey

2 C# Cricket

2 Php Hockey

As you can see in the table above, student with s_id 1 has opted for two
courses, Science and Maths, and has two hobbies, Cricket and Hockey.
You must be thinking what problem this can lead to, right?
Well the two records for student with s_id 1, will give rise to two more records, as
shown below, because for one student, two hobbies exists, hence along with both the
courses, these hobbies should be specified.

s_id course hobby

1 Science Cricket

1 Maths Hockey

1 Science Hockey

1 Maths Cricket

And, in the table above, there is no relationship between the


columns course and hobby. They are independent of each other.
So there is multi-value dependency, which leads to un-necessary repetition of data and
other anomalies as well.
How to satisfy 4th Normal Form?
To make the above relation satify the 4th normal form, we can decompose the table
into 2 tables.
CourseOpted Table

s_id course

1 Science

1 Maths

2 C#

2 Php

And, Hobbies Table,

s_id hobby

1 Cricket

1 Hockey

2 Cricket

2 Hockey

Now this relation satisfies the fourth normal form.


Compare Normal Forms / 1NF vs 2NF vs 3NF vs BCNF / Differentiate
between normal forms

1 NF 2 NF 3 NF BCNF

Properties to All the


At the first The table is in For all the
hold attributes of place the table 2NF, Functional
the relation
is in 1NF, There is no Dependencies
are atomicAll the non- Functional (FDs) hold in
(indivisible key attributes Dependency the relation R,
into of the table are such that both if the FD is
meaningful fully Left Hand non-trivial then
sub parts), functionally Side and Right the determinant
Every dependent on Hand Side (LHS of FD) of
attribute the Primary attributes of that FD should
contains key of the the FD are be a Super key
single value table. non-key
(per record). attributes. In
other words,
no transitive
dependency is
allowed

Achievability Always Always Always Not always


achievable achievable achievable

Lossless Join Always Always Always Sometimes not


Decomposition achievable achievable achievable achievable

Dependency N/A Possible Either lossless


Preserving join or
Decomposition dependency
preserving
decomposition
is possible. Not
both.

Anomalies May allow May allow May allow Always


some some some eliminates
anomalies anomalies anomalies anomalies
What is Eliminate Eliminate Eliminate Eliminate
eliminated? repeating redundant data columns not multiple
groups dependent on candidate keys
key

Identification Not necessary Must Must Must


of Functional
Dependencies

Attribute Should be Should be Should be Should be


Domain atomic atomic atomic atomic

Handling of Does not Handles Handles Handles


Update handle.
Anomalies

Composite Allowed Allowed (if no Allowed Not allowed


Primary Key partial
dependency
exists)

Partial key Permitted Not permitted Not permitted Not permitted


dependencies
(if AB → C,
and if C can be
fully
determined by
either A or B,
then this
dependency is
partial key
dependencies)

Transitive Can be Can be Cannot be Cannot be


dependencies permitted permitted permitted permitted
(if A → B, and
B→C then
A → C)

Overview It is about It is about the It is about the It is about


shape of a relationship relationship determinant
record type between key between key should be a
superkey.
and non-key and non-key
fields fields

You might also like