Sunday, May 13, 2012

PL SQL- Primary,Unique,Foreign Key

Primary key - Primary key means main key
def:- A primary key is one which uniquely identifies a row
of a table. this key does not allow null values and also
does not allow duplicate values. for ex,
     empno   empname   salary
       1      firoz     35000
       2      basha     34000
       3      chintoo   40000

it will not the values as follows:
1      firoz      35000
1      basha      34000
       chintoo    35000

Unique key -  single and main key
            A unique is one which uniquely identifies a row
of a table, but there is a difference like it will not
allow duplicate values and it will any number of  allow
null values(In oracle).
it allows only a single null value(In sql server 2000)

Both will function in a similar way but a slight difference
will be there. So, decalaring it as a primary key is the
best one.

foreign key - a foreign key is one which will refer to a
primary key of another table

for ex,
       emp_table                         dept_table
empno empname salary  deptno         deptno deptname

In the above relation, deptno is there in emp_table which
is a primary key of dept_table. that means, deptno is
refering the dept_table.

==============================================================
primary key- if a column is declared as priamry then there
wont be any repeated values in that column throughout that
table, as well as it wont take any null values and we can
use primary key only once in a table. but we can declare
combination of two columns as primary key.

primary key: primary creates a clustered index on the
column and it doesn't allow null values.

===============================================================
unique- if a column is declared as unique then again there
wont be any repeated values in that column, but this will
take null values and even we can declare more thatn one
unique column in a table.

unique key: unique key creates non clustered index by
default.it allows "one null value".

===============================================================
foreign key-if a column is declared with this key then we
have to use a reference ie we need to give the column which
will have the same values as this column and the table that
column belongs to.. this referced column of other table
will be the primary key of that table.

foreign key: A foreign key (FK) is a column or combination
of columns used to establish and enforce a link between the
data in two tables

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

No comments:

Post a Comment