15

I have the following SQLite query:

CREATE TABLE Logs ( Id integer IDENTITY (1, 1) not null CONSTRAINT PKLogId PRIMARY KEY, ...
  1. IDENTITY (1, 1) -> What does this mean?
  2. PKLogId what is this? This doesn't seem to be defined anywhere
  3. I want Id to be integer primary key with autoincrement. I would like to be able to insert into this Logs table omitting Id column in my query. I want Id to be automatically added and incremented. Is this possible? How can I do this?

At the moment when I try to insert without Id I get:

Error while executing query: Logs.Id may not be NULL
2
  • It looks like SQL Server syntax. Commented Dec 15, 2011 at 12:12
  • Yes, this Create Table query was converted from SQL Server to SQLite
    – Caner
    Commented Dec 15, 2011 at 12:14

3 Answers 3

60

I'm not sure whether you're actually using SQLite according to the syntax of your example.

If you are, you may be interested in SQLite FAQ #1: How do I create an AUTOINCREMENT field?:

Short answer: A column declared INTEGER PRIMARY KEY will autoincrement.

1
  • 11
    My issue was that I had used INT instead of INTEGER. Thanks for the answer Commented Aug 9, 2013 at 7:09
12

Change it to:

CREATE TABLE Logs ( Id integer PRIMARY KEY,....
0

If you are, you may be interested in SQLite FAQ #1: How do I create an AUTOINCREMENT field?:

Short answer: A column declared INTEGER PRIMARY KEY will auto increment.

This in fact is not entirely accurate. An integer primary key will indeed increment, however if the table drops all rows, it starts from the beginning again, It is important if you want to have all associated records tied correctly to use the autoincrement description after the primary key declaration on the integer field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.