Primary Keys and Auto Increment Fields

Posted by tutor | Posted in PHP Tutorial | Posted on 18-12-2009-05-2008

0

A primary key uniquely identifies the rows in a table. Each primary key value is unique within the table. It cannot be null because the database engine requires a value to locate the record.

The primary key of a relational table uniquely identifies each record in the table. A primary key can be any of the following:

It can either be a normal attribute that is guaranteed to be unique such as Social Security Number in a table with no more than one record per person

It can be generated by the DBMS. Primary keys may consist of a single attribute or multiple attributes in combination.

Autoincrement

We can get a unique auto generated number from MySQL by creating an auto incremented field. MySQL will generate a unique number by incrementing the last number of the table and will automatically add to the auto incremented field. We need not have to specify any thing in our query while adding the auto incremented field data. After successfully adding the record we can get this unique number by using mysql_insert_id().

    For eg:

    $sqlQuerry = “CREATE TABLE Persons
    (
    countryID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(countryID),
    Country varchar(15),
    Capital varchar(15)
    )”;
    mysql_query($sqlQuerry,$conn);

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.