-1

I have question about changing value in child table automatically if value of table mother change value.

I have two tables are brand(mother) and cat(child).

I add foreign key is brand_id in table cat related to primary key id in table brand.

If I change brand_name from brand table, I also want brand_name in table cat changed automatically. Is it possible? and how can I do it.

Thank you so much.

Brand:

enter image description here

Cat:

enter image description here

Hai Tien
  • 2,929
  • 7
  • 36
  • 55

1 Answers1

0

You could create a custom trigger on the brand table which before update would perform the necessary updates on the cat table.

Here is a link to the documentation for MySQL triggers: https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html

UPDATE: As commented by @kerbholz, proper data modeling would suggest that the brand name column shouldn't exist in the cat column as it creates duplication of data. If this is purely for your client to see the data, perhaps building them a custom view using properly modeled tables is a better solution.

Link to appropriate documentation: https://dev.mysql.com/doc/refman/8.0/en/create-view.html

brandonris1
  • 455
  • 3
  • 9
  • Thank you so much. Can you explain me by example syntax? I add in mysql but 'definition' is? – Hai Tien Apr 17 '20 at 16:31
  • In the links there are examples of the proper syntax. – brandonris1 Apr 17 '20 at 16:33
  • I understand adding brand_name in cat table is duplication of data but they gave me the available data so I want to keep it – Hai Tien Apr 17 '20 at 16:33
  • That is exactly why I think giving your users a custom view of the data would be better for both of you. From your side, you get to maintain good data modeling avoiding this whole issue of updating the brand names in the cat table while from the users perspective they get to see the data in a table format as they want to see it. This is an exact use case for using a DB view. – brandonris1 Apr 17 '20 at 17:13