![]() |
|
MySQL Primary Keys. Free online PHP and MySQL Web Database Programming Tutorial... |
| Lessons | MySQL Primary Keys |
|
|
This is true for al most all of the relational database management systems that each table of the database has to have a primary key field. A primary key field is used to uniquely identify the rows or records in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record. Often the primary key field is some sort of a number e.g. ID, and is often used with the AUTO_INCREMENT setting. When we use AUTO_INCREMENT with primary key, or any other field, it means that the value of the field is to be incremented automatically, usually by 1 each time a new record is added. To ensure that the primary key field is not to be null, we must add the NOT NULL setting to the field. Example: The following SQL code sets the studentID field as the primary key field. $sql = "CREATE TABLE Students
(
studentID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(studentID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_query($sql,$con);
Next: PHP MySQL Insert
|