PRAGMA foreign_key_check;

时间:2014-02-13 11:59:44

标签: sqlite

我试图使用PRAGMA foreign_key_check;但没有成功:(

我在数据库上发生了一些外键违规(使用PRAGMA foreign_keys = OFF;),然后我将其设置为true,然后启动PRAGMA foreign_key_check;,但这并没有返回任何结果。

但是,当我尝试使用PRAGMA foreign_keys = ON;插入与外键冲突相同的行时,我会收到外键约束违规错误。

我正在使用SQLite 3.7.3。

关于这个命令我有什么遗漏吗?有错误吗?

由于

编辑:我刚试过3.8.3.1(这次是更高版本;))但是不能得到任何结果:(

但是,其他一些命令似乎无法按预期工作(特别是.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>

2 个答案:

答案 0 :(得分:3)

PRAGMA foreign_key_checkadded in sqlite 3.7.16。你有an older version

未知的pragma只是无操作;没有发出错误。

答案 1 :(得分:0)

我找到了解决方案:)问题是sqlite3.exe的误用(或错误?)

在没有参数的情况下调用sqlite3.exe时,会创建一个主空数据库,看起来命令不会应用附加的数据库:

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

如果打开数据库作为sqlite3.exe的参数,它将按预期工作:

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