SQL Primary Key Constraint



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



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:



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


The name of the table to modify.


The name of the primary key.

column1, column2, … column_n

The columns that make up the primary key.

Previous articleSQL Regular Expressions
Next articleHow to create a Session Array() variable in PHP
Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!

Leave a Reply

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