corresponding IDs in different tables

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?
al4629740Asked:
Who is Participating?
 
dannygonzalez09Connect With a Mentor Commented:
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
 
dannygonzalez09Commented:
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
al4629740Author Commented:
Jim, the "whatever" would be the tblID1 and tblID2

Something needs to identify the connection between the records of two separate tables.
0
 
al4629740Author Commented:
How do I create a foreign key in the design mode of the table?
0
 
al4629740Author Commented:
I am using 2012 sql server
0
 
Jim HornConnect With a Mentor Microsoft SQL Server Developer, Architect, and AuthorCommented:
>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
 
awking00Commented:
You might create an after update trigger on table1.tblid1 to update table2.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.