我有两张桌子,主题和测验,这里是他们的定义:
CREATE TABLE "theme" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"nom" VARCHAR NOT NULL ); CREATE TABLE quiz( id INTEGER PRIMARY KEY,nom VARCHAR(256) NOT NULL,theme INTEGER NOT NULL,niveau INTEGER NOT NULL,pass INTEGER DEFAULT 1 NOT NULL,jok INTEGER DEFAULT 1 NOT NULL,etat INTEGER DEFAULT 0 NOT NULL,FOREIGN KEY (theme) REFERENCES theme(id) );
表格主题中的字段ID(主键)是测验表中的外键。
当我尝试在包含值30的表格测验中插入记录,例如作为外键时,尽管在id = 30的主题表中没有记录,但记录已成功插入到测验表中,我的意思是,不应该阻止这个插入,因为我有一个外键约束?
您确定启用了
foreign key support吗?
原文链接:https://www.f2er.com/sqlite/197925.htmlAssuming the library is compiled with foreign key constraints enabled,
it must still be enabled by the application at runtime,using the
PRAGMA foreign_keys command. For example:
sqlite> PRAGMA foreign_keys = ON;