Sobes.tech
Junior — Middle

Can the PRIMARY KEY be considered as a constraint consisting of multiple columns?

sobes.tech AI

Answer from AI

Yes, a PRIMARY KEY in a database can consist of multiple columns — this is called a composite (or concatenated) primary key. Such a key guarantees the uniqueness of the combination of values in the specified columns.

For example, if a table stores information about orders, where uniqueness is determined not by a single field but by a combination of order_id and product_id, a composite PRIMARY KEY can be declared:

CREATE TABLE orders (
    order_id INT,
    product_id INT,
    quantity INT,
    PRIMARY KEY (order_id, product_id)
);

Thus, the PRIMARY KEY constraint can include multiple columns, ensuring uniqueness across their combination.