Primary key does not allow unique replace functionality - sqlite

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
);

Related

Can not add a second foreign key when creating table

I am creating a table with 2 foreign keys
but whenever I have the second key, it will return an error:
CREATE TABLE reviews(
id INTEGER PRIMARY KEY,
stars INT,
business_id INT,
FOREIGN KEY(business_id) REFERENCES businesses(id),
user_id INT,
FOREIGN KEY (user_id) REFERENCES users(id)
);
It will throw syntax error near user_id, and if I put business_id after user_id, it will throw syntax error near business_id...
And if I only put one foreign key there, it will just create the table, tried several times. What's the problem here?
users and businesses are two tables, I'm creating a junction table for them.
Don't mix column definition with constraint definition. Columns first, constraints after:
CREATE TABLE reviews(
id INTEGER PRIMARY KEY,
stars INT,
business_id INT,
user_id INT,
FOREIGN KEY(business_id) REFERENCES businesses(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);

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

What's wrong with this query?

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
);

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