Does SQLite3 not support foreign key constraints?(SQLite3 不支持外键约束吗?)
问题描述
我在使用 SQLITE3 时遇到问题.
I am have a problem with SQLITE3.
我使用以下 SQL 脚本创建了 2 个表 persons 和 orders:
I have created 2 tables persons and orders using the following SQL script:
sqlite> create table Persons(
P_Id int primary key,
LastName varchar,
FirstName varchar,
Address varchar,
City varchar
);
sqlite> create table Orders(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
);
sqlite> insert into Orders values(1,77895,3);
sqlite> select * from Orders;
1|77895|3
sqlite>
即使persons表是空行也可以插入到orders表中.
Even though the persons table is empty rows can be inserted into the orders table.
它没有显示任何错误.
这怎么可能.
推荐答案
在 SQLite 3.x 中,您必须每次连接到 SQLite 数据库时进行以下查询:
In SQLite 3.x, you have to make the following query every time you connect to an SQLite database:
PRAGMA foreign_keys = ON;
否则 SQLite 将忽略所有外键约束.
Otherwise SQLite will ignore all foreign key constraints.
为什么每次都这样?根据文档,向后兼容 SQLite 2.x.
Why every time? Backwards compatibility with SQLite 2.x, according to the the documentation.
在 SQLite 4.x 中,FK 约束将默认启用.
In SQLite 4.x, FK constraints will be enabled by default.
这篇关于SQLite3 不支持外键约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQLite3 不支持外键约束吗?
基础教程推荐
- 带有WHERE子句的LAG()函数 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
