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


基础教程推荐
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01