home
training courses
why choose us?
solutions
support
company
LearnItFirst User Forum
Welcome Guest
Search
|
New Posts
|
Members
|
Log In
|
Register
SQL Server Forum - LearnItFirst.com
»
Transact-SQL Programming
»
DML (SELECT, INSERT, UPDATE, DELETE) Questions
»
How to populate foreign key column with primary key column data
How to populate foreign key column with primary key column data
Options
Previous Topic
·
Next Topic
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.
Back to top
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)
Back to top
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"
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.
Back to top
Users browsing this topic
Guest
Forum Jump
SQL Server Database Administration
- General SQL Database Question & Answer
- Backup, Recovery and Disaster Recovery
- SQL Server Security
- Integration Services (SSIS) and DTS
Transact-SQL Programming
- DML (SELECT, INSERT, UPDATE, DELETE) Questions
- Stored Procedures, Triggers, & Functions
SQL Server Scripts, Code Samples and SSMS Custom Reports
- All SQL Server Versions
- SQL Server 2005/2008
- SQL Server Management Studio Custom Reports
Customer Service
- Video Requests
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.
SQL Server 2005 DBA Training Videos
SQL Server 2008 DBA Training Videos
Watch this topic
RSS Feed
Email this topic
Print this topic
Threaded
Normal