Can SQL table have multiple columns with primary key?
The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
PRIMARY KEY can't have null values. A table can have only one PRIMARY KEY either on one column or multiple columns. When multiple columns are defined as PRIMARY KEY, then, it is called COMPOSITE KEY.
Usually, a primary key is just a single column that uniquely identifies a row within a table. However, a composite primary key consisting of 2 or more columns can be created.
Answer: No it is not possible. The wording Primary Key imply non duplicate values, it is by design ! You can have duplicate values in Non Primary Key, it is the only way.
A table can have just one primary key constraint, but that may be comprised of several columns e.g.
create table my_table (col1 integer, col2 integer, col3 integer, primary key (col1, col2, col3) );
In addition to the primary key, a table may also have one or more UNIQUE constraints, e.g.
create table my_table2 (col1 integer, col2 integer, col3 integer, primary key (col1, col2), unique (col2, col3) );
If you mean "can a primary key in SQL have multiple columns", the answer is yes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With