PRAGMA foreign_key_check; - sqlite

I am trying to use PRAGMA foreign_key_check; without success :(
I have made some foreign keys violations on a database (with PRAGMA foreign_keys = OFF;), then I set this to true then launched PRAGMA foreign_key_check; but this did not return any results.
However, when I try to insert the very same lines with foreign keys violations with PRAGMA foreign_keys = ON; I do get a foreign key constraint violation error.
I am using SQLite 3.7.3.
Is there something I am missing regarding this command? Is there a bug?
Thanks
EDIT: I have just tried with 3.8.3.1 (higher version this time ;) ) but cannot get any results though :(
However, some other commands seem to not work as expected (especially .schema !?):
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> attach "D:\MusicLib_Minimal_TEST_FOREIGN_KEYS.db" as db1;
sqlite> .schema device
sqlite> PRAGMA foreign_keys=ON;
sqlite> PRAGMA foreign_keys;
1
sqlite> PRAGMA foreign_key_check;
sqlite> select * from device
...> ;
1|test|/|/|0|0
sqlite> select * from playlist
...> ;
sqlite> PRAGMA integrity_check;
ok
sqlite> .dump device
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
COMMIT;
sqlite> PRAGMA table_info(device);
0|idDevice|INTEGER|1||1
1|name|TEXT|1||0
2|source|TEXT|1||0
3|destination|TEXT|1||0
4|idPlaylist|INTEGER|1||0
5|idMachine|INTEGER|1||0
sqlite>

PRAGMA foreign_key_check was added in sqlite 3.7.16. You have an older version.
Unknown pragmas are just no-ops; no errors are emitted.

I have found out the solution :) Problem was a misuse (or bug ?) of sqlite3.exe
When calling sqlite3.exe without parameters, a main empty database is created, and looks like the commands does not apply the attached db:
D:\>sqlite3.exe
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> attach "D:\MusicLib_Minimal_TEST_FOREIGN_KEYS.db" as db1
...> ;
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main
2 db1 D:\MusicLib_Minimal_TEST_FOREIGN_KEYS.db
sqlite> .tables
db1.device db1.libType db1.optiontype db1.playlist
db1.file db1.machine db1.path db1.statement
db1.genre db1.option db1.playCounter db1.statsource
sqlite> .schema db1.device
sqlite> .exit
If opening database as an argument of sqlite3.exe, it works as expected:
D:\>sqlite3.exe MusicLib_Minimal_TEST_FOREIGN_KEYS.db
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main D:\MusicLib_Minimal_TEST_FOREIGN_KEYS.db
sqlite> .tables
device genre machine optiontype playCounter statement
file libType option path playlist statsource
sqlite> .schema device
CREATE TABLE "device" (
"idDevice" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" TEXT NOT NULL,
"source" TEXT NOT NULL,
"destination" TEXT NOT NULL,
"idPlaylist" INTEGER NOT NULL,
"idMachine" INTEGER NOT NULL,
FOREIGN KEY(idPlaylist) REFERENCES playlist(idPlaylist),
FOREIGN KEY(idMachine) REFERENCES machine(idMachine)
);
sqlite> PRAGMA foreign_keys;
0
sqlite> PRAGMA foreign_keys=ON;
sqlite> PRAGMA foreign_keys;
1
sqlite> PRAGMA foreign_key_check;
device|1|machine|0
device|1|playlist|1
sqlite> select * from device
...> ;
1|test|/|/|0|0
sqlite> select * from playlist
...> ;
sqlite> select * from machine
...> ;

Related

How to select from sqlite table and dump to .sql file?

I am trying to select records from a SQLite.db3 table and create a new insert.sql file.
these are the commands that I have used:
SQLite> .mode insert
SQLite> .output newFile.sql
SQLite> .dump
The problem I have is that I do not want to copy the entire db into newFile.sql. I only want to certain data from a table like: select * from table1 where status = 1
I have found a working solution:
sqlite> .open Corrupt.db3
sqlite> .mode insert
sqlite> .output NewTable.sql
sqlite> select * from OldTable where Status = 1;
sqlite> .output stdout

