How do I create a MySQL table in which the last column contains the sum of the first two columns, so whenever I add data to the first and second column, the last one should update automatically?

You can use generated columns for the purpose

The code would look like this for example

  1. CREATE TABLE TableName

  2. (

  3. Column1 INTEGER NOT NULL,

  4. Column2 INTEGER NOT NULL,

  5. Column3 INTEGER GENERATED ALWAYS AS (Column1 + Column2)

  6. )

Please note that Column3 would be VIRTUAL in the above case i.e. it would be generated on the fly without persisting the data to table.
If you want it to persist the value and not calculate it on the fly, you need to make it STORED
Like

  1. CREATE TABLE TableName

  2. (

  3. Column1 INTEGER NOT NULL,

  4. Column2 INTEGER NOT NULL,

  5. Column3 INTEGER GENERATED ALWAYS AS (Column1 + Column2) STORED

  6. )