MySQL AUTO INCREMENT

The AUTO_INCREMENT keyword is used to increment the value of a column automatically on every insert.
Usually, AUTO_INCREMENT will increment the value by 1 for each row.

Example for AUTO_INCREMENT

create table employees(empno int primary key AUTO_INCREMENT, 
name varchar(50), 
age numeric, 
role varchar(50), 
location varchar(50), 
salary decimal);

Here we have set the empno field as primary key as well as with AUTO_INCREMENT keyword.

Now, any insert to the table employees will automatically assign a value incremented by 1 for the field empno.

Example

insert into employees(name, age, role, location, salary)
values('Andrew',30, 'Manager','India',100000);

In the above query, the columns to which the values have to be inserted is mentioned with comma seperation along with its value(s). The column empno is not specified in the list as well as its values is not supplied.

An unique value will be automatically assigned to the empno column.


Most Read