I put and did not put AUTOINCREMENT for id in person table as shown below:
CREATE TABLE person (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT # ↑↑↑↑↑↑↑↑↑↑↑↑↑
);
CREATE TABLE person (
id INTEGER PRIMARY KEY, # No AUTOINCREMENT
name TEXT
);
Then, I added 3 rows to person table as shown below:
INSERT INTO person (name)
VALUES ('John'), ('David'), ('Lisa');
Finally, I got the exactly same 3 rows from person table both incrementing the number for id by one as shown below:
.headers on
.mode box
SELECT * FROM person;
┌────┬───────┐
│ id │ name │
├────┼───────┤
│ 1 │ John │
│ 2 │ David │
│ 3 │ Lisa │
└────┴───────┘
So, what is the difference between AUTOINCREMENT and no AUTOINCREMENT for id in person table?
AUTOINCREMENTcan avoid to reuse the deleted numbers unless explicitly specifying them while noAUTOINCREMENTcannot according to the doc.For example, you put
AUTOINCREMENTforidinpersontable and it has 3 rows as shown below:Then, you delete the row where
idis3, then get 2 rows frompersontable as shown below:Now, you add one row to
persontable, then4is added toidas shown below:In addition, if you add one row to
persontable by explicitly setting3toid, then3is added toidas shown below:Next for example, you do not put
AUTOINCREMENTforidinpersontable and it has 3 rows as shown below:Then, you delete the row where
idis3, then get 2 rows frompersontable as shown below:Now, you add one row to
persontable, then3is added toidas shown below: