Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite: autoincrement primary key questions

Tags:

sql

sqlite

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
like image 839
Caner Avatar asked Dec 15 '11 12:12

Caner


People also ask

Is Autoincrement primary key?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

How can create autoincrement primary key in SQLite?

SQLite AUTOINCREMENT is a keyword used for auto incrementing a value of a field in the table. We can auto increment a field value by using AUTOINCREMENT keyword when creating a table with specific column name to auto increment. The keyword AUTOINCREMENT can be used with INTEGER field only.

Does primary key auto increment SQLite?

On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.

What is the maximum size of varchar in SQLite?

(9) What is the maximum size of a VARCHAR in SQLite? SQLite does not enforce the length of a VARCHAR. You can declare a VARCHAR(10) and SQLite will be happy to store a 500-million character string there. And it will keep all 500-million characters intact.


2 Answers

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.

like image 172
Bruno Avatar answered Oct 02 '22 15:10

Bruno


Change it to:

CREATE TABLE Logs ( Id integer PRIMARY KEY,....
like image 40
Michał Powaga Avatar answered Oct 02 '22 16:10

Michał Powaga