Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Update table values if corresponding values change in another table Expand / Collapse
Author
Message
Posted Wednesday, July 24, 2013 5:03 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 9:26 AM
Points: 554, Visits: 1,727
Hi,

I am very new to SQL and really dont know how to phrase my question.

There are 2 tables linked through a primary key and if the values in one table change, the corresponding values in another table should be changed and reflected accordingly.

Does someone know what logic I need to apply for this to work ? Do I have to create a primary key-foreign key relationship and then create a trigger on the other table on which the values need to be updated ?

The values in the table will be changed through a webpage.

Any ideas would be appreciated.

Thanks.
Post #1476986
Posted Wednesday, July 24, 2013 5:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:39 AM
Points: 12,906, Visits: 31,985
pwalter83 (7/24/2013)
Hi,

I am very new to SQL and really dont know how to phrase my question.

There are 2 tables linked through a primary key and if the values in one table change, the corresponding values in another table should be changed and reflected accordingly.

Does someone know what logic I need to apply for this to work ? Do I have to create a primary key-foreign key relationship and then create a trigger on the other table on which the values need to be updated ?

The values in the table will be changed through a webpage.

Any ideas would be appreciated.

Thanks.

it sounds like you want to echo the same value in two places, is that correct? so if i change one table, it copies the same value to a related record in another?

if the above assumption is true, here's my thoughts:
This is breaking the whole normal forms rule: if a value is in one table, and there is a relationship between the two tables anyway, you can always join the tables together to get the value when it needs to be displayed in reference to the second table, so you want to try and avoid that.

so i'd say it's better to drop the column in the second table, and get the value in a query;

you could also create a view with that query above, to make it easier to access;

another possibility is to replace the column in table 2 with a calculated field, and the calculated field calls a user defined function that effectively gets the value you want from the other table, based on the join/relationship between the two.

because that UDF would be scalar, it would slow down access to the table if there is a ton of rows, which makes the query above idea more palatable in my eyes.

finally, if you are painted into a corner, and just have to do it, you could create a trigger that copies the value to the other table for insert and update.


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1476994
Posted Wednesday, July 24, 2013 5:16 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 3:13 PM
Points: 2,255, Visits: 2,718
If you post the DDL (table definition) statements and include some sample data (INSERT statements) we can help you better.

You probably should use a single stored procedure to first update the values in the base-table. Include a second update statement in this stored procedure to update the values in the related table.

create procedure ({define all variables})
as
begin
update {base_table} set {column_name}={updated value}, ... where {id_column}={id_value}
update {related_table} set {column_name}={updated value}, ... where {foreign_key_column}={id_value}
end


** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1476996
Posted Wednesday, July 24, 2013 5:26 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 9:26 AM
Points: 554, Visits: 1,727
Lowell (7/24/2013)
pwalter83 (7/24/2013)
Hi,

I am very new to SQL and really dont know how to phrase my question.

There are 2 tables linked through a primary key and if the values in one table change, the corresponding values in another table should be changed and reflected accordingly.

Does someone know what logic I need to apply for this to work ? Do I have to create a primary key-foreign key relationship and then create a trigger on the other table on which the values need to be updated ?

The values in the table will be changed through a webpage.

Any ideas would be appreciated.

Thanks.


it sounds like you want to echo the same value in two places, is that correct? so if i change one table, it copies the same value to a related record in another?

if the above assumption is true, here's my thoughts:
This is breaking the whole normal forms rule: if a value is in one table, and there is a relationship between the two tables anyway, you can always join the tables together to get the value when it needs to be displayed in reference to the second table, so you want to try and avoid that.

so i'd say it's better to drop the column in the second table, and get the value in a query;

you could also create a view with that query above, to make it easier to access;

another possibility is to replace the column in table 2 with a calculated field, and the calculated field calls a user defined function that effectively gets the value you want from the other table, based on the join/relationship between the two.

because that UDF would be scalar, it would slow down access to the table if there is a ton of rows, which makes the query above idea more palatable in my eyes.

finally, if you are painted into a corner, and just have to do it, you could create a trigger that copies the value to the other table for insert and update.



