Inter related records

It may be showing how much of a noob I am at this but I would like to know, is there a way to code a record so that it is automatically updated when OTHER records are updated. Kinda like in spreadsheet when you can make cell A = cell B+ 2*Cell C?

just curious. Thanks.

Imo it’s bad practice to store values in columns that can be calculated from other columns in the row or from elsewhere.

Storing “technically redundant” data firstly increases tables sizes needlessly and secondly increases the chance of reducing the integrity of the data in the table if someone accidentally enters a value for, say, area which is incorrect for the radius and/or diameter value in that row.

Your table really only needs columns “name” and “radius” or “diamter”. Circumference and area can be calculated from the radius or diameter.

Bur for the sake of this exercise, you could use an insert statement something similar to this -

insert into circle
values ('circle2',5,radius*2,3.1416*diameter,3.1416*radius*radius);

Yup, it’s called a trigger :slight_smile:

Thank you Kalon. :slight_smile:
And, BTW,I was under the impression that the less you had to send back and forth from the DB the better… so I thought If i started storing values it may reduce the # of calls W/O having to use functions to recalculate them in my PHP. but I see where you are coming from… And I rather not take the chance of corrupting my data.

Wow… that looks interesting… and very advanced. but it also seems to be a query. What i am after is a way to have cells in the same table relate to each other … probably my terminology is off.

For the sake of a simplified example: lets say I have set up an existing table ‘Cricle’ in a DB with the fields:
Name | Radius | Area | Circumference| Diameter
the PHP script inserts a row filling the name and the radius fields. I suppose in my example I could use PHP to perform the calculations, then insert the calculated values into their corresponding fields. but I am looking for an alternative. Since I dont need EVERY ROW of a Table being acted upon, in fact I am only acting on the row being created… more specifically filling values from cells in that row being created to create the rest for the cells of that row