views:

124

answers:

1

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);
)
+1  A: 

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.

Greg Hewgill
I agree with you! - Thank you for your answer!
Masi