Solved

corresponding IDs in different tables

Posted on 2013-12-16
8
157 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:dannygonzalez09
ID: 39721876
0
 
LVL 65

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 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 250 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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