pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Rich Shepard <rshepard@appl-ecosys.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Primary key data type: integer vs identity
Date: Fri, 19 Apr 2019 11:05:09 -0700
Message-ID: <c1df337c-8819-711b-496e-459e7fb88408@aklaver.com> (raw)
In-Reply-To: <alpine.LNX.2.20.1904191050120.24219@salmo.appl-ecosys.com>
References: <alpine.LNX.2.20.1904191050120.24219@salmo.appl-ecosys.com>

On 4/19/19 10:55 AM, Rich Shepard wrote:
> When I created the database I set primary key data types as integer; for
> example:
> 
> Column    |   Type          | Collation | Nullable |  Default
> --------------+-----------------------+-----------+----------+-------------
>   org_id   | integer         |           | not null |
> nextval('organizations_org_id_seq'::regclass)
> 
> I probably should have used the serial type, but didn't.

If you want it to be like serial(assuming the sequences are unique to 
each table) then:

https://www.postgresql.org/docs/11/sql-altersequence.html
"OWNED BY table_name.column_name
OWNED BY NONE

     The OWNED BY option causes the sequence to be associated with a 
specific table column, such that if that column (or its whole table) is 
dropped, the sequence will be automatically dropped as well. If 
specified, this association replaces any previously specified 
association for the sequence. The specified table must have the same 
owner and be in the same schema as the sequence. Specifying OWNED BY 
NONE removes any existing association, making the sequence “free-standing”.
"



> 
> If it would be advisable for me to convert from integer to identity please
> point me to the appropriate work flow.

If it is working for you now I see no reason to switch.

> 
> Reading the CREATE TABLE pages in the manual did not give me sufficient
> insights to appreciate the differences or indicate how to change the
> column's data type.

IDENTITY is a SQL standard.

The difference:

https://www.postgresql.org/docs/11/sql-createtable.html
"GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]

     This clause creates the column as an identity column. It will have 
an implicit sequence attached to it and the column in new rows will 
automatically have values from the sequence assigned to it.

     The clauses ALWAYS and BY DEFAULT determine how the sequence value 
is given precedence over a user-specified value in an INSERT statement. 
If ALWAYS is specified, a user-specified value is only accepted if the 
INSERT statement specifies OVERRIDING SYSTEM VALUE. If BY DEFAULT is 
specified, then the user-specified value takes precedence. See INSERT 
for details. (In the COPY command, user-specified values are always used 
regardless of this setting.)

     The optional sequence_options clause can be used to override the 
options of the sequence. See CREATE SEQUENCE for details.
"

Basically the ALWAYS/DEFAULT choices.

> 
> Regards,
> 
> Rich
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (18+ messages)  latest in thread

Message-ID: <c1df337c-8819-711b-496e-459e7fb88408@aklaver.com>
Permalink:  ../c1df337c-8819-711b-496e-459e7fb88408@aklaver.com/
Also on:    postgresql.org/message-id/c1df337c-8819-711b-496e-459e7fb88408@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, rshepard@appl-ecosys.com
  Subject: Re: Primary key data type: integer vs identity
  In-Reply-To: <c1df337c-8819-711b-496e-459e7fb88408@aklaver.com>
  References: <alpine.LNX.2.20.1904191050120.24219@salmo.appl-ecosys.com> <c1df337c-8819-711b-496e-459e7fb88408@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox