let's assume we have a Relational DB in MS-Access(image).
Where We have 4 Tables and columns related to tables which are related to one another.
I know we can insert like:
INSERT INTO Project ( ProjectName, ProjectID, FK_Employee, FK_Department )
SELECT 'Project1' AS Name, '99950' AS ID, Employee.ID, Department.ID
FROM Department, Employee
WHERE (((Employee.employeeName)=@employee) AND ((Department.DepartmentName)=@Dept));
But is there any better way to do it? I know we can
Select
with different types of Join. Is it make sense or is there a way to use Join with insertion?I've faced a problem with this kind of query when the Foreign key is optional. What I mean is if I use this query for my VB/ADO.Net and if I send
DBNull.Value
it creates no new row or
In precise.
- Let's say I have saved no employee during project creation. So, my FK_Employee is Empty. Now, I want to insert a new Employee in the
Employee
table and also update the Foreign Key inProject
table whereProjectName
is 'Project1'. Is it possible to do it in one query? If it is not, how can I insert values inEmployee
Table and then read the Auto incremented ID and update it toProject
Table in VB/ADO.Net? (Answer with C# is also okay).
I really am stuck here. If anyone knows the answer please help me to get rid of this situation. I really appreciate your kindness and help. Thank you in advance.