Monday, 13 February 2012

PRIMARY KEY

    - It doesn't accept NULL values.
    - It doesn't allow duplicate values.
    - We can have only one primary key per table, but we can have combination of columns as a primary key.
        This kind of primary key is called as Composite Key.

    Syntax:
        column_name datatype [size] priamary key;
   
    Examples:   
   
    (i). Creating a Primary Key constraint.

        Column Level Primary Key declaration.
            create table emp
            ( empno number(4) primary key, .... );
       
        Table Level Priamary Key declaration.
            create table emp
            ( empno number(4), ename varchar2(20), ..... ,
                primary key (empno) ,.... );
       
        Using Alter
            create table emp
            ( empno number(4), ename varchar2(20), ..... );

            Alter table emp add (primary key (empno));

        Creating primary key with name.
            create table emp ( empno number(4) constraint pk_emp primary key(empno), ....);

    (ii). Dropping a Primary Key Constraint.
       
        Alter table emp drop primary key;


No comments:

Post a Comment