SQL Primary key,Foreign Key ,Unique

Primary Key :-

  • Primary Key is very important and special key or contraint that a single attribute or column in a  table  possess .
  • Primary key value is used to identify individual record or tuple in a table .
  • Primary key value cannot be NULL 


Lets see the declaration of primary key during the creation of table   

     Create table Person (

                                       Pid          int  primary key,

                                       Name      varchar(30)

                                 );

 

 

Foreign Key :-

  •  Foreign key is another very important key or constraint that an attribute or columns in a table posses .
  •  Foreign key is also stated as reference key .
  •  Foreign key value in one table that refers to another record in other table.


Lets see the declaration of foreign key during the creation of table

 

           create table user (

                               user_id         int primary key,

                               username      varchar(30)

                            );





           create table vehicle (

                                 vehicle_id            int primary key,

                                 vehicle_name       varchar(30)

                               );


 

 

Unique :-

  •   The Unique key or constraint is an alternative secondary key .
  •   The attribute or column in a table with unique key specifies the value of that column should be unique.


Lets see the declaration of Unique key during the creation of table   

    

              create table user (

                               user_id         int primary key,

                               username      varchar(30) unique

                         );

    No comments:

    Post a Comment