Good! I am learning sql about a week ago and I have some problems understanding how foreign keys work, specifically creating inserts in an application where the foreign key is receiving null value. (and each table id is receiving its respective value and data).
for example
process:(id, idclient, processnumber, processcompany, Supervisorname, bank ,state)
client(id, name, contact, SSN)
The process table has the following data and constraints:
id int identity(1,1) not null,
idclient int,
processnumber int not null,
processcompany nvarchar(150),
supervisor nvarchar(150),
bank nvarchar(150),
state int not null,
constraint PK_Processo primary key (id, processnumber),
constraint FK_ProcessClient foreign key(idclient)
references client(id)
on update cascade
on delete set null
);
and these are the most important data insertion queries for creating a new process:
string insertclientquery = "insert into dbo.client(name, contact) values (@name, @contact)";
string insertprocquery = "insert into dbo.process(processnumber, processcompany, bank, state) values (@processnumber, @processcompany, @bank, @state)";
the insert works fine when I check the data in the process table, idclient is null. and this is not what I intended, as in the following case:
client(id=1, bla bla bla),
process(id=1, idclient = null, bla bla bla)
I would like to know why the value is given as null in idclient and how could it be solved so that the value would be assigned automatically.
Thank you very much for your help! Rest of a good weekend!