SQLite - 外键约束 - IO 5 [英] SQLite - Foreign Keys Contraints - IOs 5

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

问题描述

似乎从 SQLite 3.6.x 版开始就支持外键约束.IOS5.0上的SQLite版本是3.7.7(在sqlite3.h中找到).

it seems that Foreign Keys Constraints are supported since version 3.6.x in SQLite. The version of SQLite on IOS5.0 is 3.7.7 (found in sqlite3.h).

但是当我尝试在有约束的表中插入一行时,即使相关的外键不存在,我的行也会被正确插入.我没有错误.

But when I try to insert a row in a table that has a constraint, my row is correctly inserted even if the related foreign key is not existing. I have no error.

使用 Navicat 之类的应用程序执行相同的插入语句会给我一个违反约束错误"

Doing the same insert statement using apps like Navicat gives me a "Constraint violation error"

您知道 IO 5 是否支持外键吗?

Do you know if foreign keys are supported on IOs 5 ?

这是数据库架构:

CREATE TABLE artist(
  artistid    INTEGER PRIMARY KEY, 
  artistname  TEXT
)

CREATE TABLE "track" (
     "trackid" INTEGER PRIMARY KEY AUTOINCREMENT,
     "trackname" TEXT,
     "trackartist" INTEGER,
    CONSTRAINT "trackartist" FOREIGN KEY ("trackartist") REFERENCES "artist" ("artistid") ON DELETE CASCADE ON UPDATE CASCADE)

真的很简单,不是吗?

谢谢伊曼纽尔

推荐答案

默认禁用外键.您必须为每个连接分别启用它们.该设置不是粘性".每次连接到 SQLite 数据库时都必须这样做.

Foreign keys are disabled by default. You have to enable them separately for each connection. The setting isn't "sticky". You have to do this every time you connect to a SQLite database.

PRAGMA foreign_keys = ON;

很有可能 Navicat 会为您处理这些.在您自己的代码中,这是您的工作.

Odds are good that Navicat takes care of that for you. In your own code, it's your job.

这篇关于SQLite - 外键约束 - IO 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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