LearnItFirst User Forum

New SQL Server 2008 DBA Course
Welcome Guest Search | New Posts | Members | Log In | Register

How to populate foreign key column with primary key column data Options
gridview
Posted: Tuesday, November 11, 2008 1:54:37 PM
Rank: Newbie

Joined: 9/2/2008
Posts: 6
Points: 18
I have table called A with a primary key called Id.
I have another table called B. Both tables A and B have no of fields and data in it.

Now due to sudden change in the application we need to build relation between table A and B as below:

Table B will have a new column called AId which will be foreign key and will be key to connect table A primary key Id.

Now the problem is how to write a sql script to populate the data in AId column of child Table B same as data in Id column of parent Table A?

Please suggest.
bklr
Posted: Tuesday, January 27, 2009 2:56:15 AM

Rank: PFY

Joined: 1/21/2009
Posts: 27
Points: 81
Where do you live?: India
alter table tableb add constraint constraintname foreign key (columnanamefor in teableb) references tablea(primarykey columnname)
Scott Whigham
Posted: Tuesday, January 27, 2009 5:04:34 AM


Rank: Super Mod

Joined: 3/20/2006
Posts: 460
Points: 1,002
Where do you live?: Dallas, TX
You haven't given us enough info - how do you know which child rows are related to which parent rows? Just telling us that you are adding a new column to the child ("AId"Wink is not enough info; we need to know how you know what value to put in a given row. Once we know that we can write an UPDATE query.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.