Sie sind auf Seite 1von 17

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
Table is now in black
sweatshirt first normal form
25.00 1.25
Second Normal Form (2NF)
• All non-key fields depend on all components of the primary key.
• Guaranteed when primary key is a single field.

• Table is not in second normal form because:


price and tax depend on item, but not color
Tables are now in second normal form

item color item price Tax


T-shirt red T-shirt 12.00 0.60
T-shirt blue
polo 12.00 0.60
polo red
sweatshirt 25.00 1.25
polo yellow
sweatshirt blue
sweatshirt black
Third Normal Form (3NF)
• No non-key field depends upon another
• All non-key fields depend only on the primary key.

Tables are not in third normal form because:


• tax depends on price, not item
EXAMPLE
Tables are now in third normal form

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:

• function (arg1, ..., argN) OVER ([partition_by_clause] [order_by_clause


[windowing_clause]])
• The OVER keyword
• partition_by_clause: Optional. Analogous to GROUP BY
• order_by_clause: Mandatory for Ranking and Windowing functions.
• windowing_clause: Optional. Should always be preceded by ORDER BY clause
Examples:
• Rank -If 2 rows have the same value (N), they receive the same sequential value
of N, and the next value N+1 will be skipped and the N+2 value will be given to
the next record.
• Dense_rank - If 2 rows have the same value (N), it does not skip position N+1.
• Row_number -running serial number to the partition records based on the order
by clause.
• Lag-used to access data from previous row
• Lead-used to return row from further down from result set.
Rank
SELECT empno,
deptno,
sal,
RANK() OVER (PARTITION BY deptno ORDER BY sal) AS myrank
FROM emp;

EMPNO DEPTNO SAL MYRANK


---------- ---------- ---------- ----------
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()

SELECT empno,deptno, sal,


DENSE_RANK() OVER (PARTITION BY deptno ORDER BY sal) AS myrank
FROM emp;
EMPNO DEPTNO SAL MYRANK
---------- ---------- ---------- ----------
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
---------- ---------- ---------- --------- ---------- ----------
10 7934 MILLER CLERK 1300 2450
10 7782 CLARK MANAGER 2450 5000
10 7839 KING PRESIDENT 5000 0
20 7369 SMITH CLERK 800 1100
20 7876 ADAMS CLERK 1100 2975
20 7566 JONES MANAGER 2975 3000
20 7788 SCOTT ANALYST 3000 3000
20 7902 FORD ANALYST 3000 0
30 7900 JAMES CLERK 950 1250
30 7654 MARTIN SALESMAN 1250 1250
30 7521 WARD SALESMAN 1250 1500
30 7844 TURNER SALESMAN 1500 1600
30 7499 ALLEN SALESMAN 1600 2850
30 7698 BLAKE MANAGER 2850 0

Das könnte Ihnen auch gefallen