mysqljdbcapache-commons-dbutils

Is it possible in mySql to insert a column that is a mathematical function of two other columns?


Specifically, I'd like to insert a third column (c3) that equals c1 divided by c2 rounded to the nearest integer. I've looked at mySql cookbooks and skimmed through O' Reilly's introduction, but I've yet to see anything that would suggest this is possible.

Using JDBC, Apache's DBUtils, or a simple mysql script are all possibilities for me.


Solution

  • Create a view on the base table:

    create view myview as
    select c1, c2, round(c1 / c2) as c3
    from mytable
    

    You can treat myview like a regular table, except you won't be allowed to update or insert values in the calculated column.