How can a composite unique key be used in MySQL to ensure that a row only appears once?

To ensure that a row only appears once in MySQL, you can use a composite unique key. This key consists of multiple columns that, when combined, must be unique for each row. By creating a composite unique key on the desired columns, MySQL will enforce this uniqueness constraint, preventing duplicate rows from being inserted.

// Create a table with a composite unique key
$sql = "CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50),
    email VARCHAR(100),
    UNIQUE KEY unique_user_email (username, email)
)";
mysqli_query($conn, $sql);