Skip to content
PHP Logo

How does the process of creating relationships between tables differ when using InnoDB compared to manual indexing in PHP?

When using InnoDB, relationships between tables can be created using foreign keys, which ensures referential integrity and helps maintain data consistency. On the other hand, when manually indexing in PHP, you would need to handle the relationship logic yourself, which can be more error-prone and time-consuming.

php
// Creating a relationship between tables using foreign keys in InnoDB
$sql = "ALTER TABLE orders ADD CONSTRAINT fk_customer_id FOREIGN KEY (customer_id) REFERENCES customers(id)";
$result = mysqli_query($connection, $sql);

if($result){
    echo "Foreign key constraint added successfully";
} else {
    echo "Error adding foreign key constraint: " . mysqli_error($connection);
}