Database Primary Key Importance

Primary Key main task is to provide uniqueness of every record in a table. You can write different kinds of data into fields of a table, but one field should be unique. For instance something like second name field could be bad choice, because many people can have the same second name. In most cases the best choice is to choose some integer number which is auto-incremented. You can use this number or not, this is up to you, but with the number you can be sure that every inserted record can be recognized as different, assigned with an unique number, because every time you insert new record into a table, the primary key int field is incremented by one automatically. If you have 100,000 records, and delete 50,000 records, and insert a new record, the new number for this record will be 100,001, in spite of the previously deleted 50,000 records. In other words, the primary key with auto-incremented option doesn’t look back.

Below you can see an example of very SQL query – creation of simple customers table – with usage of primary key.

$sqlquery = “CREATE TABLE customers
(
customerID int NOT NULL AUTO_INCREMENT,
FirstName varchar(15),
LastName varchar(15),
PRIMARY KEY(customerID)
)”;

Related posts: