What's wrong with this query? - sqlite

CREATE TABLE IF NOT EXISTS fw_users (id INT(64) NOT NULL PRIMARY KEY AUTOINCREMENT, auth CHAR(64) UNIQUE, money INT(32) DEFAULT '0', unlocks VARCHAR(8000))
I can't see any error in it, but SQLite throws an error:
Query failed! AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
It doesn't make sense, id IS an integer

INT(64) isn't close enough; it must be INTEGER.

The SQLite notation is INTEGER PRIMARY KEY. Docs reference:
If you declare a column of a table to be INTEGER PRIMARY KEY, then whenever you insert a NULL into that column of the table, the NULL is automatically converted into an integer which is one greater than the largest value of that column over all other rows in the table, or 1 if the table is empty. Or, if the largest existing integer key 9223372036854775807 is in use then an unused key value is chosen at random.
[...]
CREATE TABLE t1(
a INTEGER PRIMARY KEY,
b INTEGER
);

Related

Is it possible to insert the specified value in autoincrement column?

I need to copy one table to another and both tables contain column with AUTOINCREMENT. Is it possible to insert a defined value into AUTOINCREMENT column.
Tables:
CREATE TABLE tmptimetables (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
_title NVARCHAR(256) NOT NULL,
_weeks INTEGER NOT NULL,
_first_week_date INTEGER NOT NULL,
_auto_complete INTEGER NOT NULL,
_first_lesson_time INTEGER NOT NULL,
_lesson_duration INTEGER NOT NULL,
_break_duration INTEGER NOT NULL,
_color INTEGER NOT NULL,
_symbol NCHAR(1) NOT NULL
);
CREATE TABLE timetables (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title NVARCHAR(256) NOT NULL,
weeks INTEGER NOT NULL,
first_week_date INTEGER NOT NULL,
auto_complete INTEGER NOT NULL,
first_lesson_time INTEGER NOT NULL,
lesson_duration INTEGER NOT NULL,
break_duration INTEGER NOT NULL,
color INTEGER NOT NULL,
symbol NCHAR(1) NOT NULL
);
My SQL request:
INSERT INTO timetables (
auto_complete,
break_duration,
color,
first_lesson_time,
first_week_date, id,
lesson_duration,
symbol,
title,
weeks
)
SELECT
_auto_complete,
_break_duration,
_color,
_first_lesson_time,
_first_week_date,
_id,
_lesson_duration,
_symbol,
_title,
_weeks
FROM tmptimetables
AUTOINCREMENT can only be used for a column that is INTEGER PRIMARY KEY, it is INTEGER PRIMARY KEY that is the factor that makes the column a special column whereby if the value is not provided when inserting a row that a unique integer will be assigned.
So be the column INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT you can specify an integer value and a row may be inserted with the given value.
A row will not be inserted with a given value if that value is not unique.
For example if the table timetables is currently :-
Then
INSERT INTO timetables (id,title,weeks,first_week_date,auto_complete,first_lesson_time,lesson_duration,break_duration,color,symbol) VALUES (null,'mytitle',78,86000,23,1800,900,200,16,'E');
Would insert a new row with the id as determined by SQLite's algorithm for providing a unique id (probably 5).
If the id were changed to be provided (i.e. not null) say to 10 as per :-
INSERT INTO timetables (id,title,weeks,first_week_date,auto_complete,first_lesson_time,lesson_duration,break_duration,color,symbol) VALUES (10,'mytitle',78,86000,23,1800,900,200,16,'E');
Then the id for the new row would be 10.
However if then using (the same SQL but with the last column value changed) :-
INSERT INTO timetables (id,title,weeks,first_week_date,auto_complete,first_lesson_time,lesson_duration,break_duration,color,symbol) VALUES (10,'mytitle',78,86000,23,1800,900,200,16,'Z');
A new row would not be inserted as a row with an id of 10 already exists.
Finally if the id is not given (null is used) but the SQL is otherwise the same a new row is inserted with a unique id being provided by SQLite e.g.
INSERT INTO timetables (id,title,weeks,first_week_date,auto_complete,first_lesson_time,lesson_duration,break_duration,color,symbol) VALUES (null,'mytitle',78,86000,23,1800,900,200,16,'Z');
So the end result of following the above is :-
AUTOINCREMENT
The AUTOINCREMENT keyword, only usable for an INTEGER PRIMARY COLUMN, invokes a different algorithm for determining the next sequence to ensure that the next sequence/id is always greater, whilst without AUTOINCREMENT a lower sequence/id can be applied.
The AUTOINCREMENT keyword does not specify that if a value for the column is not provided then a sequence/id is applied it is INTEGER PRIMARY KEY that specifies that. Well actually, by default, i.e. unless WITHOUT ROWID is specified, this happens for all tables. It's just the the special rowid column is hidden. Specifying <column_name> INTEGER PRIMARY KEY (where is a valid column name) creates an alias of the rowid.
For example using SELECT rowid,* FROM timetables produces :-
SQLite Autoincrement
Rowid Tables

Primary key does not allow unique replace functionality

CREATE TABLE resource_sync
(
_id INTEGER UNIQUE ON CONFLICT REPLACE PRIMARY KEY,
status_id INTEGER,
result_id INTEGER
);
In case two equal _id values get inserted, SQLite would throw an exception:
[13:39:48] Error while executing SQL query on database 'test': UNIQUE
constraint failed: resource_sync._id
However, it allows for desired replaces in case primary key declaration is removed from table creation SQL.
Why is that?
Thanks.
UNIQUE is ignored on primary keys.
The correct syntax, as shown in the documentation, is:
CREATE TABLE resource_sync
(
_id INTEGER PRIMARY KEY ON CONFLICT REPLACE,
status_id INTEGER,
result_id INTEGER
);

INSERT OR IGNORE INTO doesn't work

I have a table created as:
create table association (_id integer unique primary key autoincrement , id_rules integer, id_places integer)";
To avoid replication of entry, I use the statement INSERT OR IGNOR, but it doesn't work. For example,
value (id_rules , id_places) = ("11","1") alredy in table, but using:
INSERT OR IGNORE INTO association (id_rules , id_places) VALUES ("11","1")
a new row is created.
Please, do anyone Know hwere is my mistake?
INSERT OR IGNORE will ignore any rows that would violate a UNIQUE constraint.
The only such constraint is on the _id column, which you did not specify.
If you want to prevent duplicates in those two columns, you have to add a constraint for them to the table definition:
CREATE TABLE association (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
id_rules INTEGER,
id_places INTEGER,
UNIQUE (id_rules, id_places)
);

Abort due to constraint violation columns C1,C2,C3 are not unique

I have a composite primary key(C1+C2+C3)on a table.
Here is the DDL
CREATE TABLE "InputFiles" (
[PlantID] INTEGER,
[FileID] INTEGER,
[VesselDataCase] CHAR(9),
[Comments] CHAR(73),
Primary key([PlantID], [FileID]),
FOREIGN KEY(PlantId) REFERENCES Plant(PlantId) ON DELETE CASCADE);
CREATE TABLE [VesselData] (
[MaterialType] NVARCHAR(100) NOT NULL,
[Operating_Temperature] NUMERIC,
[IRTndt] numeric,
[VDID] integer,
[PlantId] integer,
[FileId] integer,
FOREIGN KEY([plantid], [fileid]) REFERENCES [inputfiles]([plantid], [fileid]) ON DELETE cascade,
CONSTRAINT [sqlite_autoindex_VesselData_1] PRIMARY KEY ([VDID], [PlantId], [FileId]));
When I try to insert a new row in VesselData Table
Suppose VDID = 1, Fileid = 2, Plantid = 3. So it looks for(1+2+3) combination.
Even though fields with these values dont exist in the Table, it gives me
Abort due to constraint violation
columns VDID, PlantId, FileId are not unique SQlite exception
But, it is inserting the field in the table. After inserting this field it gives me this exception. Either it shouldn't insert or abort due to invalid field values
Thank you
Sun

How do I specify a Primary Key in Sqlite

How to define your specified attribute like StudentId in student table as Primary key in sqlite
CREATE TABLE Student(
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT
);
From the Sqlite spec:
One exception to the typelessness of
SQLite is a column whose type is
INTEGER PRIMARY KEY. (And you must use
"INTEGER" not "INT". A column of type
INT PRIMARY KEY is typeless just like
any other.) INTEGER PRIMARY KEY
columns must contain a 32-bit signed
integer. Any attempt to insert
non-integer data will result in an
error.
http://www.sqlite.org/datatypes.html
You can also place a primary key on the arbitrary blobish data eg:
CREATE TABLE Student(id PRIMARY KEY, name)
Its a bit risky cause
INSERT INTO Student(1, "hello")
INSERT INTO Student("1", "hello")
will result in two rows.
If you need a unique constraint on other stuff you can try using the Create Index command
CREATE TABLE Students (
StudentId INTEGER PRIMARY KEY,
Name VARCHAR(80)
)
is one simple way.

Resources