Re: Table design - unknown number of column - Mailing list pgsql-general

From Sean Davis
Subject Re: Table design - unknown number of column
Date
Msg-id 200611090813.11651.sdavis2@mail.nih.gov
Whole thread Raw
In response to Table design - unknown number of column  (Richard Ollier <r.ollier@tequila.co.jp>)
List pgsql-general
I think the typical way of attacking a problem would be a second and third
table.  The second table would look like:

flat_type table
  flag_type_id
  flag_type (like the column name in your original table)
  flag_type_description (BONUS:  you can describe each flag)

product_flag table
  product_id (fk to your original table)
  flag_type_id (fk to the flag_type table)
  flag_value (0, 1, or whatever you want to store here)

The advantages with this method are several:
1)  No wasted storage for all those NULLs where a flag is not needed
2)  Should be very fast to lookup by product_id to get all flags
3)  You can expand to an arbitrary number of flags
4)  Your schema remains normalized

Sean

pgsql-general by date:

Previous
From: Harco de Hilster
Date:
Subject: Re: converting Informix outer to Postgres
Next
From: Richard Huxton
Date:
Subject: Re: planer picks a bad plan (seq-scan instead of index)