pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
foreign key question
3+ messages / 2 participants
[nested] [flat]

* foreign key question
@ 2011-01-05 09:53 Gary Stainburn <gary.stainburn@ringways.co.uk>
  2011-01-05 10:40 ` Re: foreign key question Gary Stainburn <gary.stainburn@ringways.co.uk>
  0 siblings, 1 reply; 3+ messages in thread

From: Gary Stainburn @ 2011-01-05 09:53 UTC (permalink / raw)
  To: pgsql-sql

Hi folks,

I have a table which lists facilities and another table that lists access 
levels for those facilities. All straight forward using a foreign key set up 
using a normal references clause.

users=# select f_id, f_desc from facilities order by f_id;
 f_id |       f_desc                                                                                                                                                                                                                         
------+---------------------                                                                                                                                                                                                                 
    1 | Login                                                                                                                                                                                                                                
    3 | Users                                                                                                                                                                                                                                
.... 
   16 | Itinerary
....
31 rows)

users=# select * from facility_levels ;
 fl_f_id | fl_level |        fl_desc        
---------+----------+-----------------------
      16 |        1 | Own itinerary
      16 |        2 | Dealer/Dept itinerary
      16 |        3 | Dept itinerary
      16 |        4 | Dealer/Dept On/Off
      16 |        5 | Dept On/Off
      16 |        6 | All On/Off
      16 |        7 | All features
(7 rows)

users=# 

Now I want to set up a new access level table specific to the itinerary, along 
the lines of

u_id int4 not null references users(u_id)
fl_level int4 not null references facility_levels(16, fl_level)

Firstly, is this possible, and secondly how would I do it?
-- 
Gary Stainburn
I.T. Manager
Ringways Garages
http://www.ringways.co.uk 



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: foreign key question
  2011-01-05 09:53 foreign key question Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2011-01-05 10:40 ` Gary Stainburn <gary.stainburn@ringways.co.uk>
  2011-01-05 19:54   ` Re: foreign key question Jasen Betts <jasen@xnet.co.nz>
  0 siblings, 1 reply; 3+ messages in thread

From: Gary Stainburn @ 2011-01-05 10:40 UTC (permalink / raw)
  To: pgsql-sql

On Wednesday 05 January 2011 09:53:43 Gary Stainburn wrote:
> Now I want to set up a new access level table specific to the itinerary,
> along the lines of
>
> u_id int4 not null references users(u_id)
> fl_level int4 not null references facility_levels(16, fl_level)
>
> Firstly, is this possible, and secondly how would I do it?

I've managed a work-around by creating a column that defaults to 16 and then 
used ALTER TABLE ADD CONSTRAINT to add a foreign key with two pairs of keys.

This feels wrong though as my table now has a column that is ultimately 
redundant, and worse can be changed to a wrong value.

Ok, I've sorted the last bit by adding a check constraint to make sure it 
always contains 16, but it still feels wrong.


-- 
Gary Stainburn
I.T. Manager
Ringways Garages
http://www.ringways.co.uk 



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: foreign key question
  2011-01-05 09:53 foreign key question Gary Stainburn <gary.stainburn@ringways.co.uk>
  2011-01-05 10:40 ` Re: foreign key question Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2011-01-05 19:54   ` Jasen Betts <jasen@xnet.co.nz>
  0 siblings, 0 replies; 3+ messages in thread

From: Jasen Betts @ 2011-01-05 19:54 UTC (permalink / raw)
  To: pgsql-sql

On 2011-01-05, Gary Stainburn <gary.stainburn@ringways.co.uk> wrote:
> On Wednesday 05 January 2011 09:53:43 Gary Stainburn wrote:
>> Now I want to set up a new access level table specific to the itinerary,
>> along the lines of
>>
>> u_id int4 not null references users(u_id)
>> fl_level int4 not null references facility_levels(16, fl_level)
>>
>> Firstly, is this possible, and secondly how would I do it?
>
> I've managed a work-around by creating a column that defaults to 16 and then 

> used ALTER TABLE ADD CONSTRAINT to add a foreign key with two pairs of keys.
>
> This feels wrong though as my table now has a column that is ultimately 
> redundant, and worse can be changed to a wrong value.
>
> Ok, I've sorted the last bit by adding a check constraint to make sure it 
> always contains 16, but it still feels wrong.

it feels wrong that's because it's not normalised,

the column with the 16's probably should not be there.

or possibly it should have rows with other values too.

look at how this table is useful and look for a more general way to
do it.


-- 
⚂⚃ 100% natural



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2011-01-05 19:54 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-01-05 09:53 foreign key question Gary Stainburn <gary.stainburn@ringways.co.uk>
2011-01-05 10:40 ` Gary Stainburn <gary.stainburn@ringways.co.uk>
2011-01-05 19:54   ` Jasen Betts <jasen@xnet.co.nz>

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