Create a MySQL table with a primary key

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: MySQL  

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).

 

28 Comments -


  1. sandeep said on January 27, 2009

    thanks a lot buddy

  2. Frank said on February 4, 2009

    Cool, that’s what I was looking for

  3. asder43 said on February 6, 2009

    it was helpful, thanks

  4. Cw said on June 23, 2009

    Simple and nice

  5. Atchyut said on November 14, 2009

    thank you

  6. Anonymous said on January 7, 2010

    thanks for the comments…..
    very useful for my study……..

  7. kimberley said on January 21, 2010

    thank you, its really usable to us students…
    it helps us to learn….

  8. 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.

  9. 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?

  10. Anonymous said on February 16, 2010

    thanks!!!

  11. rimpe said on February 26, 2010

    Thanks a lot. But I have a question,
    KEY `TITLE_INDEX` (`TITLE`),
    Why use that?

  12. Patrick said on April 18, 2010

    Nice brief summary.

  13. Shukla Alok1 said on August 11, 2010

    Thanks for this information…. nice……..

  14. Sandeep Boda said on September 2, 2010

    can i have secondary key ?
    can i load data by verifying both keys ?

  15. Ntb said on September 7, 2010

    thx alots

  16. Piotr said on October 20, 2010

    thx

  17. Khehlamarema said on November 9, 2010

    yes u can have a secondary key

  18. Shinaskollam said on December 15, 2010

    thanx machaa !!!!

  19. Moulalishaik123 said on December 16, 2010

    thank

  20. No1 said on January 5, 2011

    Anyone got the MySql command for deleting a row based on its primary key?

  21. Evaline Cherotich said on January 28, 2011

    this is a good summary which has helped me a lot in my study.thanx

  22. Evaline Cherotich said on January 28, 2011

    this was of great use to my study.thanx a lot

  23. Qrrrr said on February 8, 2011

    Thanks

  24. Rayalla said on April 11, 2011

    tanx its gud for freshers

  25. Feesi1 said on June 21, 2011

    thanks bud

  26. 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

  27. Pacopag said on December 9, 2011

    Tech recipes rocks! Just had to say thanks for the countless times this site has helped me out.

  28. Saaaa said on January 11, 2012

    thx, very helpful for beginners !!

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -