Foreign key can be null ?

Posted by Pkanwar under Sql Server on 10/8/2013 | Points: 10 | Views : 1000 | Status : [Member] | Replies : 4
Can foreign key can be null ? If yes then how it can be null, because it is reference of primary key and primary key can not be a null?




Responses

Posted by: Vuyiswamb on: 10/8/2013 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
a Foreign Key cannot be Null. if you get an error like that it means the table that contains the foreign key as a primary key has no data

Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Bandi on: 10/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
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.

If there is NULL value in Foreign key column means that we haven't decided the value for that column...

For example:
DEPARTMENT Table: DeptID (PK), DeptName
EMP Table: EMPID, DEPTID (FK)

You can have all departments details with DEPARTMENT table..
In EMP table, you should assign the deptid for each employee... Here you can insert NULL for DeptId of a employee if NOT YET assigned a department for the employee who joined the company...

If you doesn't want to insert NULL value in the case of "NOT Assigned Department" then you can have one departmentId ( Eg: -1, 0) for Others department in the master table( DEPARTMENT Table)...

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: Sriramnandha on: 10/10/2013 [Member] Starter | Points: 25

Up
0
Down
Foreign key cannot be Null because its primary key constraint what ever values insert in primary key table those values only able to insert foreign key table...


hope this will help

sriram


sriram

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

Posted by: Bandi on: 10/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Click on "Mark as Answer " link

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Login to post response