What is the best way to establish relationships between tables in SQLite when using PHP?

To establish relationships between tables in SQLite when using PHP, you can use foreign keys. Foreign keys are used to link one table to another based on a column that they have in common. By defining foreign keys in your SQLite tables, you can ensure referential integrity and maintain the relationships between the tables.

<?php
// Establish a connection to the SQLite database
$db = new SQLite3('your_database.db');

// Enable foreign key support in SQLite
$db->exec('PRAGMA foreign_keys = ON;');

// Create tables with foreign key constraints
$db->exec('CREATE TABLE table1 (
    id INTEGER PRIMARY KEY,
    name TEXT
);');

$db->exec('CREATE TABLE table2 (
    id INTEGER PRIMARY KEY,
    table1_id INTEGER,
    FOREIGN KEY (table1_id) REFERENCES table1(id)
);');
?>