can i insert null values for a foreign key

  • i heard that foreign key field can contain null values

    but when i'm trying to

    insert into executive values('ss22v','',2733)

    among quotes contain foreign key

    but when im executing program found error message

    "Msg 547, Level 16, State 0, Line 1

    The INSERT statement conflicted with the FOREIGN KEY constraint "FK__emp__mngid__75035A77". The conflict occurred in database "master", table "dbo.manager", column 'mngid'.

    The statement has been terminated.

    "

    why coudn't i keep null values for it

    here the entire code

    create table manager( mngid varchar(10) primary key,salary money)

    create table executive( exid varchar(10) primary key,mngid varchar(10) references manager(mngid),salary money)

    insert into manager values('e1122v',2700)

    insert into executive values('ss22v','',2733)

  • NULL and '' (empty string) are not same. SQL interprets empty string as a valid value, that is why the insert is failing.

    Try replacing empty string with NULL value. i.e.

    insert into executive values('ss22v',NULL,2733)

    And also, to make a note, you are creating tables in master database. Are you sure you are creating objects in the right database?

    --Ramesh


  • You probably don't want tables in master, and note in the example above, NULL does not need quotes. It's a reserved word.

  • Thanks all you guys

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply