-1

This question continue to the previous question How to create Trigger to prevent inserting the duplicate row in MYSQL 5.0.27?

Now I have a I have a table MyText which have ID (Auto incrementation ) column, text (varchar 700, utf8) column

Id - text
1  - this is my text
2  - xxxx

Now I want to add a new column "textHash" which is SHA1(lower(text)). That means everytime there is a new text inserted into text column, then the "textHash" column will calculate the hash value & updated accordingly.

Ex, at the beginning

Id - text            - textHash
1  - this is my text - 5sd4as55ads
2  - xxxx            - zxcz5454

Then if someone insert into myTable (text) values ('new text'), then the table will be like this:

Id - text            - textHash
1  - this is my text - 5sd4as55ads
2  - xxxx            - zxcz5454
3  - new text        - 212121zxc

SO how to deal with it?

1
  • Triggers is what you want. Alternatively, you could consider using computed columns Aug 7, 2014 at 2:17

2 Answers 2

1

Use the following for the trigger:

delimiter //
create trigger texthash
before insert on mytext
for each row
begin
   set new.txt_hash = sha1(lower(new.text));
end;
//
delimiter ;

Fiddle example is here: http://sqlfiddle.com/#!2/67b15/1/0

You have to set the delimitter to something other than a semicolon (in my example, // ) to differentiate between the start and end of the trigger, as there is a semicolon within the statement. (You can set it back to a semicolon immediately after the trigger is created)

Note that the SHA1 value is always 40 characters regardless of the length of the text field. Ie. the SHA1 hash of 'new text' is 1eb8a29393c0e376645481c0185ec8ef05c0b65c

Regarding your comment, try formatting what you posted exactly as follows (line by line): Only thing I noticed is you forgot a semicolon after the SET

DELIMITER $$
CREATE TRIGGER textHash_InsertTrigger
BEFORE INSERT ON myTable
FOR EACH ROW
begin
   set new.textHash=sha1(lower(new.text));
end;
$$
DELIMITER ; 
4
  • u forgot sha1(lower(new.text))? since I don't care about case
    – Tum
    Aug 7, 2014 at 2:18
  • it said the "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set new.textHash=sha1(lower(new.text)) end' at line 1"
    – Tum
    Aug 7, 2014 at 2:24
  • DELIMITER $$ CREATE TRIGGER textHash_InsertTrigger BEFORE INSERT ON myTable FOR EACH ROW begin set new.textHash=sha1(lower(new.text)) end; $$ DELIMITER;
    – Tum
    Aug 7, 2014 at 2:24
  • @Tum probably just the semicolon after your SET (it's not there), but try using what I just put into my answer above to clarify anything else Aug 7, 2014 at 2:31
0
create trigger hashing before insert on table
for each row begin
   set table.textHash= md5(text);
end;

Not too sure about the syntax but the logic should be correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.