*Nodding to Zoltán Zörgő*
It's almost certainly down to your table definition ... best demonstrated with an example ...
create table parent
(
ID int IDENTITY(1,1) NOT NULL,
SomeData varchar(20),
CONSTRAINT PK_parentID PRIMARY KEY CLUSTERED (ID)
)
insert into parent values('parent 1')
insert into parent values('parent 2')
insert into parent values('parent 3')
create table child
(
ID int IDENTITY(1,1) NOT NULL,
SomeChildData varchar(20),
FKID int NOT NULL,
CONSTRAINT PK_childID PRIMARY KEY CLUSTERED (ID),
CONSTRAINT FK_ChildToParent FOREIGN KEY (FKID) REFERENCES parent
)
insert into child values('child 1.1', 1)
insert into child values('child 1.2', 1)
insert into child values('child 2.1', null)
Output
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'FKID', table 'child'; column does not allow nulls. INSERT fails.
The statement has been terminated.
(0 row(s) affected)
Whereas ...
drop table child
drop table parent
create table parent
(
ID int IDENTITY(1,1) NOT NULL,
SomeData varchar(20),
CONSTRAINT PK_parentID PRIMARY KEY CLUSTERED (ID)
)
insert into parent values('parent 1')
insert into parent values('parent 2')
insert into parent values('parent 3')
create table child
(
ID int IDENTITY(1,1) NOT NULL,
SomeChildData varchar(20),
FKID int,
CONSTRAINT PK_childID PRIMARY KEY CLUSTERED (ID),
CONSTRAINT FK_ChildToParent FOREIGN KEY (FKID) REFERENCES parent
)
insert into child values('child 1.1', 1)
insert into child values('child 1.2', 1)
insert into child values('child 2.1', null)
select * from parent
select * from child
Output
ID SomeData
1 parent 1
2 parent 2
3 parent 3
ID SomeChildData FKID
1 child 1.1 1
2 child 1.2 1
3 child 2.1 NULL
Here is also a link to the MSDN documentation
http://msdn.microsoft.com/en-us/library/ms189049(v=sql.110).aspx[
^]