Update table values if corresponding values change in another table

  • pwalter83 (7/24/2013)


    ChrisM@Work (7/24/2013)


    pwalter83 (7/24/2013)


    ChrisM@Work (7/24/2013)


    pwalter83 (7/24/2013)


    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.

    This looks correct: "The main webpage is populated using the tables which are linked to the sub-pages". That's fine - a subpage and the main page can read the same data from the same table.

    The rest of it is bonkers. It's the wrong way round.

    I am not sure where we are going with this debate. The webpages have already been setup and they are working properly. The only thing left to do is to normalise data so that the change on one table is reflected on the other table and hence updated accordingly on the main webpage.

    Do you have any ideas or suggestions to accomplish this ?

    Sorry, no I don't. I can only reiterate this:

    "The only thing left to do is to properly code the pages so that the change on one page is updated accordingly in the table and hence reflected on the other page."

    - and wish you luck, because this is probably the thin end of a very big problem wedge.

    Sorry Chris, I know you have helped me before but this time I know that the solution lies in normalising the database and not changing the webpage coding.

    Heh that's okay Paul. I'd be interested to see how you get on with this - not in a "I told you so" kinda way, but curiosity. I can't see how it can cause anything but problems, and your confidence in the resolution tells me I've got the wrong end of the stick 😉

    “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

  • Since you started a new thread and Chris pointed me back to this one I will post here.

    I would agree that trying to keep this data in multiple tables is very bad design decision. It will cause you nothing but grief and anguish. You will need to implement triggers in each of the tables in order to even have a chance at making this work.

    Lowell asked if you are certain that the rows always exist in all tables or if there is a possibility that you will have to create them. I never saw an answer from you on that.

    Personally I would run away from this design as quickly as possible. Normalize your data and keep one and only copy of the details. There are a number of people on this thread already who can and will help you normalize your structures but you are going to have to provide some details. By details I don't mean another description of the issue, I mean actual ddl, sample data and a bit more detail about these web pages.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • pwalter83 (7/24/2013)


    ChrisM@Work (7/24/2013)


    pwalter83 (7/24/2013)


    ChrisM@Work (7/24/2013)


    pwalter83 (7/24/2013)


    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.

    This looks correct: "The main webpage is populated using the tables which are linked to the sub-pages". That's fine - a subpage and the main page can read the same data from the same table.

    The rest of it is bonkers. It's the wrong way round.

    I am not sure where we are going with this debate. The webpages have already been setup and they are working properly. The only thing left to do is to normalise data so that the change on one table is reflected on the other table and hence updated accordingly on the main webpage.

    Do you have any ideas or suggestions to accomplish this ?

    Sorry, no I don't. I can only reiterate this:

    "The only thing left to do is to properly code the pages so that the change on one page is updated accordingly in the table and hence reflected on the other page."

    - and wish you luck, because this is probably the thin end of a very big problem wedge.

    Sorry Chris, I know you have helped me before but this time I know that the solution lies in normalising the database and not changing the webpage coding.

    Using a trigger or stored procedure to update the same value in different tables is not the same as normalizing the database. Normalizing the database involves removing the duplicated data. ie. One table has all the status codes and other tables just link to it using a key.



    The opinions expressed herein are strictly personal and do not necessarily reflect the views or policies of my employer.

  • Sean Lange (7/29/2013)


    Since you started a new thread and Chris pointed me back to this one I will post here.

    I would agree that trying to keep this data in multiple tables is very bad design decision. It will cause you nothing but grief and anguish. You will need to implement triggers in each of the tables in order to even have a chance at making this work.

    Lowell asked if you are certain that the rows always exist in all tables or if there is a possibility that you will have to create them. I never saw an answer from you on that.

    Personally I would run away from this design as quickly as possible. Normalize your data and keep one and only copy of the details. There are a number of people on this thread already who can and will help you normalize your structures but you are going to have to provide some details. By details I don't mean another description of the issue, I mean actual ddl, sample data and a bit more detail about these web pages.

    Thanks for your reply. Sorry I am late in replying as I was still trying to figure out a way to achieve it but in vain.

    The webpage I am working on contains of a main webpage which is linked to a table (master table). The other webpages (subpages) are each linked to a separate sub-table.

    The tables linked to the subpages have prepopulated data and sometimes the data needs to be amended or changed.

    The main webpage has Add and Edit button to add or edit the data. When the Add button is clicked, it displays dropdownlists each linked to the sub-tables and same with the Edit button.

    With your logic to normalize the table, the data linked to the main webpage should not be stored in the master table as actual values, is that what you mean ?

    What if someone needs to see the actual data in SQL database in the main table ? They would only find ID columns that link to the other tables in stead of actual values/data. This is the part I am not clear about, can you please throw some light on it.

    Thanks.

  • pwalter83 (7/31/2013)


    Sean Lange (7/29/2013)


    Since you started a new thread and Chris pointed me back to this one I will post here.

    I would agree that trying to keep this data in multiple tables is very bad design decision. It will cause you nothing but grief and anguish. You will need to implement triggers in each of the tables in order to even have a chance at making this work.

    Lowell asked if you are certain that the rows always exist in all tables or if there is a possibility that you will have to create them. I never saw an answer from you on that.

    Personally I would run away from this design as quickly as possible. Normalize your data and keep one and only copy of the details. There are a number of people on this thread already who can and will help you normalize your structures but you are going to have to provide some details. By details I don't mean another description of the issue, I mean actual ddl, sample data and a bit more detail about these web pages.

    Thanks for your reply. Sorry I am late in replying as I was still trying to figure out a way to achieve it but in vain.

    The webpage I am working on contains of a main webpage which is linked to a table (master table). The other webpages (subpages) are each linked to a separate sub-table.

    The tables linked to the subpages have prepopulated data and sometimes the data needs to be amended or changed.

    The main webpage has Add and Edit button to add or edit the data. When the Add button is clicked, it displays dropdownlists each linked to the sub-tables and same with the Edit button.

    With your logic to normalize the table, the data linked to the main webpage should not be stored in the master table as actual values, is that what you mean ?

    What if someone needs to see the actual data in SQL database in the main table ? They would only find ID columns that link to the other tables in stead of actual values/data. This is the part I am not clear about, can you please throw some light on it.

    Thanks.

    Yeah, this is the way normalization works. To look at the actual values from the lookup tables with the other information in the main table, you would use a join. Something along the lines of

    SELECT * FROM maintable AS a INNER JOIN subtable AS b ON a.subtableid = b.subtableid



    The opinions expressed herein are strictly personal and do not necessarily reflect the views or policies of my employer.

  • 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?

    Sorry Chris, you were right about this, could you please help me further ?

    I wish to track the changes in table1 if the row is updated on table2 based on the condition that both tables are linked through primary-foreign key (VENDOR_ID). In other words, if VENDOR_NAME is changed on table2 then that change should be reflected on table1 through VENDOR_ID. Can this be achieved through a trigger ? If yes, then could you please guide me how can that be accomplished.

    The DDL for table 1 and 2 are as below:

    CREATE TABLE [dbo].[table1](

    [SYSTEM_ID] [int] IDENTITY(1,1) NOT NULL,

    [SYSTEM_NAME] [varchar](100) NOT NULL,

    [VENDOR_ID] [varchar](100) NOT NULL

    CREATE TABLE [dbo].[table2](

    [VENDOR_ID] [int] IDENTITY(1,1) NOT NULL,

    [VENDOR_NAME] [varchar](100) NOT NULL

    Thanks in advance for your help.

  • There is no need for a trigger or something else. This is the key to a relational database design.

    The value (of VENDOR_NAME) is stated on one position and is displayed on all locations with the use of key-references. With a SELECT statement you join the two tables together on the key reference (VENDOR_ID).

    You can use the code below to display the joined data of both tables:

    SELECT

    SYSTEM_ID

    , SYSTEM_NAME

    , table1.VENDOR_ID

    , VENDOR_NAME

    FROM table1

    INNER JOIN table2

    ON table1.VENDOR_ID = table2.VENDOR_ID

    You can update the VENDOR_NAME with statement:

    UPDATE table2 SET VENDOR_NAME = 'new_name' WHERE VENDOR_NAME = '{existing name}'

    If you execute the SELECT statement before and after the UPDATE, you can see the SELECT will always return the actual values.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • pwalter83 (8/16/2013)

    I wish to track the changes in table1 if the row is updated on table2 based on the condition that both tables are linked through primary-foreign key (VENDOR_ID). In other words, if VENDOR_NAME is changed on table2 then that change should be reflected on table1 through VENDOR_ID. Can this be achieved through a trigger ? If yes, then could you please guide me how can that be accomplished.

    The DDL for table 1 and 2 are as below:

    CREATE TABLE [dbo].[table1](

    [SYSTEM_ID] [int] IDENTITY(1,1) NOT NULL,

    [SYSTEM_NAME] [varchar](100) NOT NULL,

    [VENDOR_ID] [varchar](100) NOT NULL

    CREATE TABLE [dbo].[table2](

    [VENDOR_ID] [int] IDENTITY(1,1) NOT NULL,

    [VENDOR_NAME] [varchar](100) NOT NULL

    Thanks in advance for your help.

    You are close here but not quite. What you want to do is have the VendorID in table1 NOT the VendorName. That way when the name of the vendor changes you don't have to do anything in any other table.

    Using you ddl as a starting point, I changed Vendor_ID in table1 to be an int. It will hold the primary key of table2 instead of the value.

    Here is a full example to demonstrate.

    CREATE TABLE [dbo].[table1](

    [SYSTEM_ID] [int] IDENTITY(1,1) NOT NULL,

    [SYSTEM_NAME] [varchar](100) NOT NULL,

    [VENDOR_ID] int NOT NULL

    )

    CREATE TABLE [dbo].[table2](

    [VENDOR_ID] [int] IDENTITY(1,1) NOT NULL,

    [VENDOR_NAME] [varchar](100) NOT NULL

    )

    insert table2

    select 'My Vendor'

    insert table1

    select 'My System', SCOPE_IDENTITY()

    select *

    from table1 t

    join table2 t2 on t.VENDOR_ID = t2.VENDOR_ID

    update table2

    set VENDOR_NAME = 'Name is changed'

    where VENDOR_ID = 1

    select *

    from table1 t

    join table2 t2 on t.VENDOR_ID = t2.VENDOR_ID

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • pwalter83 (8/16/2013)


    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?

    Sorry Chris, you were right about this, could you please help me further ?

    I wish to track the changes in table1 if the row is updated on table2 based on the condition that both tables are linked through primary-foreign key (VENDOR_ID). In other words, if VENDOR_NAME is changed on table2 then that change should be reflected on table1 through VENDOR_ID. Can this be achieved through a trigger ? If yes, then could you please guide me how can that be accomplished.

    The DDL for table 1 and 2 are as below:

    CREATE TABLE [dbo].[table1](

    [SYSTEM_ID] [int] IDENTITY(1,1) NOT NULL,

    [SYSTEM_NAME] [varchar](100) NOT NULL,

    [VENDOR_ID] [varchar](100) NOT NULL

    CREATE TABLE [dbo].[table2](

    [VENDOR_ID] [int] IDENTITY(1,1) NOT NULL,

    [VENDOR_NAME] [varchar](100) NOT NULL

    Thanks in advance for your help.

    Have you considered using views instead of tables as the data sources for your web pages? View1 and View2 could be based off of the same table but have different definitions.

    “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

  • ChrisM@Work (8/16/2013)


    pwalter83 (8/16/2013)


    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?

    Sorry Chris, you were right about this, could you please help me further ?

    I wish to track the changes in table1 if the row is updated on table2 based on the condition that both tables are linked through primary-foreign key (VENDOR_ID). In other words, if VENDOR_NAME is changed on table2 then that change should be reflected on table1 through VENDOR_ID. Can this be achieved through a trigger ? If yes, then could you please guide me how can that be accomplished.

    The DDL for table 1 and 2 are as below:

    CREATE TABLE [dbo].[table1](

    [SYSTEM_ID] [int] IDENTITY(1,1) NOT NULL,

    [SYSTEM_NAME] [varchar](100) NOT NULL,

    [VENDOR_ID] [varchar](100) NOT NULL

    CREATE TABLE [dbo].[table2](

    [VENDOR_ID] [int] IDENTITY(1,1) NOT NULL,

    [VENDOR_NAME] [varchar](100) NOT NULL

    Thanks in advance for your help.

    Have you considered using views instead of tables as the data sources for your web pages? View1 and View2 could be based off of the same table but have different definitions.

    No, I havent, but would it be possible to add data to a view ? The webpage allows the users to add new values and also change the existing values as well. Can that be made possible using views ?

    Thanks.

  • Yes - have a read of this MS article.

    “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

Viewing 11 posts - 16 through 25 (of 25 total)

You must be logged in to reply to this topic. Login to reply