How to set initial value and auto increment in MySQL ?

Monday, February 27, 2017

How to set initial value and auto increment in MySQL ?


we can use below command -

ALTER TABLE employee AUTO_INCREMENT=1001;

or if you haven't already added an id column, also add it

ALTER TABLE employee ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ADD INDEX (id);


Example :-

MySQL - Setup an auto-incrementing primary key that starts at 20001:

Step 1.  create your table:

create table employee(
  id       int(11) auto_increment, 
  name varchar(40),
  PRIMARY KEY (id)
)

Step 2 set the start number for auto increment primary key:

ALTER TABLE employee AUTO_INCREMENT=20001;

Step 3 insert some rows:-

insert into employee (name) values("pappu");
insert into employee(name) values("mamu");

Step 4, interpret the output:

select * from employee

'20001', 'pappu'
'20002', 'mamu'


If you need to add column for auto increment

alter table employee add column id int(5) NOT NULL AUTO_INCREMENT FIRST

This query for add column at first. Now you have to reset auto increment initial value. So use this query 

alter table employee AUTO_INCREMENT=20001


0 comments:

About This Blog

Lorem Ipsum

  © Copyright 2009 Linux-HelpLine.Blogspot.com

Back to TOP