c# - Why my foreign key is NULL on inserting values -
i sqlserver beginner , had first created table customers see below :
create table customers ( id int not null, name varchar(20) not null, age int not null, address char (25) , salary decimal (18,2) default 70000, not null identity(1, 1) primary key (id));
and inserted value in table this:
insert [vims].[dbo].[customers] (name, age, address, salary) values('abheshekkabhai',21,'agra',70000.00);
and done succesfully please see : http://prntscr.com/7w19cr\
after created table orders this:
create table [vims].[dbo].orders ( id int not null identity(1, 1) primary key, date datetime, customer_id int foreign key (customer_id) references customers (id), amount int );
and insert data inside :
insert [vims].[dbo].orders(date,amount) values(28,66000.00);
i table on repetitively inserting record 6 times : http://prntscr.com/7w1a6j
my problem when try insert data in foreign key column gives error saying there conflict of value of id between customers , orders , when add nothing @ foreign key column shows null. why ? how insert value on foreign key column ?
you need following while performing insert.
declare @customerid int insert [vims].[dbo].[customers](name,age,address,salary) values('abheshekkabhai',21,'agra',70000.00); @customerid = scope_identity() insert [vims].[dbo].orders(date,amount,customer_id) values(28,66000.00,@customerid);
thanks
Comments
Post a Comment