SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

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(2) 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 TYPE=MyISAM;

If you delete all records by truncate table sql command then while adding the first record the auto increment field will start from 1.

Discuss this tutorial at forum

List of SQL Tutorials


Further readings
MySQL Auto Increment field to generate unique record ID
How to start auto increment value from a predefined value ?
mysql_insert_id() to get the auto increment number of just inserted record
 
Scripts
PHP
JavaScript
HOME
SQL Tutorial List
SQL (Home)
SQL Commands
AVG
Alter Table
Between
Count
Copy Table
Create Table
Delete
Distinct
Group by
Having
Insert
Inner Join
IN
Left join
Limit
Like
MAX
MIN
Order By
OR AND
Rand
Replace
Rename Table
Select Query
Sum
Union
Update
Where
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.