Re: Is there any different for foreign key to be serial instead of integer

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Yan Cheng Cheok <yccheok(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Is there any different for foreign key to be serial instead of integer
Date: 2010-01-07 04:07:45
Message-ID: dcc563d11001062007n4f3657ffrf89fd0eb4b0c8b7d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jan 6, 2010 at 8:51 PM, Yan Cheng Cheok <yccheok(at)yahoo(dot)com> wrote:
> I came across a lot of similar example for foreign key
>
> CREATE TABLE orderinfo
> (
> orderinfo_id serial ,
> customer_id integer NOT NULL,
> date_placed date NOT NULL,
> date_shipped date ,
> shipping numeric(7,2) ,
> CONSTRAINT orderinfo_pk PRIMARY KEY(orderinfo_id),
> CONSTRAINT orderinfo_customer_id_fk FOREIGN KEY(customer_id) REFERENCES
> customer(customer_id)
> );
>
> instead of let customer_id being type as integer, can i let it be serial? is there any difference?
>
> if the table referenced by customer_id is having primary key typed big serial, customer_id shall be declared as bigint ?

serial and big serial are basically syntactic sugar for creating the
table with an int / bigint, create a sequence, create a default for
the bigint field, and setting a dependency in the system catalogs for
the sequence to the table. So, yep, a serial / bigserial is
equivalent to int / bigint from an FK point of view.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Broersma 2010-01-07 04:08:54 Re: Is there any different for foreign key to be serial instead of integer
Previous Message Yan Cheng Cheok 2010-01-07 03:51:28 Is there any different for foreign key to be serial instead of integer