2

I know its possible to autoincrement values, but i was wondering if its possible to fill a field based on the value of two other fields. I have a table with the fields:

CREATE TABLE pligg_links (
  ...
  link_votes INT,
  link_reports INT,
  link_votes_total INT,
  ...
);

Field link_votes_total should hold the value of field link_votes subtracted from link_reports. So basically, this is the math equation: link_votes_total = link_votes - link_reports. Is this possible without having to use php to do it before data is stored?

1
  • 1
    @jiexi: code is always appreciated as it's concise and unambiguous, and can be clearer than human languages. I'm guessing at the table structure. Feel free to correct it.
    – outis
    Feb 25, 2010 at 1:30

2 Answers 2

3

Yes, this can be done by creating a trigger for BEFORE INSERT and another one for BEFORE UPDATE:

DELIMITER //

CREATE TRIGGER trig_mytable BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
    SET NEW.link_votes_total = NEW.link_votes - NEW.link_reports;
END
//

CREATE TRIGGER trig_mytable BEFORE UPDATE ON my_table
FOR EACH ROW
BEGIN
    SET NEW.link_votes_total = NEW.link_votes - NEW.link_reports;
END
//

DELIMITER ;

Further Reading:

10
  • care to link me to a tutorial? Thank you!
    – jiexi
    Feb 25, 2010 at 1:20
  • where would i put this? Im using a phpmyadmin equivalent. sorry, im a noob :(
    – jiexi
    Feb 25, 2010 at 1:25
  • also, what about updates, will this check be performed when rows are updated aswell?
    – jiexi
    Feb 25, 2010 at 1:26
  • @jiexi: Check this question regarding Triggers and phpMyAdmin: stackoverflow.com/questions/2324924/where-i-write-mysql-trigger... Update my answer with a trigger for UPDATEs as well. Feb 25, 2010 at 1:26
  • This will almost work. MySQL does not support this INSERT OR UPDATE construct. You have to write 2 separate triggers. See my answer. Feb 25, 2010 at 1:31
1

See:http://dev.mysql.com/doc/refman/5.1/en/triggers.html

DELIMITER //

CREATE TRIGGER bir_links
BEFORE INSERT ON links
FOR EACH ROW 
BEGIN
    SET link_votes_total = NEW.link_votes - NEW.link_reports;
END;
//

CREATE TRIGGER bur_links
BEFORE UPDATE ON links
FOR EACH ROW 
BEGIN
    SET link_votes_total = NEW.link_votes - NEW.link_reports;
END;
//

DELIMITER ;
1
  • DELIMITER // CREATE TRIGGER bir_links BEFORE INSERT ON pligg_links FOR EACH ROW BEGIN SET link_vote_total = NEW.link_votes - NEW.link_reports; END; // CREATE TRIGGER bur_links BEFORE UPDATE ON pligg_links FOR EACH ROW BEGIN SET link_vote_total = NEW.link_votes - NEW.link_reports; END; // DELIMITER ; failed : 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 'DELIMITER // CREATE TRIGGER bir_links BEFORE INSERT ON pligg_links FOR EACH ROW ' at line 1 Im using virtualmin/webmin btw
    – jiexi
    Feb 25, 2010 at 1:38

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.