Create a MySQL table with a primary key

Contributor Icon Contributed by qmchenry Date Icon February 8, 2004  
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).

Previous recipe | Next recipe |
 
  • sandeep
    thanks a lot buddy
  • Cool, that's what I was looking for
  • asder43
    it was helpful, thanks
  • Cw
    Simple and nice
  • Atchyut
    thank you
  • velmuruganm
    thanks for the comments.....
    very useful for my study........
  • kimberley
    thank you, its really usable to us students...
    it helps us to learn....
  • 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
    thank you!!! but if i want to add primary key after entering the data inside the table what i do for it?
blog comments powered by Disqus