pragma busy_timeout not working

I am trying to view and change the busy_timeout parameter in a SQLite database, but I can not view the output from pragma busy_timeout.
sqlite> PRAGMA busy_timeout = 1000;
sqlite> PRAGMA busy_timeout;
sqlite>
What am I doing wrong here?
SQLite version 3.7.10
sqlite> pragma compile_options;
ENABLE_FTS3
ENABLE_RTREE
TEMP_STORE=1
THREADSAFE=1
PRAGMA busy_timeout was added in version 3.7.15.

how to get the ordered query in sqlite?

I insert some data into test table.
C:\> sqlite3 e:\\test.db
sqlite> create table test(code TEXT,content numeric);
sqlite> insert into test(code,content)values('x',3);
sqlite> insert into test(code,content)values('x',1.5);
sqlite> insert into test(code,content)values('x',1);
sqlite> insert into test(code,content)values('y',9);
sqlite> insert into test(code,content)values('y',3);
sqlite> insert into test(code,content)values('y',2);
sqlite> insert into test(code,content)values('y',12.3);
sqlite> insert into test(code,content)values('y',11.5);
how can i get the ordered output as the following ?
select * from test group by code order by content;can not get it.
select * from test order by content;neither.
x|1
x|1.5
x|3
y|2
y|3
y|9
y|11.5
y|12.3
You can sort by multiple criteria:
SELECT * FROM test ORDER BY code, content;

Is it allowed to have null foreign key in sqlite when PRAGMA foreign_keys=ON?

according to this null foreign keys are allowed unless and until we are adding the appropriate "NOT NULL" constraint to the schema.
but I am seeing the some different behavior,
sqlite> PRAGMA Foreign_keys;
1
sqlite> create table proc (
sqlite> pid integer,
sqlite> name text,
sqlite> ppid integer,
sqlite> foreign key (ppid) references proc (id)
sqlite> );
sqlite> .schema proc
CREATE TABLE proc (
pid integer,
name text,
ppid integer,
foreign key (ppid) references proc (id)
);
sqlite> insert into proc (pid, name, ppid)
sqlite> values (0, "init", null);
Error: foreign key mismatch
sqlite> PRAGMA Foreign_keys=OFF;
sqlite> PRAGMA Foreign_keys;
0
sqlite> insert into proc (pid, name, ppid)
sqlite> values (0, "init", null);
sqlite> select * from proc;
0|init|
how can I allow null foreign key in sqlite when PRAGMA foreign_keys=ON? or it is not possible at all?
The ID column is named pid, not id.
The parent key column must have a UNIQUE or PRIMARY KEY constraint.
Try adding a foreign key clause by changing your table create statement to:
CREATE TABLE proc (pid integer, name text, ppid integer, foreign key (ppid) references
proc (id) ON UPDATE CASCADE ON DELETE SET NULL);

Not empty string constraint in SQLite

Can I create a database constraint on a TEXT column in SQLite disallowing the value of the column to be empty string ""?
I want to allow the column to be null, but disallow empty string.
Yes you can:
sqlite> create table foo (bar TEXT, CHECK(bar <> ''));
sqlite> insert into foo values (NULL);
sqlite> insert into foo values ('bla');
sqlite> insert into foo values ('');
Error: constraint failed
You can use a CHECK constraint (http://www.sqlite.org/lang_createtable.html):
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> create table example(col, CHECK (col is null or length(col) > 0));
sqlite> insert into example values ('');
SQL error: constraint failed
sqlite> insert into example values (null);
sqlite> insert into example values ('sample');
sqlite> .nullvalue NULL
sqlite> select col from example;
NULL
sample
As far as i know doesn't exist a similar constraint in SQLite, but maybe you can workaround with a Trigger that on INSERT and/or UPDATE automatically change the string empty in NULL.

Resources