0

Is there a way to set a field to 0 whenever that row gets updated without using PHP or explicitly including that in my queries?

4 Answers 4

1

You can set a default value = 0 when defining the table.

From CREATE TABLE Syntax:

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
   .../...
column_definition:
    data_type [NOT NULL | NULL] [DEFAULT default_value]

Let's say

CREATE TABLE test (
a INT,
b INT DEFAULT 0);

If we do

INSERT INTO test (a) values (1),(2),(3);

Then when we

SELECT * FROM test

we will get

a  |  b
1     0
2     0
3     0

See it working on SQL Fiddle

2
  • The field already has a default value set. What I need is the value of that field to get changed back to 0 (if it was updated to something else before) if there is an update done to that row. Mar 22, 2013 at 22:50
  • Ah, I see. Well, it is a matter of creating a trigger, like @Sebastian is suggesting. Hope a mixure of answers help you.
    – fedorqui
    Mar 22, 2013 at 22:54
1

Use a trigger:

CREATE TRIGGER set_zero AFTER UPDATE ON table_name
   FOR EACH ROW
      UPDATE table_name SET col_name=0 WHERE xyz=cond_xyz;
;

Didn't test it.

4
  • Yes. This looks like something that might work. I'll have to do more research into how to apply this to my actual use case. Thanks. Mar 22, 2013 at 22:54
  • How would I go about just updating that one row? This seems like it would update all rows. I see the WHERE clause, but how would I put in WHERE id="ID of only the row that was updated" Mar 22, 2013 at 22:58
  • I don't know! But I found this thread: stackoverflow.com/questions/2334478/… which says that my code above will cause infinite recursion which is true. It also has a solution. ANother thread stackoverflow.com/questions/9227382/… says that using BEFORE will do the trick.
    – 8192K
    Mar 22, 2013 at 23:01
  • MySQL uses row level triggers, you don't update the table, you just assign values to the new record that is being passed into the trigger.
    – user330315
    Nov 3, 2013 at 9:54
0

This seemed like a bizarre request to me at first, but it actually is possible in MySQL. If you specify 0 as the default for that field, you can take advantage of the fact that REPLACE INTO uses defaults for unspecified but existing columns.

INSERT INTO t1 (id, numToZero, value) VALUES (1, 1345, 'value');

Later, when you want to update:

REPLACE INTO t1 (id, value) VALUES (1, 'new value');

This will yield (1, 0, 'new value') without creating a new row.

0

A trigger is the right way to go, but Sebastian got it all wrong.

You don't run UPDATE inside a trigger you just assign the value to the new record that is being passed to the trigger. Additionally it has to be a BEFORE UPDATE trigger (because a change to the new record in an AFTER trigger won't be persisted).

CREATE TRIGGER set_zero 
  BEFORE UPDATE ON table_name
  FOR EACH ROW
BEGIN
  SET new.col_name = 0; 
END;

Note that you can still supply a non-zero value during insert even if a default value is defined. If you don't want that either, you also need an insert trigger

Here is an SQLFiddle example: http://sqlfiddle.com/#!2/cebe82/1

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.