Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Foriegn Key and Null

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2007-10-10 : 16:07:15
If you create a field and it's a foriegn key, can it be null?

For instance:

LocationID INT,
CONSTRAINT FK_LocationID_MyTable FOREIGN KEY (ID)
REFERENCES Location (LocationID),

So, if there is no locationID yet, would the record fail?

Thanks,

Zath

Kristen
Test

22859 Posts

Posted - 2007-10-10 : 16:14:09
"If you create a field and it's a foriegn key, can it be null?"

If you have a LocationID then a record must exist in Location.

But LocationID IS NULL is valid (assuming that LocationID column allows NULLs)

Kristen
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2007-10-10 : 16:21:58
Yes, the field will allow nulls.

So insert won't fail.

Thanks,

Zath
Go to Top of Page
   

- Advertisement -