Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create calculated field in mysql?

i have a table like below:

create table info (username varchar(30),otherinfo varchar(100));

now i want to alter this table to have new field and this field has to have default value as

md5(username)

something like below:

alter table info add NewField varchar(100) default md5(username);

how to do so?
Thanks for your help

like image 304
Alaa Avatar asked Nov 21 '10 07:11

Alaa


People also ask

How do you create a calculated field in SQL?

You can use the string expression argument in an SQL aggregate function to perform a calculation on values in a field. For example, you could calculate a percentage (such as a surcharge or sales tax) by multiplying a field value by a fraction.

How do I add a calculated column to a table in MySQL?

MySQL generated column's syntaxFirst, specify the column name and its data type. Next, add the GENERATED ALWAYS clause to indicate that the column is a generated column. Then, indicate whether the type of the generated column by using the corresponding option: VIRTUAL or STORED .

How do you calculate in MySQL?

Introduction to the MySQL SUM() function Here is how the SUM() function works: If you use the SUM() function in a SELECT statement that returns no row, the SUM() function returns NULL , not zero. The DISTINCT option instructs the SUM() function to calculate the sum of only distinct values in a set.

How do I create a calculated field in MySQL workbench?

In MySQL Workbench you can add a formula by selecting the row in the Columns tab, clicking the 'Generated' button, and adding the formula (without an equals symbol at the start) in the Default/Expression field.


2 Answers

Per MySQL docs (emphasis added) you cannot have an expression in a default value:

10.1.4. Data Type Default Values

The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column.

I've tested that the following trigger works for your intent:

CREATE TRIGGER MyTriggerName
  BEFORE INSERT ON info
  FOR EACH ROW
    SET NEW.NewField = md5(NEW.username);
like image 127
mechanical_meat Avatar answered Sep 28 '22 10:09

mechanical_meat


You should write a Trigger to achieve this functionality, given that MySQL has a md5 function built in or you are capable to either write or find a md5 algorithm for MySQL :)

Take a look @ http://dev.mysql.com/doc/refman/5.0/en/triggers.html

for the md5 function take a look here

http://dev.mysql.com/doc/refman/5.0/en/encryption-functions.html

Also, you should know that MD5 is not a secure algorithm anymore.

like image 25
Ranhiru Jude Cooray Avatar answered Sep 28 '22 10:09

Ranhiru Jude Cooray