MySQL Auto Increment

Profile picture for user arilio666

Auto-increment is used when we want MySQL to generate unique numbers, which increments itself automatically when a new record is inserted into a table.

Example: MySQL Auto Increment Without Primary Key

CREATE TABLE Namelist (
    id int NOT NULL AUTO_INCREMENT,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    PRIMARY KEY (id)
);
INSERT INTO Namelist (LastName, FirstName)  
VALUES ('Bond', 'James') 

mysql auto increment without primary key

  • We have created a table Namelist, and we added auto increment to it so that now when we try to insert some records without adding id, it automatically increments.
  • It will increment 1 for each row, and if we want to start incrementing with different values.
ALTER TABLE Namelist AUTO_INCREMENT=100;

how to define auto_increment in mysql

  • We can see here that different sequences of values can also be added.
Tags