MainMenu

Home Core Java Overview Maven Tutorials

Sunday, 15 January 2017

Auto-Increment With example

Auto Increment in Mysql and in SQL with example

AutoIncrement

As the name says, when a new record will be inserted into the table then primary key value will be increment.
Also the starting value and interval of auto-increment can be managed. By default auto-increment start from 1 and increase by 1

AutoIncrement In Mysql


Example :
Create Table Employee
(
Emp_id Int Not Null AUTO_INCREMENT,
Name varchar(50),
salary int(10),
PRIMARY KEY(Emp_id)
)

Now insert the record in this table


Insert Into Employee (Name, Salary)
Values("chandan", 25000),("Adhiraj", 35000)
Result:

Emp_idNameSalary
1chandan25000
2Adhiraj35000

Condition: Now if you want that Auto-Increment value start from 5 and increment by 6 then you need to use alter keyword.
Query : Alter Table Employee AUTO_INCREMENT = 5

AutoIncrement In Sql


Example :
Create Table Employee
(
Emp_id Int IDENTITY(1,1) PRIMARY KEY
Name varchar(50),
salary int(10),
)

Now insert the record in this table


Insert Into Employee (Name, Salary)
Values('chandan', 25000),('Adhiraj', 35000)
Result:

Emp_idNameSalary
1chandan25000
2Adhiraj35000

Condition: Now if you want that Auto-Increment value start from 5 and increment by 6 then you need to use alter keyword.
Example :
Create Table Employee
(
Emp_id Int IDENTITY(5,6) PRIMARY KEY
Name varchar(50),
salary int(10),
)

Now insert the record in this table


Insert Into Employee (Name, Salary)
Values('Himani', 21000),('Sailja', 32000)
Result:

Emp_idNameSalary
5Himani21000
11Sailja32000

Tag:Auto increment in SQl with Example, Auto Increment in my sql with Example

No comments:

Post a Comment