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 不支持外键约束吗?


- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01