pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ray O'Donnell <ray@rodonnell.ie>
To: Rino Mardo <rino19ny@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: newbie db design question
Date: Sat, 11 Jun 2022 09:43:46 +0100
Message-ID: <18151eefdd0.2834.f9dd809031fc0469edf0bbbf79c1d468@rodonnell.ie> (raw)
In-Reply-To: <CAGnasUXDC2R4juLfXrJ=P2fsnPZMt_M6tGdtZ=BEp+0j1=FxMQ@mail.gmail.com>
References: <CAGnasUXDC2R4juLfXrJ=P2fsnPZMt_M6tGdtZ=BEp+0j1=FxMQ@mail.gmail.com>

On 11 June 2022 08:05:41 Rino Mardo <rino19ny@gmail.com> wrote:
> hello!
>
> really noob question here. i have this experimental database design:
>
> create table products (
> product_id serial primary key,
> description text,
> supplier_id????) ;
>
> create table supplier (
> supplier_id serial primary key,
> description text) ;
>
>
> the products table should be linked to the supplier table via "supplier_id" 
> column. i can't find out what would be the data type of supplier_id in 
> table products to do that. i tried

Hi there,

"serial" isn't a real type - it's just a shortcut for "integer not null 
default nextval(....)". Read about it here:

https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL

So the supplier_id column in the first table should be of type "integer".

>
> supplier_id serial primary key references supplier
>
> but it won't allow multiple primary key.
>
> how then to proceed?


You need a foreign key.... add this to the first table:

constraint <constraint name> foreign key (supplier_id) references 
supplier(supplier_id)

I hope this helps.

Ray.


>
>
> regards,

view thread (5+ messages)  latest in thread

Message-ID: <18151eefdd0.2834.f9dd809031fc0469edf0bbbf79c1d468@rodonnell.ie>
Permalink:  ../18151eefdd0.2834.f9dd809031fc0469edf0bbbf79c1d468@rodonnell.ie/
Also on:    postgresql.org/message-id/18151eefdd0.2834.f9dd809031fc0469edf0bbbf79c1d468@rodonnell.ie

 · 

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: ray@rodonnell.ie, rino19ny@gmail.com
  Subject: Re: newbie db design question
  In-Reply-To: <18151eefdd0.2834.f9dd809031fc0469edf0bbbf79c1d468@rodonnell.ie>
  References: <CAGnasUXDC2R4juLfXrJ=P2fsnPZMt_M6tGdtZ=BEp+0j1=FxMQ@mail.gmail.com> <18151eefdd0.2834.f9dd809031fc0469edf0bbbf79c1d468@rodonnell.ie>

* 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