Thanks Lowell,

However, I was wondering is it not possible to implement this logic within the table structure because as I mentioned earlier, the values in one table would be updated through a webpage. The changed value then needs to be reflected in the other related table.

Would it be possible to achieve this by developing a primary key-foreign key relationship and then use trigger to update the values in the other table ?

Thanks.
Post #1476998
Posted Wednesday, July 24, 2013 5:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:50 AM
Points: 7,191, Visits: 13,645
Lowell has already asked but the question remains unanswered: why do you want to hold the same data in two places at once?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1477004
Posted Wednesday, July 24, 2013 5:47 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:39 AM
Points: 12,906, Visits: 31,985
pwalter83 (7/24/2013)

Thanks Lowell,

However, I was wondering is it not possible to implement this logic within the table structure because as I mentioned earlier, the values in one table would be updated through a webpage. The changed value then needs to be reflected in the other related table.

Would it be possible to achieve this by developing a primary key-foreign key relationship and then use trigger to update the values in the other table ?

Thanks.

Walter, yes it's possible using a trigger and a relationship between the two tables(ie a PK/FK), and i certainly understand that sometimes it's going to be done regardless of whether its following best practices and just doing the work to get the job done;
I just wanted to provide a decent discussion on all possible aspects i could think of;
if you have the DDL for the two tables, and the column(s) you want copied, we could help with the trigger;

but of course,here's my follow up question:

if you insert into Table1, do you know for sure rows exist in Table2, or do you have to create those rows int eh trigger as well? is this only for updting, so you know both rows already exist due to some other process having created them previously?


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1477007
Posted Wednesday, July 24, 2013 5:52 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 9:26 AM
Points: 554, Visits: 1,727
ChrisM@Work (7/24/2013)
Lowell has already asked but the question remains unanswered: why do you want to hold the same data in two places at once?


Hi,

The reason is - The same data needs to exist in two related tables is bacause this is part of the web project consisting of many sub-pages. If the value in one sub-page is changed then that saved value in the main webpage should be updated accordingly.

Each of these webpages are linked to individual tables so in fact the changes are to be made at the SQL level first.
Post #1477010
Posted Wednesday, July 24, 2013 5:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:50 AM
Points: 7,191, Visits: 13,645
pwalter83 (7/24/2013)
ChrisM@Work (7/24/2013)
Lowell has already asked but the question remains unanswered: why do you want to hold the same data in two places at once?


Hi,

The reason is - The same data needs to exist in two related tables is bacause this is part of the web project consisting of many sub-pages. If the value in one sub-page is changed then that saved value in the main webpage should be updated accordingly.

Each of these webpages are linked to individual tables so in fact the changes are to be made at the SQL level first.


Surely the main page and the subpage should be reading the same value from the same table?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1477014
Posted Wednesday, July 24, 2013 7:43 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 9:26 AM
Points: 554, Visits: 1,727
ChrisM@Work (7/24/2013)
pwalter83 (7/24/2013)
ChrisM@Work (7/24/2013)
Lowell has already asked but the question remains unanswered: why do you want to hold the same data in two places at once?


Hi,

The reason is - The same data needs to exist in two related tables is bacause this is part of the web project consisting of many sub-pages. If the value in one sub-page is changed then that saved value in the main webpage should be updated accordingly.

Each of these webpages are linked to individual tables so in fact the changes are to be made at the SQL level first.


Surely the main page and the subpage should be reading the same value from the same table?


Wish it was ? But no, as I mentioned before, each webpage is associated with a different table.

The main webpage is populated using the tables which are linked to the sub-pages and so the same columns exist in different tables. This implies that the tables should be normalised so that if any value is updated using the sub-pages, the change should be reflected in the main webpage.
Post #1477063
Posted Wednesday, July 24, 2013 7:51 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 5:24 AM
Points: 128, Visits: 490
You could put a trigger on the table but I don't think you can directly pass parameters to it, so there may not be a way to get the new values to your second table.

My suggestion would be to change your stored procedure to update both tables like someone else mentioned. This is a more direct approach and gives you total control of the transaction in one place.

Mark



Post #1477070
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse