SQL Primary Key Constraint

0
148

sqltuts

SQL Primary Key Constraint

This constraint is used to uniquely define an identify any entry in the table. There are certain conditions like a primary key value can’t be NULL and it must be UNIQUE. There can be only one primary key in a table.

  Example of PRIMARY KEY Constraint on CREATE TABLE

 

Example of PRIMARY KEY Constraint on ALTER TABLE

 

 

Example of DROP a PRIMARY KEY Constraint

 

 

Sample Version

 

Example

Let’s look at an example of how to create a primary key using the CREATE TABLE statement in MySQL.

 

We could also create a primary key with more than one field as in the example below:

 

CREATE PRIMARY KEY – USING ALTER TABLE STATEMENT

You can create a primary key in MySQL with the ALTER TABLE statement.

table_name

The name of the table to modify.

constraint_name

The name of the primary key.

column1, column2, … column_n

The columns that make up the primary key.

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.