Math expressions

rcand001's profile image rcand001 posted 2 years ago in General Permalink

I'm new to SQL and HeidiSQL. I've got all of my columns setup, with two where the DEFAULT needs to be an "Expression:" What I've tried so far continues to give me a SQL error (1064). The fields envolved are: sf_cost, ohead, oh_item, freight, adhes_sf, labor_sf, profit, sf_installed. Those in Bold are the ones I need a DEFAULT "Expression:" for.

The two expressions, which I'll express in spreadsheet like format, are:

  1. oh_item Expression - sf_cost*ohead
  2. sf_installed Expression - (sf_cost+oh_item+freight+adhes_sf+labor_sf)/((100-profit)/100)

Any help would be greatly appreciated!

ansgar's profile image ansgar posted 2 years ago Permalink

Assuming you are on MariaDB, this is how I would do it in HeidiSQL's table designer:

Description

Note the data tab shows such virtual columns just like a normal one, but its values are auto-generated:

Description

rcand001's profile image rcand001 posted 2 years ago Permalink

I failed to specify that the database I'm working in is MySQL

rcand001's profile image rcand001 posted 2 years ago Permalink

"Assuming you are on MariaDB, this is how I would do it in HeidiSQL's table designer:"

I've gone back and entered the formula in the Expression Column with the syntax you show. HeidiSQL will not allow the Virtuality Column to be changed from STORED to VIRTUAL for a generated field. When I changed the column to STORED the expression was accepted. This helped me quickly solve the second equation. Initially, I was attempting to go to the DEFAULT Column, choose a DEFAULT Expression, and enter the expression there. I'm not sure if entering an expression, with the correct syntax, as a Default would work but your suggestion pointed me in the direction of a solution that did work. Thanks for the help!

Please login to leave a reply, or register at first.