CREATE TABLE `student` (
`student_id` INT( 3 ) NOT NULL AUTO_INCREMENT,
`name` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 50 ) NOT NULL ,
UNIQUE (
`student_id`
)
);
Now we will add one record to this table using one insert command.
INSERT INTO `student` ( `name` , `email` )
VALUES ( 'john', 'john@sitename.com' );
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.
Use lastInsertId() PHP PDO function
Use insert_id() while adding record using MySQLi.
Details about autoincrement field and insert_id() with examples
ALTER TABLE student AUTO_INCREMENT = 50;
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.
CREATE TABLE IF NOT EXISTS `student4` (
`id` int(5) unsigned zerofill NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '',
`class` varchar(10) CHARACTER SET utf8 NOT NULL DEFAULT '',
`mark` int(3) NOT NULL DEFAULT '0',
`gender` varchar(6) CHARACTER SET utf8 NOT NULL DEFAULT 'male',
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=38 ;
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.Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
Senthu | 07-12-2009 |
How to export the tables in .sql format from phpmysql db by using in phpMySQL ?? Thank you SENTHU |
dhananjay | 13-09-2010 |
goto phpmyadmin, select database , select table using checkbox, go downward select option mysql then zip and click on go it will ask you where u want to save your table . |
Nitin | 09-11-2011 |
is it compulsory to define auto increment field unique or primary key constraint ? |