Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

corresponding IDs in different tables

Posted on 2013-12-16
8
Medium Priority
?
164 Views
Last Modified: 2013-12-16
I have Table1 and Table2

tblID1 is a string in Table1 and tblID2 in Table2 is a string nchar10

tblID1 in Table1 needs to correspond with all the tblID2 values in Table2.  Is there a way to link the values in Table1 to Table2 if they change in Table1?

For example, if I create a record in Table1 with tblID1 = 1, then tblID2 = 1.  However if I change ID1 = 5, then tblID2 = 5

Is there a way to do that?
0
Comment
Question by:al4629740
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:dannygonzalez09
ID: 39721876
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39721883
>Is there a way to link the values in Table1 to Table2 if they change in Table1?
Just a thought ... if Table2.tblID2 should always be the same value as Table1.tblID2, then it would make sense to not have column Table2.tblID2, and in queries JOIN the two tables with the below snippet in the SELECT clause
SELECT Table1.tblID1 as TblID2
FROM Table1
   JOIN Table2 ON Table1.Whatever = Table2.Whatever

Open in new window

0
 

Author Comment

by:al4629740
ID: 39722048
Jim, the "whatever" would be the tblID1 and tblID2

Something needs to identify the connection between the records of two separate tables.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:al4629740
ID: 39722052
How do I create a foreign key in the design mode of the table?
0
 

Author Comment

by:al4629740
ID: 39722059
I am using 2012 sql server
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1000 total points
ID: 39722103
>Jim, the "whatever" would be the tblID1 and tblID2
Uhh, no, not if tblID2 keeps changing as tblID1 changes.

One solution is a foreign key relationship (mentioned above) with a cascade update, so that tblID2 changes as tblID1 changes for a given tblID1 value.

Or ... manually run an UPDATE statement every time tblID1 changes, with pouplated @the_new_value and @the_old_value variables, like this..

UPDATE Table2
SET tblID2 = @the_new_value
WHERE tblID1 = @the_old_value
0
 
LVL 32

Expert Comment

by:awking00
ID: 39722179
You might create an after update trigger on table1.tblid1 to update table2.
0
 
LVL 5

Accepted Solution

by:
dannygonzalez09 earned 1000 total points
ID: 39722188
steps to create a foreign key

http://technet.microsoft.com/en-us/library/ms189049(v=sql.105).aspx

As Jim suggests, foreign key with an update should keep the Id's in sync
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question