# Linking Tables in MySQL

**URL:** <https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853>\
**Category:** General Discussion\
**Tags:** scripting\
**Created:** [February 25, 2025, 5:13pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853 "2025-02-25T17:13:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Howard](https://avatars.discourse-cdn.com/v4/letter/w/a4c791/32.png) [@William\_Howard](https://forum.inductiveautomation.com/u/William_Howard)\
**Post date:** [February 25, 2025, 5:13pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/1 "2025-02-25T17:13:44Z")

</div>

Not sure if this is the best place to post this question but thought someone hear might now a bit more then me about MySQL. we are currently setting up some table to store are user data and im looking into how we can link this two tables together so that is we update the data in a row of one of the tables it will update the data in are other table respectively based of the PK.  
so i have the following table that in the first photo i want to be set up as are master and in the second photo is are slave table so when we make a row in the slave it will have a matching userName and assetName column to the parent and im wanting to use that to then link the rows so if we query change something in the master say we change are assetName it would make the same change to are slave. so im not sure if what im looking to achieve is possible from what I've read it seems like it should be using foreign keys but im vary new to this so i could be mistaken.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/f/aff9d4196a65692ab545d591d0b969cd89ac334c.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/3/e34e5731256b7aeae77c41c25e7898e784f39788.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 25, 2025, 5:20pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/2 "2025-02-25T17:20:46Z")

</div>

Whether there's a way to do what you've _asked_ in MySQL I don't know.

What you _should_ do is set up a _third_ table that stores asset information.  
This table should have a primary key and an asset name _column_.  
Then the first two tables should be linked to the asset info's _id_ column. The ID is stable, and the name is therefore free to be updated. Whenever you're querying from the other two tables, you just have to join in the current asset name from your asset info table.

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [February 25, 2025, 5:22pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/3 "2025-02-25T17:22:54Z")

</div>

Do you have control over the tables all around?

You shouldn't have the columns `userName` and `assetName` be in two places. Instead, you can create a primary key and link the tables via foreign key. Then you can simply do a join to get the proper `userName` and `assetName`. Otherwise, you will unnecessarily have to manage both of those in a situation like you stated above.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [February 25, 2025, 5:36pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/4 "2025-02-25T17:36:33Z")

</div>

Foreign keys are the way to link tables together in this manner and maintains data integrity. You can also set different behaviors for when foreign key values change as in `On UPDATE` and `On DELETE` actions. The options are

1. No action
2. Cascade
3. Set Null
4. Restrict

4 and 1 do the same thing, it prevents foreign key values to be altered. 2 will update all entries that refers to the foreign key that is altered (there is overhead involved here). 3 will set the FK value to null.

---

<div class="post-metadata">

**Author:** ![William\_Howard](https://avatars.discourse-cdn.com/v4/letter/w/a4c791/32.png) [@William\_Howard](https://forum.inductiveautomation.com/u/William_Howard)\
**Post date:** [February 25, 2025, 5:47pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/5 "2025-02-25T17:47:17Z")

</div>

ok we currently do have are two tables linked but are running into the problem where when we make a change on are parent the chilled doesn't update so i changed are option to onUpdate and that seems to have fix are data righting back and forth and just some more testing to make sure it is fully working. I am wondering still about what problems would come from me only having the two tables and linking them together im not sure i understand why we need a 3rd table was this just to help with queries and making sure the data we want is pulled from both tables with the use of just an ID?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 25, 2025, 5:54pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/6 "2025-02-25T17:54:00Z")

</div>

_Dude!_ Grammar! Punctuation!

These matter in professional contexts, and are vital to non-native-speakers being able to participate. And are enough like gobbledygook for people like me to simply _ **skip** _.

---

<div class="post-metadata">

**Author:** ![William\_Howard](https://avatars.discourse-cdn.com/v4/letter/w/a4c791/32.png) [@William\_Howard](https://forum.inductiveautomation.com/u/William_Howard)\
**Post date:** [February 25, 2025, 5:55pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/7 "2025-02-25T17:55:41Z")

</div>

sorry i have dyslexia i try my best.

---

<div class="post-metadata">

**Author:** ![William\_Howard](https://avatars.discourse-cdn.com/v4/letter/w/a4c791/32.png) [@William\_Howard](https://forum.inductiveautomation.com/u/William_Howard)\
**Post date:** [February 25, 2025, 5:57pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/8 "2025-02-25T17:57:38Z")

</div>

is there a way we can link tables together without needing the column to be a PK as we want are isVisible column to be linked but don't want it as a PK.

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [February 25, 2025, 5:58pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/9 "2025-02-25T17:58:07Z")

</div>

I am still unsure that you even need two tables for your scenario.

you want them to be the same so why are there two tables at all?

> [@William\_Howard](#):
>
> is there a way we can link tables together without needing the column to be a PK as we want are isVisible column to be linked but don't want it as a PK.

this strengthens the idea you don't need more than one table.

If you are wanting a parent child relationship between assets you can link a foreign key to a primary key in the same table to achieve this easily.

---

<div class="post-metadata">

**Author:** ![William\_Howard](https://avatars.discourse-cdn.com/v4/letter/w/a4c791/32.png) [@William\_Howard](https://forum.inductiveautomation.com/u/William_Howard)\
**Post date:** [February 25, 2025, 6:02pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/10 "2025-02-25T18:02:12Z")

</div>

The two tables is because when looking into SQL tables i found a lot saying not to make lists in one column and i need each asset to be able to be part of multiple groups. so I broke the groups out into a new table there could defiantly be a better way to go about this that im not aware of im vary new to SQL.

---

<div class="post-metadata">

**Author:** ![William\_Howard](https://avatars.discourse-cdn.com/v4/letter/w/a4c791/32.png) [@William\_Howard](https://forum.inductiveautomation.com/u/William_Howard)\
**Post date:** [February 25, 2025, 6:29pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/11 "2025-02-25T18:29:39Z")

</div>

So we managed to get everything linked it doesn't wright from the child to the parent when you make a change but it does from parent to child which works for how we will be using this. Ill be doing some more research into how I can maybe make this one table instead of two as it seems you are suggesting there should be a way so ill keep looking. Thanks for the help sorry for the bad spelling, grammar, and general comprehension of my messages i do try my best.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 25, 2025, 6:44pm UTC](https://forum.inductiveautomation.com/t/linking-tables-in-mysql/102853/12 "2025-02-25T18:44:07Z")

</div>

I would strongly suggest doing some basic research on [database normalization](https://en.wikipedia.org/wiki/Database_normalization) and, before you do _anything_ else in your database, stop and **plan** your DB structure. A few hours of research and planning in the short term will save you **days** in the future untangling things and regretting past decisions.

In general, nothing that might need to change down the road should be used as a primary key directly, (i.e. an asset _name_ is not suitable as a stable identifier if it might be changed) and, you're correct, you should not be storing _lists_ in a single row of a single column. You should always use additional table(s), as needed, to store multi-valued sets.
