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_id | Name | Salary |
---|---|---|
1 | chandan | 25000 |
2 | Adhiraj | 35000 |
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_id | Name | Salary |
---|---|---|
1 | chandan | 25000 |
2 | Adhiraj | 35000 |
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_id | Name | Salary |
---|---|---|
5 | Himani | 21000 |
11 | Sailja | 32000 |
No comments:
Post a Comment