Skip to content

Latest commit

 

History

History
20 lines (15 loc) · 690 Bytes

auto-increment-primary-ids.md

File metadata and controls

20 lines (15 loc) · 690 Bytes

Auto incrementing IDs

Sometimes we had to add a lot of data into our tables, each row should have a primary key. This can be a manual task. There is a way to always auto increment that number and let the db handle it.

create table contacts (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(20) not null,
    email VARCHAR(30),
    telephone VARCHAR(30)
);

If we look the above table, we can see that our id INT PRIMARY KEY has AUTO_INCREMENT which means this will increment the ID automatically.

To add the data we would do the following:

insert into contacts(name, email, telephone) values('Paul Bennett', '[email protected]', '07551 123456');