外键约束不起作用 [英] Foreign Key constraint doesn't work
问题描述
我有两个表格,theme 和 quiz,这是它们的定义:
I have two tables, theme and quiz, here is their definition:
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 的表测验中插入一条记录作为外键时,该记录已成功插入 quiz
表中,尽管主题表中没有记录id = 30
,我的意思是,不应该阻止这个插入,因为我有一个外键约束?
The field id
(the primary key) in the table theme is a Foreign Key
in the quiz table.
When i try to insert a record in the table quiz which contain the value 30 for example as a foreign key, the record is inserted successfully in the quiz
table although there is no record in the theme table with the id = 30
, i mean, wasn't supposed to interdict this insert since i had a Foreign key constraint?
推荐答案
Are you sure 外键支持 是否启用?
Are you sure foreign key support is enabled?
假设库是在启用外键约束的情况下编译的,它仍然必须在运行时由应用程序启用,使用PRAGMA foreign_keys 命令.例如:
Assuming 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;
这篇关于外键约束不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!