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)
 Foreign Keys - Allow zero?

Author  Topic 

Sunkist
Starting Member

4 Posts

Posted - 2008-06-08 : 04:28:51
I am creating a series of foreign keys in my new database, and so far everything is going fine. The company I work for never uses them, and I am working on my own stuff on my own time now.

I come across one table that I do want one field to allow zeroes, but when its set to a value, to exist in the other table.

To be more specific. I have a PO type table, which has an EmployeeID of the person who made the PO, and another field to store who received the PO when the order arrives. I want that 2nd Employee field to be zero until it's received.

I created an employee of zero, to allow the foreign key to be created. But all the other tables that have foreign keys to my Employee table I would prefer to not allow zeroes. So I changed them to use a check of (employeeid > 0).

Is it possible to have a foreign key say that I want the value from the Field in Table A to exist in table B, or to be zero? Or would it just be easier to leave off the foreign key in this one case?


Tks

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-06-08 : 14:36:18
Fkey column can have any value as along as column in parent table has that value.
Go to Top of Page

Sunkist
Starting Member

4 Posts

Posted - 2008-06-08 : 15:03:17
I didn't originally want to add EmployeeID zero to my Employee table however. I did yesterday in order to create this foreign key, but was wondering if I could still create a foreign key without creating a record for Emp # zero?
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-06-08 : 15:47:27
if the FK is nullable, it can also be NULL (even though the PK column can nerver be NULL)


elsasoft.org
Go to Top of Page

Sunkist
Starting Member

4 Posts

Posted - 2008-06-08 : 16:17:35
Thanks jezemine, I will give that a try. :)
Go to Top of Page
   

- Advertisement -