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')
- 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;
- We can see here that different sequences of values can also be added.
- Log in to post comments