How to make auto increment column in mysql with example

if you want increment count in mysql than use this syntax

Syntax:-

column_name data_type(size) AUTO_INCREMENT

auto increment count for example 1 for 1st row 2 for 2nd row 5 for 5th row so on..

Example:-

CREATE TABLE homedetails
(
Homeno int AUTO_INCREMENT,
Homename varchar(255) NOT NULL,
state varchar(255),
country varchar(255),
PRIMARY KEY(homeno)
);

homeno homename state country
1 chiguru karnataka india
2 krish kerala india
3 apti NULL USA

No need to insert homeno,it automatically increment

AUTO_INCREMENT with NOT NULL

Syntax:-

column_name data_type(size) NOT NULL AUTO_INCREMENT

Example:-

CREATE TABLE homedetails
(
Homeno int NOT NULL,
Homename varchar(255) NOT NULL AUTO_INCREMENT,
state varchar(255),
country varchar(255),
PRIMARY KEY(homeno)
);


Note:-
# AUTO_INCREMENT add 1 to last row data
# NOT NULL and AUTO_INCREMENT can be used for single column.


Leave reply

Add your comments here

Back to Top