Parse error: near "autoincrement": syntax error - SQLite
Asked Answered
N

5

32

I get a syntax error near AUTOINCREMENT. What is the cause of this error?

CREATE TABLE person (
    id INTEGER NOT NULL AUTOINCREMENT,
    name TEXT NOT NULL
);

CREATE TABLE department (
    id INTEGER NOT NULL AUTOINCREMENT,
    name TEXT NOT NULL,
    FOREIGN KEY (leader) REFERENCES person(id)
);
Nollie answered 28/10, 2010 at 12:22 Comment(0)
L
64

According to SQLite FAQ you have to declare either a INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT column to achieve that.

Lucre answered 28/10, 2010 at 13:16 Comment(0)
P
10

In SQLite you need not to specify AUTOINCREMENT if you are specifying a column as Primary Key...

Philippopolis answered 9/4, 2014 at 9:47 Comment(0)
C
1

SQLite AUTOINCREMENT : You Should Avoid Using It

Unless you create a table specifying the WITHOUT ROWID option, you get an implicit auto increment column called rowid.

The rowid column store 64-bit signed integer that uniquely identifies a row within the table.

Claver answered 14/3, 2019 at 17:55 Comment(1)
Also, do select rowid, * from table_name to retrieve the rowid as well.Equator
C
0

I got the same error below:

Parse error: near "autoincrement": syntax error

Because I used AUTOINCREMENT without INTEGER PRIMARY KEY as shown below:

CREATE TABLE my_table (
  id INTEGER AUTOINCREMENT
          -- ↑↑↑↑↑↑↑↑↑↑↑↑↑
);

So, I used AUTOINCREMENT with INTEGER PRIMARY KEY as shown below, then the error was solved:

CREATE TABLE my_table (
  id INTEGER PRIMARY KEY AUTOINCREMENT
  -- ↑↑↑↑↑↑↑ ↑↑↑↑↑↑↑ ↑↑↑
);

The doc says below:

Any attempt to use AUTOINCREMENT on a WITHOUT ROWID table or on a column other than the INTEGER PRIMARY KEY column results in an error.

In addition, if you use TEXT type with PRIMARY KEY AUTOINCREMENT as shown below:

CREATE TABLE my_table (
  id TEXT PRIMARY KEY AUTOINCREMENT
  -- ↑↑↑↑
);

Then, you get the error below:

Parse error: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY

Colubrid answered 29/9, 2023 at 5:5 Comment(0)
M
-4

It's an easy solution. Just use AUTOINCREMENT instead of AUTO_INCREMENT

Mosa answered 8/5, 2020 at 3:28 Comment(1)
They are using AUTOINCREMENT.Preventive

© 2022 - 2024 — McMap. All rights reserved.