Create a MySQL table with a primary key
A primary key uniquely identify a row in a table. One or more columns may be identified as the primary key. The values in a single column used as the primary key must be unique (like a person’s social security number). When more than one column is used, the combination of column values must be unique.
When creating the contacts table described in Create a basic MySQL table, the column contact_id can be made a primary key using PRIMARY KEY(contact_id) as with the following SQL command:
CREATE TABLE `test1` (
contact_id INT(10),
name VARCHAR(40),
birthdate DATE,
PRIMARY KEY (contact_id)
);
Additional columns can be identified as part of the primary key with a comma separated list in the PRIMARY KEY command, like PRIMARY KEY (contact_id, name).










sandeep said on January 27, 2009
thanks a lot buddy
Frank said on February 4, 2009
Cool, that’s what I was looking for
asder43 said on February 6, 2009
it was helpful, thanks
Cw said on June 23, 2009
Simple and nice
Atchyut said on November 14, 2009
thank you
Anonymous said on January 7, 2010
thanks for the comments…..
very useful for my study……..
kimberley said on January 21, 2010
thank you, its really usable to us students…
it helps us to learn….
Van sen said on February 2, 2010
Thanks !
Can you define a primary key once the table is created ?
My table is already created and I would like to set the primary key at this point.
sinthiya said on February 4, 2010
thank you!!! but if i want to add primary key after entering the data inside the table what i do for it?
Anonymous said on February 16, 2010
thanks!!!
rimpe said on February 26, 2010
Thanks a lot. But I have a question,
KEY `TITLE_INDEX` (`TITLE`),Why use that?
Patrick said on April 18, 2010
Nice brief summary.
Shukla Alok1 said on August 11, 2010
Thanks for this information…. nice……..
Sandeep Boda said on September 2, 2010
can i have secondary key ?
can i load data by verifying both keys ?
Ntb said on September 7, 2010
thx alots
Piotr said on October 20, 2010
thx
Khehlamarema said on November 9, 2010
yes u can have a secondary key
Shinaskollam said on December 15, 2010
thanx machaa !!!!
Moulalishaik123 said on December 16, 2010
thank
No1 said on January 5, 2011
Anyone got the MySql command for deleting a row based on its primary key?
Evaline Cherotich said on January 28, 2011
this is a good summary which has helped me a lot in my study.thanx
Evaline Cherotich said on January 28, 2011
this was of great use to my study.thanx a lot
Qrrrr said on February 8, 2011
Thanks
Rayalla said on April 11, 2011
tanx its gud for freshers
Feesi1 said on June 21, 2011
thanks bud
Sreeram said on December 5, 2011
Thanks a lot …..Is there any option in the mysql table structure it selft rather than creating the table……
Regards,
Sreeram Veerapaneni
Pacopag said on December 9, 2011
Tech recipes rocks! Just had to say thanks for the countless times this site has helped me out.
Saaaa said on January 11, 2012
thx, very helpful for beginners !!