Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • SQL Primary Key

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 360
    Comment on it

    Basically Primary key is used to identify each row in a table . In a table Only one Primary key can be present . When in a table , there is more than one primary key then it would be called composite Primary key . Composite key is used when we need multiple column to identify a row .

    Primary Key Constraints
    1) Primary key always has a unique key i.e every Primary key is a unique key but vice-versa may not be true .
    2) Primary Key can not accept Null value i.e Once you define Primary key in a table then you must enter the value .
    3) In a table only one Primary key exist i.e no duplicate value for a Primary key .

    Advantage :
    Using Primary key we can access to the data faster .

    Syntax in MySQL :

    create table student ( s_id int not null , s_name varchar ( 20 ) not null Primary Key ( s_id ) ) ;
    

    Syntax in SQL , Oracle , Ms Access :

    create table student ( s_id int not null Primary Key , s_name varchar ( 20 ) not null ) ;
    

    Syntax in MySQL , SQL , Oracle , Ms Access for Multiple Column :

    create table student ( s_id int not null  , s_name varchar ( 20 ) not null , s_city varchar ( 30 ) constraint pk_studentID primary key ( s_id , s_name ) ) ;
    

    Note : In above query there is only one Primary key ( pk_studentID ) and this primary key is made up of two column ( s_id and s_name ) ;

    If you want to change existing Primary key in a table then you can use below code :

    1) Primary key on one column :

    alter table student add primary ( s_id ) ;
    

    2) Primary key on multiple column :

    alter table student add constraint pk_studentID primary key ( s_id , s_name ) ;
    

    If you want to drop a Primary Key constraint then you can use below code :
    In MySQL Database :

    alter table student drop primary key ;
    

    In SQL , Oracle , MS Access :

    alter table student drop constraint pk_studentID ;
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: