Professional Documents
Culture Documents
Database Normalization
Database Normalization
What is Normalization?
The process by which we efficiently organize data to achieve
these goals:
• Eliminating redundancy
• Ensuring data is stored in the correct table
• Eliminating need for restructuring database when data is added.
Five levels of normal form
• In order to achieve one level of normal form, each previous level
must be met
• Third normal form is sufficient for most typical database applications.
First Normal Form (1NF)
• There are no repeating or duplicate fields.
• Each cell contains only a single value.
• Each record is unique.
Identified by primary key
EXAMPLE
item colors price tax
T-shirt red, blue 12.00 0.60
polo red, yellow 12.00 0.60
T-shirt red, blue 12.00 0.60
sweatshirt blue, black 25.00 1.25
Table is not in first normal form because:
• Multiple items in color field
• Duplicate records / no primary key
item color price tax
T-shirt red 12.00 0.60
T-shirt blue 12.00 0.60
polo red 12.00 0.60
polo yellow 12.00 0.60
sweatshirt blue 25.00 1.25
sweatshirt black 25.00 1.25
Table is now in first normal form
Second Normal Form (2NF)
• All non-key fields depend on all components of the primary key.
• Guaranteed when primary key is a single field.
price tax
12.00 0.60
25.00 1.25
Analytic Functions
• Introduced in Oracle v 8i
• Also known as windowing functions
• Use “partition by” clause to group rows together
• The last set of operations performed in a query, except for the order
by clause.
• All joins, where, group by, and having clauses are completed before
the analytic functions are processed.
• Can only appear in the select list or order by clauses.
Syntax:
7934 10 1300 1
7782 10 2450 2
7839 10 5000 3
7369 20 800 1
7876 20 1100 2
7566 20 2975 3
7788 20 3000 4
7902 20 3000 4
7900 30 950 1
7654 30 1250 2
7521 30 1250 2
7844 30 1500 4
7499 30 1600 5
7698 30 2850 6
Dense_rank()
7934 10 1300 1
7782 10 2450 2
7839 10 5000 3
7369 20 800 1
7876 20 1100 2
7566 20 2975 3
7788 20 3000 4
7902 20 3000 4
7900 30 950 1
7654 30 1250 2
7521 30 1250 2
7844 30 1500 3
7499 30 1600 4
7698 30 2850 5
Row number()
SELECT *, ROW_NUMBER()
OVER(ORDER BY EmpName) AS
Row_Number
FROM Employee
Lag()
SELECT deptno,empno, ename, job, sal,
LAG(sal, 1, 0) OVER (PARTITION BY deptno ORDER BY sal) AS sal_prev
FROM emp;
DEPTNO EMPNO ENAME JOB SAL SAL_PREV
---------- ---------- ---------- --------- ---------- ----------
10 7934 MILLER CLERK 1300 0
10 7782 CLARK MANAGER 2450 1300
10 7839 KING PRESIDENT 5000 2450
20 7369 SMITH CLERK 800 0
20 7876 ADAMS CLERK 1100 800
20 7566 JONES MANAGER 2975 1100
20 7788 SCOTT ANALYST 3000 2975
20 7902 FORD ANALYST 3000 3000
30 7900 JAMES CLERK 950 0
30 7654 MARTIN SALESMAN 1250 950
30 7521 WARD SALESMAN 1250 1250
30 7844 TURNER SALESMAN 1500 1250
30 7499 ALLEN SALESMAN 1600 1500
30 7698 BLAKE MANAGER 2850 1600
Lead()
SELECT deptno,empno,ename,job,sal,
LEAD(sal, 1, 0) OVER (PARTITION BY deptno ORDER BY sal) AS sal_next
FROM emp;
DEPTNO EMPNO ENAME JOB SAL SAL_NEXT