MySQL - Primary keys and auto increment fields in MySQL

Primary Keys and Auto Increment Fields in MySQL

Primary key is used to uniquely identify a row in a table. Usually the primary key is an integer value that could be an auto incremented value. The primary key column can also be a combination of two columns.

The column that is identified for PRIMARY key is defined in the schema as:
PRIMARY KEY (employee_id)
When a column is set to “Auto Increment”, its value is automatically incremented in a sequence when a new row is inserted.

Primary Keys and Auto Increment Fields in MySQL

Primary Key:

A primary key is used to uniquely identify a row in a table. A table can have only one primary key. The values in a single column to store primary key are unique. More one column can be used as a primary key. When used, the combination of column values must be unique.

Auto Increment attribute:

The Auto Increment attribute is used to generate unique values for new rows. Value once used by a row, can not be used for another row.

The following example illustrates the use of both primary key and auto increment attribute.
CREATE TABLE Products (
id NOT NULL AUTO_INCREMENT,
product_name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
MySQL - COMMIT and ROLLBACK in MySQL
COMMIT and ROLLBACK in MySQL - A transaction in MySQL is a set of SQL statements written to perform a specific task......
MySQL - ALTER command to add and drop INDEX in MySQL
ALTER command to add and drop INDEX in MySQL - An index in MySQL can be added using ALTER statement in multiple ways....
MySQL Connection- Describe MySQL Connection using PHP Script.
MySQL Connection - Establishing connection to MySQL database using PHP can be done by using mysql_connect(server,user,passwd,new_link,client_flag)....
Post your comment