is this correct

Posted by Jameschowdare under Sql Server on 2/11/2010 | Views : 1324 | Status : [Member] | Replies : 3
we can't insert null value into foreignkey column




Responses

Posted by: Nishithraj on: 2/11/2010 [Member] Bronze

Up
0
Down
Foreignkey you can keep as nullable columns. But if you insert null value it can bareak the "The relational referential-integrity constraint"

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Jameschowdare, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/11/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Yes you can. A foreign key can be nullable, but Primary key doesnt allows null. So the foreign key should be a part of Composite key to allow null values in it.

But it is not better to allow nulls to foreign key.
Msdn Says :

A FOREIGN KEY constraint can contain null values; however, if any column of a composite FOREIGN KEY constraint contains null values, verification of all values that make up the FOREIGN KEY constraint is skipped. To make sure that all values of a composite FOREIGN KEY constraint are verified, specify NOT NULL on all the participating columns.


So keep this in mind and use the Nullable Foreign key only if it is unavoidable.

I hope you like my answer.
Cheers


www.abhisheksur.com

Jameschowdare, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: RtpHarry on: 2/12/2010 [Member] [MVP] Bronze

Up
0
Down
As people are saying - its not recommended. You should try to keep your data clean.

Depending on the cascade policy of your foreign key constraints you could set it to "set null" if a related record is removed.

Jameschowdare, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response