Home > Database > Mysql Tutorial > Why Do SQLite3 Foreign Key Constraints Need to Be Enabled Explicitly?

Why Do SQLite3 Foreign Key Constraints Need to Be Enabled Explicitly?

DDD
Release: 2025-01-05 00:38:40
Original
873 people have browsed it

Why Do SQLite3 Foreign Key Constraints Need to Be Enabled Explicitly?

SQLite3 Foreign Key Constraints

While using SQLite3, you may encounter an issue where rows can be inserted into a child table even when the parent table is empty. This behavior arises due to SQLite3's default behavior, where foreign key constraints are disabled upon connection.

To enable foreign key constraints in SQLite3, execute the following query every time you connect to the database:

PRAGMA foreign_keys = ON;
Copy after login

The reason for repeating this command every time is to maintain compatibility with SQLite version 2.x. By default, SQLite3 behaves like SQLite 2.x, which ignores foreign key constraints.

In SQLite version 4.x, however, foreign key constraints will be enabled by default, resolving this issue permanently.

The above is the detailed content of Why Do SQLite3 Foreign Key Constraints Need to Be Enabled Explicitly?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template