Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created two tables in SQL Database

TableA: Columns Names; ID, Name;
TableB: Columns Names: E_Id, DOB;

TableA: ID primary KEY, TableB: E_Id Foreign KEY

TableA ID		Name
		1		AAAA
		2		BBBB


how to reflect TableA of ID Second TableB of E_ID

TableB E_ID 	DOB
				12/12/1998
				15/09/1994


only Insert ID in TableA. whenever Enter ID in TableA, at the same time
how to reflect E_ID in TableB.

after that reflect TableB in E_ID

how to get this OUTPUT

TableB E_ID 	DOB
		1		12/12/1998
		2		15/09/1994



now TableA ID and TableB E_ID are SAME.

What I have tried:

SQL
SELECT * INTO TableB
FROM TableA
WHERE ID IN
(SELECT ID FROM TableB)
Posted
Updated 1-Jan-22 5:55am
v2

1 solution

Foreign keys are there to enforce referential integrity and you can't have that unless the appropriate row already exists in the other table.

It's like an invoice: you have one invoice which has the (legally required) invoice number and date, the customer information, the total item cost, the delivery charges, the taxes, and the total to be paid.
There are also the invoice lines, each of which refers to a specific invoice, and holds the details for one purchase item: the number of product units involved, the product type and description, the unit price, the line total.

You can't create the in voice lines without first creating the invoice as they would not have anything to refer to, so you create the invoice first, then add the line items to it.

What you have is done the same way: the TableA row is created first, then the ID for that is used when the TableB rows are created.

And if I'm honest, there doesn't seem to be any good reason why you would split the tables that way: a person is born only once, so there is no reason for keeping the DOB in a separate table from the person's name.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900