dbTalk Databases Forums  

Array values and foreign keys

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Array values and foreign keys in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Daniel Savard
 
Posts: n/a

Default Array values and foreign keys - 10-27-2004 , 09:19 AM






Is there a way to define a foreign key for the values of an array?

For example, if table T1 is having a colum A which is defined as integer[] can I define a foreign key in order to force each value to be a pointer (index) to a row in a table T2?

If yes, how? Is there any shortcomings to this approach?

Thanks,

Daniel Savard

---
Daniel Savard
dsavard (AT) cids (DOT) ca

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org


Reply With Quote
  #2  
Old   
Pierre-Frédéric Caillaud
 
Posts: n/a

Default Re: Array values and foreign keys - 10-27-2004 , 10:59 AM








You can't express it directly with a CHECK constraint but you can do this
:

- add CHECK( test_array( yourcolumn )) in your table definition
- create function test_array which takes an array and looks if all its
elements are in your table T2, I do something like comparing the length of
the array to SELECT count(1) FROM T2 WHERE key IN array
You can do it other ways but you'll have to use a function.


On Wed, 27 Oct 2004 10:19:02 -0400, Daniel Savard <dsavard (AT) cids (DOT) ca> wrote:

Quote:
Is there a way to define a foreign key for the values of an array?

For example, if table T1 is having a colum A which is defined as
integer[] can I define a foreign key in order to force each value to be
a pointer (index) to a row in a table T2?

If yes, how? Is there any shortcomings to this approach?

Thanks,

Daniel Savard

---
Daniel Savard
dsavard (AT) cids (DOT) ca

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #3  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: Array values and foreign keys - 10-27-2004 , 01:01 PM



On Wed, Oct 27, 2004 at 05:59:46PM +0200, Pierre-Fr?d?ric Caillaud wrote:
Quote:
- add CHECK( test_array( yourcolumn )) in your table definition
- create function test_array which takes an array and looks if all
its elements are in your table T2, I do something like comparing the
length of the array to SELECT count(1) FROM T2 WHERE key IN array
This provides only partial foreign key checking: depending on how
the application works, you might also need to ensure that updates
and deletes in T2 don't break the references in T1.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #4  
Old   
Daniel Savard
 
Posts: n/a

Default Re: Array values and foreign keys - 10-27-2004 , 02:44 PM



Le mer 27/10/2004 à 11:59, Pierre-Frédéric Caillaud a écrit :
Quote:

You can't express it directly with a CHECK constraint but you can do this
:

- add CHECK( test_array( yourcolumn )) in your table definition
- create function test_array which takes an array and looks if all its
elements are in your table T2, I do something like comparing the length of
the array to SELECT count(1) FROM T2 WHERE key IN array
You can do it other ways but you'll have to use a function.


Fine. I got it right after fiddling a little bit. The function is
something like:

CREATE FUNCTION test_array (smallint[]) RETURNS bool AS '
select case when count(1) = array_upper($1,1) then true
else false
end from t2 where cle = any($1);
' LANGUAGE SQL;

It compares the length of the array to the number of elements actually
found in the reference table.

--

=======================================
Daniel Savard

=======================================



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.