You are here: Articles > Database > MySQL

 See more articles about "MySQL "

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

 

Also see ...

Delete a column from an existing MySQL table
H3The SQL command in this recipe removes a column and the column's data from an existing MySQL table./H3PTo delete the column span style="font weight: bold"col_stuff/span from the table span style="font weight: bold"table_things/span, use the following SQL command: br / br /div cl

Add a column to an existing MySQL table
H3MySQL tables are easy to extend with additional columns./H3PTo add a column called span style="font weight: bold"email/span to the contacts table created in a href="mysql_tips376.html" target="_blank" class="postlink" rel="nofollow"Create a basic MySQL table/a with a datatype of VARC

Modify an existing MySQL column
H3The best laid plans of mice and DBAs oft go awry, so it is sometimes necessary to change the characteristics of a column after it exists and contains data. Beware whenever you make changes to your database always make a backup first./H3PAfter a week of using the contacts table created in

MySQL dump import -> Error 1217
H3When using innodb tables and foreign key constraints, importing a mysqldump can sometimes generate foreign key errors. /H3PAdd to the beginning of the dump file: SET FOREIGN_KEY_CHECKS=0; br / br /Add to the end of the dump file: SET FOREIGN_KEY_CHECKS=1; br / br /This disables forei

Show or list tables in a MySQL database
H3Once you have selected a database, you can list the tables in it by using the show command. /H3PTo view all of the tables in the selected database, use: br / br /[code]SHOW TABLES; br /[code] br /To view all of the tables in a different database that isn't selected: br / br /&