Last post May 18, 2015 02:18 PM by Nadeem157
May 18, 2015 10:07 AM|Nadeem157|LINK
I want to know that whether the foreign key column value can be null or not in sql server??
May 18, 2015 10:24 AM|PatriceSc|LINK
Yes, technically you can use NULL or NOT NULL for your FK column depending on what you want. I would have to check but it seems quite rare for me and in some cases I (and likely others) find better to use a NOT NULL column and have a special "not defined"
row or whatever in the referenced table....
If you tried something that doesn't work, it's likely best to ask directly about that.
May 18, 2015 10:26 AM|Nadeem157|LINK
Yes got a bit of clearance from your explanation.
I couldn't post more related to this as this was asked to me in one of the interview's whether I can set foreign key value null or not ???
May 18, 2015 10:30 AM|Mikesdotnetting|LINK
It depends on your business rules. If all ministers must have a department, the foreign key cannot be null; the relationship between ministers and departments is required. If you are allowed to have ministers without a portfolio, you can have nullable foreign
keys in a database.
May 18, 2015 10:33 AM|Nadeem157|LINK
Thanks a lot sir,
that's what exactly i was looking for. It means it depends on the logic how we apply.
May 18, 2015 10:43 AM|PatriceSc|LINK
In addition try perhaps :
http://www.sql-questions.com/microsoft/SQL-Server-Programming/35476386/nullable-foreign-key-constriants.aspx (I'm rather in the "use a special row" camp unless it *really* make sense). Else it can make querying a bit more complex IMHO.
It's always hard to understand which answer they wanted to hear during an interview. IMO don't be afraid to ask them what they think about a subject once you answered. More likely it's rather to see how you can discuss a point and wether or not it's best
to do that even if technically possible rather than just give ayou a point if you correctly answered "yes" or "no".
May 18, 2015 02:18 PM|Nadeem157|LINK