Wednesday, December 19, 2012

Normalization


What is Normalization ? Why should we use it?

Normalization is a database design technique which organizes tables in a manner that reduces redundancy and dependency of data.
It divides larger tables to smaller tables and link them using relationships.
The inventor of the relational model Edgar Codd proposed the theory of normalization with the introduction of FirstNormal Form and he continued to extend theory with Second and Third Normal Form. Later he joined with Raymond F. Boyce  to develop the theory of Boyce-Codd Normal Form

Theory of Normalization is still being developed further. For example there are discussions even on 6th Normal Form. But in most practical applications normalization achieves its best in 3rd Normal FormThe evolution of Normalization theories is illustrated below-

alt

Let’s learn Normalization with practical example -

Assume a video library maintains a database of movies rented out. Without any normalization all information is stored in one table as shown below.

alt
Table 1


Here you see Movies  Rented column has multiple values.
Now let’s move in to 1st Normal Form

1NF Rules


  • Each table cell should contain single value.
  • Each record needs to be unique.

The above table in 1NF-
alt
Table 1 : In 1NF Form


Before we proceed lets understand a few things --

What is a KEY ?


A KEY  is a value used to uniquely identify a record in a table. A KEY could be a single column or combination of multiple columns

Note: Columns in a table that are NOT used to uniquely identify a record are called non-key columns.

What is a primary Key?


alt


A primary is a single column values used to uniquely identify a database record.
It has following attributes
  • A primary key cannot be NULL
  • A primary key value must be unique
  • The primary key values can not be changed
  • The primary key must be given a value when a new record is inserted.

What is a composite Key?

A composite key is a primary key composed of multiple columns used to identify a record uniquely

In our database , we have two people with the same name Robert Phil but they live at different places.
alt
Hence we require both Full Name and Address to uniquely identify a record. This is a composite key.
Let’s move into 2NF

2NF Rules

  • Rule 1- Be in 1NF
  • Rule 2- Single Column Primary Key


It is clear that we can’t move forward to make our simple database in 2nd Normalization form unless we partition the table above.
alt
Table 1



alt
Table 2



We have divided our 1NF table into two tables viz. Table 1 and Table2. Table 1 contains member information. Table 2 contains information on movies rented.
We have introduced a new column called Membership_id which is the primary key for table 1. Records can be uniquely identified in Table 1 using membership id


Introducing Foreign Key!


In Table 2, Membership_ID is the foreign Key
alt


altForeign Key references primary key of another Table!It helps connect your Tables


  • A foreign key can have a different name from its primary key
  • It ensures rows in one table have corresponding rows in another
  • Unlike Primary key they do not have to be unique. Most often they aren’t
  • Foreign keys can be null even though primary keys can not


alt

Why do you need a foreign key ?


Suppose an idiot inserts a record in Table B such as
You will only be able to insert values into your foreign key that exist in the unique key in the parent table. This helps in referential integrity. 
alt

The above problem can be overcome by declaring membership id  from Table2  as foreign key of membership id  from Table1 
Now , if somebody tries to insert a value in the membership id  field that does not exist in the parent table , an error will be shown!

What is a transitive functional dependencies?

A transitive functional dependency is when changing a non-key column , might cause any of the other non-key columns to change
Consider the table 1. Changing the non-key column Full Name , may change Salutation.
alt
Let’s move ito 3NF

3NF Rules

  • Rule 1- Be in 2NF
  • Rule 2- Has no transitive functional dependencies
To move our 2NF table into 3NF we again need to need divide our table.

alt
TABLE 1



alt
Table 2



alt
Table 3


We have again divided our tables and created a new table which stores Salutations. 
There are no transitive functional dependencies and hence our table is in 3NF
In Table 3 Salutation ID is primary key and in Table 1 Salutation ID is foreign to primary key in Table 3

Now our little example is in a level that cannot further be decomposed to attain higher forms of normalization. In fact it is already in higher normalization forms. Separate efforts for moving in to next levels of normalization are normally needed in complex databases.  However we will be discussing about next levels of normalizations in brief in the following.


Boyce-Codd Normal Form (BCNF)


Even when a database is in 3rd Normal Form, still there would be anomalies resulted if it has more than one Candidate Key.  
Sometimes is BCNF is also referred as 3.5 Normal Form.


4th  Normal Form

If no database table instance contains two or more, independent and multivalued data describing the relevant entity , then it is in 4th Normal Form.

5th  Normal Form

A table is in 5th Normal Form only if it is in 4NF and it cannot be decomposed in to any number of smaller tables without loss of data.

6th  Normal Form

6th Normal Form is not standardized yet however it is being discussed by database experts for some time. Hopefully we would have clear standardized definition for 6th Normal Form in near future.

That’s all to Normalization!!!
alt

Summary

  • Database designing is critical to the successful implementation of a database management system that meets the data requirements of an enterprise system.
  • Normalization helps produce database systems that are cost effective, cost effective and have better security models.
  • Functional dependencies are a very important component of the normalization process
  • Most database systems are normalized up to the third normal form.
  • A primary uniquely identifies are record in a Table and cannot be null
  • A foreign key helps connect table and references a primary key

Read more at http://www.guru99.com/database-normalization.html#zhPrzf565hhCetMp.99 

 and
http://www.youtube.com/watch?v=bCdm33l2vDA

Oracle - Foreign Keys

Oracle - Sub-Queries

Simple Oracle Functions


create or replace function test_fun(inval_1 in number, inval_2 in varchar2)
  return varchar2 is
begin
  return(inval_1 || ' - ' || inval_2);
end;


Execute the function

select test_fun(1,'input') as col1,
       test_fun(2,'output') as col2,
       test_fun(3,'inout') as col3,
       test_fun(3,4) as col4
from dual;
       

Output

COL1             COL2           COL3         COL4
1 - input        2 - output     3 - inout        3 - 4



Oracle Joins

Actually i am not having that much knowledge in joins.

i hope these all better to get an idea about joins

just Click this links and go through it..

Happy reading...

http://www.youtube.com/watch?v=pp9xDBeXwKo

http://db.grussell.org/section010.html#_Toc67114483

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

Friday, December 7, 2012

Email Validation



CREATE OR REPLACE FUNCTION check_email(l_user_name IN VARCHAR2)
  RETURN VARCHAR2 IS
  l_dot_pos    NUMBER;
  l_at_pos     NUMBER;
  l_str_length NUMBER;
BEGIN
  l_dot_pos    := instr(l_user_name, '.');
  l_at_pos     := instr(l_user_name, '@');
  l_str_length := length(l_user_name);
  IF ((l_dot_pos = 0) OR (l_at_pos = 0) OR (l_dot_pos = l_at_pos + 1) OR
     (l_at_pos = 1) OR (l_at_pos = l_str_length) OR
     (l_dot_pos = l_str_length)) THEN
    RETURN 'FAILURE';
  END IF;
  IF instr(substr(l_user_name, l_at_pos), '.') = 0 THEN
    RETURN 'FAILURE';
  END IF;
  RETURN 'SUCCESS';
END check_email;



=================================================================================




declare
  v_at  number;
  v_dot number;
begin
  v_at  := instr("field_name", '@');
  v_dot := instr("field_name", '.');
  if v_at = 0 or v_dot = 0 or v_at > v_dot then
 
    show_message('Invalid Email ID Format..,', 'E', true);
 
  end if;
end;