Home > Logic, MySQL > Primary Keys and Auto Increment Fields

Primary Keys and Auto Increment Fields

The following example sets the personID field as the primary key field.

The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting.

AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added.

To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field.

Example :

$sql = “CREATE TABLE Persons
(  personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
FirstName varchar(15),
LastName varchar(15),
Age int )”;

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: