1
Good! I’ve been learning sql for about a week now and I’m having some problems understanding how Foreign Keys work, specifically by creating Inserts in an application where Foreign key(foreign key) is receiving null value. (and each principal of the table is to receive its respective value and data).
for example
process:(id, idclient, processnumber, processcompany, Supervisorname, bank ,state)
client(id, name, contact, SSN)
the tabla process 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 following 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)";
Insert works well when I check the data in the process table, the idclient is null. and that 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 null in the idclient and how it could be solved so that the value is automatically assigned.
I really appreciate your help! Good weekend rest!
hello Marco, this is the site of Stackoverflow in Portuguese, please translate your question, or you can do it directly on the site in English
– Ricardo Pontual
Hello Ricardo, I am going to translate to Portuguese, I thought the question was seen by everyone besides who spoke the Portuguese language so that’s why it was written like this. obg.
– Marco Ávila