473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Foreign Keys

I am able to enter a null in a foreign key field in one table(table2)
without the foreign key relationship (table1.t1_id) validating the value.
Why is this?

Below are 2 table's scripts:

create table table1 (
t1_id VARCHAR(32) not null,
t1_desc VARCHAR(32) null,
constraint table1_pkey primary key (t1_id) ) ;
create table table2 (
t2_id VARCHAR(32) not null,
t1_id VARCHAR(32) null,
t2_desc VARCHAR(32) null,
constraint table2_pkey primary key (t2_id) ) ;

create index table2_t1_id
on table2 ( t1_id ) ;

alter table table2
add constraint table1_table2_t1_id_FK1
foreign key ( t1_id )
references table1 ( t1_id )
ON DELETE RESTRICT ON UPDATE CASCADE ;

__________________________________________________ _______________
Is there a gadget-lover on your gift list? MSN Shopping has lined up some
good bets! http://shopping.msn.com
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #1
3 1920
Ben
My understanding of forign keys is that the constraint is only checked
when the values are non-null. I'm not aware of any way to say "keep the
value non-null unless there is a null in the column referenced."

On Fri, 21 Nov 2003, Thomas LeBlanc wrote:
I am able to enter a null in a foreign key field in one table(table2)
without the foreign key relationship (table1.t1_id) validating the value.
Why is this?

Below are 2 table's scripts:

create table table1 (
t1_id VARCHAR(32) not null,
t1_desc VARCHAR(32) null,
constraint table1_pkey primary key (t1_id) ) ;
create table table2 (
t2_id VARCHAR(32) not null,
t1_id VARCHAR(32) null,
t2_desc VARCHAR(32) null,
constraint table2_pkey primary key (t2_id) ) ;

create index table2_t1_id
on table2 ( t1_id ) ;

alter table table2
add constraint table1_table2_t1_id_FK1
foreign key ( t1_id )
references table1 ( t1_id )
ON DELETE RESTRICT ON UPDATE CASCADE ;

__________________________________________________ _______________
Is there a gadget-lover on your gift list? MSN Shopping has lined up some
good bets! http://shopping.msn.com
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #2
On Fri, Nov 21, 2003 at 14:35:54 -0600,
Thomas LeBlanc <th*********@hotmail.com> wrote:
I am able to enter a null in a foreign key field in one table(table2)
without the foreign key relationship (table1.t1_id) validating the value.
Why is this?


That is how foreign keys work. If you don't want null values for the
foreign key, use a not null constraint in addition to the foreign
key constraint.

You shouldn't be using null key values in the referenced table.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #3

On Fri, 21 Nov 2003, Thomas LeBlanc wrote:
I am able to enter a null in a foreign key field in one table(table2)
without the foreign key relationship (table1.t1_id) validating the value.
Why is this?


To go into more detail from the previous answers:
For a single column key, a NULL value is always allowed in the
referencing table (table2) and isn't considered to be referencing
any row in table1.
For a multiple column key, how NULLs are handled depends on the match
type specified. For the unspecified match type/MATCH SIMPLE, if any field
in the referencing table is NULL, the constraint is considered satisified
and doesn't reference any particular row in table1. For MATCH FULL, either
all the columns must be NULL for a row (and the constraint is satisified
and there is no referenced row) or none of the columns may be NULL and
the check is done normally. For MATCH PARTIAL (which we don't implement),
if all columns are NULL the constraint is satisified and there is no
referenced row, if all columns are non-NULL the check is done normally,
if some columns are NULL and others are non-NULL, the non-NULL columns
are checked against the referenced table without concern for matching the
columns where the referencing row has a NULL.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Olivier Crèvecoeur | last post by:
Hello, Excuse me for my poor english. I would kike know if create index on the foreign key it's necessary or if Oracle, are optimized for using foreign key whithout index. Best regards ...
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
1
by: Vinodh Kumar P | last post by:
I understand the number of foreign keys allowed is restricted by the DBMS I use. In a general relational schema design perspective how many foreign keys a table shall have? If I have large number...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
2
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
1
by: rbarber | last post by:
I have to synchronize 2 databases hourly but am having difficulty maintaining foreign key relations. These tables use auto-increment columns as primary keys, with child records in other tables...
1
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.