Re: Creating Primary Key after CREATE TABLE: Is Sequence created?

From: John R Pierce <pierce(at)hogranch(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Creating Primary Key after CREATE TABLE: Is Sequence created?
Date: 2013-09-27 19:58:09
Message-ID: 5245E351.3050501@hogranch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9/27/2013 12:40 PM, mdr wrote:
> Is it possible to create a column of type SQL:2011 types (INTEGER or such)
> and then connect a SEQUENCE to it and make that column a PRIMARY KEY -
> without creating a column of type SERIAL?

yes, its just more work.

something like...

CREATE TABLE fred (id integer, val character varying(100))
PRIMARY KEY (id);
CREATE SEQUENCE fred_id_seq OWNED BY fred.id;
ALTER TABLE fred ALTER COLUMN id SET DEFAULT
nextval('fred_id_seq'::regclass);

nextval() and OWNED BY are both postgres extensions.

>
> It seems column of type SERIAL is specific to Postgres and will make my
> script Postgres dependent?
>
> Just in case I decide to go to MySQL. Most likely not, but just wanted to
> manage to SQL:2011.

MySQL uses some kind of nonstandard 'autoincrement' attribute rather
than SEQUENCE

--
john r pierce 37N 122W
somewhere on the middle of the left coast

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Muhammad Bashir Al-Noimi 2013-09-27 20:03:57 Re: Building with MinGW issue
Previous Message Monosij 2013-09-27 19:43:36 Re: Trying to create DB / user to import some data