To show correctly PRIMARY KEY in Postgres
-
05-07-2019 - |
Question
Which one of the following ways would you use in declaring Primary Keys by Postgres?
#1
CREATE TABLE user(
user_id PRIMARY KEY,
...
)
#2
CREATE TABLE user(
user_id NOT NULL,
...
CONSTRAINT user_pk PRIMARY KEY(user_id);
)
Solution
I would use method #1.
- The indication of which column is the primary key is kept closer to the actual column definition
- You don't have to think up a name for the constraint; a name will be automatically generated
One reason to use method #2 is if your primary key were to span more than one column. In that case, method #1 won't work because it only supports a single column primary key.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow