S
S
ssbxlan2012-04-20 09:12:42
SQL
ssbxlan, 2012-04-20 09:12:42

AUTOINCREMENT in SQLite3

I'm trying to create a simple table in SQLITE3:

CREATE TABLE IF NOT EXISTS `tbl` (
  `id` int(3) NOT NULL AUTOINCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);
And nothing is created, swears at AUTOINCREMENT. Initially, I wrote AUTO_INCREMENT, as in MySQL, then I found the documentation, which states that it is written without underlining. No more sense, near "AUTOINCREMENT": syntax error. Tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Gregory, 2012-04-20
@ssbxlan

Here is a query that works.

CREATE TABLE IF NOT EXISTS `tbl` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
  `name` varchar(255) NOT NULL);

A small recipe for the future to solve such problems: create a table of the desired type with a wizard and look at the creation script.

M
Mithgol, 2012-04-23
@Mithgol

In general, to learn the SQL syntax that is used in SQLite, you need to read http://sqlite.org/lang.html .
In particular, at http://sqlite.org/lang_createtable.html you can see the "column-constraint" diagram, according to which AUTOINCREMENT is a modifier for PRIMARY KEY.

D
deko, 2013-01-18
@deko

According to the SQLite FAQ , INTEGER PRIMARY KEY may not be declared as AUTOINCREMENT.
Quote:
How do I create an AUTOINCREMENT field.
Short answer: A column declared INTEGER PRIMARY KEY will autoincrement.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question