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. This is the best way to generate a trouble ticket number for a help desk system. In a school if a new student joins then all details of the student we can feed to student table and while using the insert query, MySQL will add one auto generated unique number to this auto incremented field. We need not have to specify any thing in our query while adding the other details ( except auto incremented field data).
Getting the unique ID
After successfully adding the record we can get this unique number by using mysql_insert_id(). Read the details on how to get this number by visiting the tutorial here. Now let us try how to create a such auto incremented field.
We have to make auto increment field integer ( obvious ) and also unique. The property set to auto increment. Here is the sql to create a student table with one auto increment field to assign one unique student ID.
CREATE TABLE `student` (
`student_id` INT( 3 ) NOT NULL AUTO_INCREMENT,
`name` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 50 ) NOT NULL ,
Now we will add one record to this table using one insert command.
INSERT INTO `student` ( `name` , `email` )
VALUES ( 'john', 'firstname.lastname@example.org' );
You will see automatically the student_id field will be added with 1 and for next record it will get the next incremented value 2.
This way we can generate auto increment id for each record in our table.
Use lastInsertId() PHP PDO function
Maximum value of inserted id
The maximum value of the inserted id depends on the type of numeric field we have selected. For TINYINT it will take value upto 127 for unsigned it is 255. After this the creation of auto increment ID will fail. So take care while creating the auto increment field to match your future requirements.
Starting from higher number
Change the auto increment field like this
ALTER TABLE student AUTO_INCREMENT = 50;
Pre defined value for a MySQL auto increment field
We can start an auto increment field from a pre defined value. For example when we issue a student ID number, where student ID is an auto increment field we can generate IDs with values 100001 from the first record. This helps as we don't want to give a number like 1 to the first student and end with number 10004. This way we can have first number as 100001 and last number 110005. This way all the students will have six digit number.
This we can take care while creating the table by adding auto increment value at the end. Here is the SQL for creating a student table with auto increment field is set to 100000
CREATE TABLE student (
id int(7) NOT NULL auto_increment,
name varchar(50) NOT NULL default '',
class varchar(10) NOT NULL default '',
mark int(3) NOT NULL default '0',
sex varchar(6) NOT NULL default 'male',
UNIQUE KEY id (id)
) auto_increment=100000 ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
If you delete all records by truncate table sql command then while adding the first record the auto increment field will start from 1.
alter table and make one numeric field as auto increment
Read here on how to create one incremental auto generated record number in MSSQL table.