外键约束不起作用 [英] Foreign Key constraint doesn't work

查看:61
本文介绍了外键约束不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表格,themequiz,这是它们的定义:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