tags:

views:

110

answers:

2

Hi I have two tables like x and y. x table have one PK column and y table have FK constraint of table x (with null). i need to insert null value in table y by passing null value. is it possible in NHibernate. i tried by passing null value and is throughing FK constraint error.

Plz suggest me.

Regards Jcreddy.

A: 

If you want a specific answer on how to do what you are asking, you need to provide more information about the tools you are using. What database system? what programming language?

However I think you have a bigger problem than how to insert a null. Even though it is a FK, it is still a key. For referential integrity there should be a PK that matched in another table. This means that there would be have to be a PK with a null somewhere too, which is something you should never do in a good database design. If at all possible look into a better design for the database tables that doesn't require you to use null keys.

William Leader
I can't comment on Derek's answer but it is wrong. You wouldn't put an empty FK to a mobile table in a person table. You would have the mobile table have a FK to the person table instead. If there was no record in the mobile table that matched the PK in the person table, then the person has no mobile. So I repeat; a good database design will never have a key field, PK or FK, that allows nulls.
William Leader
A: 

If a person table has a mobilePhone fm to a phone table, it is quite valid for the mobilePhone field to be null if the person doesn't have a mobile phone. Howver you don't need to insert a phone record with a null primary key. That would be invalid.

Derek Clarkson