Last answered:

31 Aug 2022

Posted on:

22 Oct 2021

1

how can you apply primary keys in multiple columns

It was said in the video that the similarity between the two types (primary and unique) of constraints is that both can be applied to multiple columns, not just a single column. So how do you apply a primary key if there can be only one primary key in a table?

3 answers ( 0 marked as helpful)
Instructor
Posted on:

24 Oct 2021

2

Hi Edu!

Thanks for reaching out.

There's the possibility to create a composite primary key, where not just a single column, but multiple columns define the uniqueness of the primary key.
For instance, if a single column determines the primary key, all values in the column must be different.
E.g. (id_001), (id_002) etc.
Otherwise, if you are using a composite key, the id column may be combined with, e.g. the date column, so the primary keys become of the following type:
(id_001, '2021-10-24'), (id_001, '2021-10-25'), (id_002, '2021-10-24'), (id_002, '2021-10-30') etc.

Hope this helps.
Best,
Martin

Posted on:

29 Aug 2022

0

Hello,

how do we combine two columns to primary key ? Is there a specific format to define composite primary key?

tx

Instructor
Posted on:

31 Aug 2022

1

Hi Anuradha!
Thanks for reaching out.

When you define your table the syntax for a composite primary key is the following:
PRIMARY KEY (column_1, column_2, ...)

Hope this helps.
Best,
Tsvetelin

Submit an answer