What are some common constraints used in MySQL relationships and how are they utilized in PHP?

One common constraint used in MySQL relationships is the foreign key constraint, which ensures that values in a column of one table correspond to values in a column of another table. This helps maintain referential integrity between related tables. In PHP, foreign key constraints can be utilized when creating tables using SQL queries to establish relationships between tables.

// Creating a table with a foreign key constraint in PHP
$sql = "CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    product_id INT,
    quantity INT,
    FOREIGN KEY (product_id) REFERENCES products(product_id)
)";
$result = mysqli_query($conn, $sql);
if($result){
    echo "Table created successfully with foreign key constraint.";
} else {
    echo "Error creating table: " . mysqli_error($conn);
}