Solved

corresponding IDs in different tables

Posted on 2013-12-16
8
150 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
 

Author Comment

by:al4629740
ID: 39722052
How do I create a foreign key in the design mode of the table?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 31

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL JOIN 6 39
Sql query to Stored Procedure 6 21
SQL Server 2012 Express to Full 5 26
Stored Procedure 2 10
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now