Intro

  • Auto increment attribute can be applied to a column that is set as a key.
  • Whenever we insert a new row, our primary key can be populated automatically.
  • Then each subsequent row is auto incremented.
    So, below sections show how we can do it.

How to Set Auto Increment That Start With 1

  • Create a table, let's say, here we want to create a transaction table.
CREATE TABLE transactions (
	transaction_id INT PRIMARY KEY AUTO_INCREMENT,
	amount DECIMAL (5, 2)
)
  • Note that, if we want the number to start with other number than 1, we need to set it. In this example, by default, the transaction_id will start at 1.
  • Then, Insert a value into the table.
INSERT INTO transactons (amount)
VALUES (4.99);
SELECT * FROM transactions;

transaction_id amount
1 4.99
  • Try insert another value into the table to see how it work.
INSERT INTO transactons (amount)
VALUES (3.58);
SELECT * FROM transactions;

transaction_id amount
1 4.99
2 3.58
  • So, you can continue adding the data from here.

How to Set Auto Increment That Start With Specific Values

  • Create a table, let's say, here we want to create a transaction table. Here, you need to specifically state the specific value, for instance, 1000.
CREATE TABLE transactions (
	transaction_id INT PRIMARY KEY AUTO_INCREMENT = 1000,
	amount DECIMAL (5, 2)
)
  • Insert a value into the table.
INSERT INTO transactons (amount)
VALUES (4.99);
SELECT * FROM transactions;

transaction_id amount
1000 4.99
  • Try insert another value into the table to see how it work.
INSERT INTO transactons (amount)
VALUES (3.58);
SELECT * FROM transactions;

transaction_id amount
1000 4.99
1001 3.58
  • So, you can continue adding the data from here.