dbTalk Databases Forums  

can a foreign key allow null

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss can a foreign key allow null in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Keith Worthington
 
Posts: n/a

Default can a foreign key allow null - 10-19-2004 , 02:01 PM






Hi All,

I have two tables.

MYDB=# \d myschema.tbl_part
Table "myschema.tbl_part"
Column | Type | Modifiers
----------------------+------------------------+-----------
id | character varying(20) | not null
description | character varying(30) | not null
class | smallint | not null
inactive | boolean | not null
sales_description | character varying(160) |
purchase_description | character varying(160) |
last_unit_cost | real | not null
costing_method | smallint | not null
sales_gl_account | character varying(15) |
inventory_gl_account | character varying(15) |
cogs_gl_account | character varying(15) |
type | character varying(8) |
unit_of_measure | character varying(6) |
weight | real |
reorder_point | real |
reorder_quantity | real |
Indexes: tbl_part_pkey primary key btree (id)

MYDB=# \d myschema.tbl_gl_account
Table "myschema.tbl_gl_account"
Column | Type | Modifiers
-------------+-----------------------+-----------
number | character varying(15) | not null
description | character varying(30) |
type | smallint | not null
inactive | boolean | not null
Indexes: tbl_gl_account_pkey primary key btree (number)

MYDB=#

Is it possible to create a foreign key such that only null and values from
myschema.tbl_gl_account.number are allowed in myschema.tbl_part.sales_gl_account?

Kind Regards,
Keith

______________________________________________
99main Internet Services http://www.99main.com


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #2  
Old   
Steven Klassen
 
Posts: n/a

Default Re: can a foreign key allow null - 10-19-2004 , 02:12 PM






* Keith Worthington <keithw (AT) narrowpathinc (DOT) com> [2004-10-19 15:01:40 -0400]:

Quote:
Table "myschema.tbl_part"
Column | Type | Modifiers
----------------------+------------------------+-----------
sales_gl_account | character varying(15) |

Table "myschema.tbl_gl_account"
Column | Type | Modifiers
-------------+-----------------------+-----------
number | character varying(15) | not null

Is it possible to create a foreign key such that only null and
values from myschema.tbl_gl_account.number are allowed in
myschema.tbl_part.sales_gl_account?
How is this any different than a regular foreign key constraint?

ALTER TABLE tbl_part ADD CONSTRAINT account_number_exists FOREIGN KEY
(sales_gl_account) REFERENCES tbl_gl_account(number);

HTH,

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564

---------------------------(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.