Use multiple columns as the logical primary key.
Section: constraints and keys
Create a table with a composite primary key
sql
sql
CREATE TABLE user_roles (
user_id BIGINT UNSIGNED NOT NULL,
role_code VARCHAR(50) NOT NULL,
granted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, role_code)
) ENGINE=InnoDB;Explanation
Composite keys are common in mapping or junction tables.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing constraints and keys
Create a table with a foreign key
Reference a parent table and enforce relational integrity.
Create a users table
Create a table with an auto-increment primary key and timestamps.