menu

SQL PRIMARY Key

SQL – PRIMARY Key Constraint :

1 . At The Time Of Table Creation :

SQL PRIMARY Key : PRIMARY Key Constraint is used to identify every record uniquely which can’t be null. There can be only one primary key in a table.

SQL – PRIMARY Key Constraint Syntax Example :

Example

CREATE TABLE UserProfile (
ID int(11) NOT NULL,
Name varchar(110) NOT NULL,
Email varchar(100) NOT NULL,
CONSTRAINT USER_ID_PRIMARY_KEY PRIMARY KEY (ID)
) ;

Where USER_EMAIL is Constraint’s name you can choose constraint name as per you understanding.

2 .In Existing Table :

Example

ALTER TABLE UserProfile
ADD
CONSTRAINT USER_ID_PRIMARY_KEY PRIMARY KEY (ID);

Drop PRIMARY Key Constraint :

Example

ALTER TABLE UserProfile
DROP CONSTRAINT USER_ID_PRIMARY_KEY;

Note : In Mysql You have to Use Following Query to Drop the Constraint :

Example

ALTER TABLE UserProfile
DROP INDEX USER_ID_PRIMARY_KEY;

User Ans/Comments

📖 Read More


    Advertisements