How do I plus one in a table column? - mysql

I've got a mysql table column that acts as a counter (int). Each time I update that column I want the field value to get plussed with 1.
So if it was 45 I want it to be 46.
How could I do that with SQL?

You can use a query such as this one :
update your_table
set your_column = your_column + 1
where identifier = X
Of course, up to you to replace the names of the table and the columns ;-)
And to make sure the condition in the where clause is OK ;-)

MySQL v5.0.2 or higher supports triggers, which are pieces of code that are executed, whenever an insert/update/delete operation is made on one of the rows.
for more information about triggers in MySQL check this link

Since you want it to happen each time, you could use a trigger:
delimiter //
create trigger increment_field before update on your_table
for each row
begin
if new.your_column <> old.your_column then
set new.your_column = new.your_column + 1
end if;
end;//
delimiter ;
I am not entirely sure I understand your question. The above solution will make it so whenever a row is updated such that your_column is given a different value, it will instead make your_column = new_value+1. The use of new and old keywords and the conditional used on each row should be changed to suit your needs.

Related

MySQL: Modify column based on column values in same table

I've got two columns in the same table for my users: name-displayed and short-name.
name-displayed is populated with the full name of the user, for example "John Doe". In short-name, there is the short value, e.g. "john-doe" (essentially de-capitalized and hyphenated).
How would I amend the data in short-name based on the data in name-displayed? I'm sure I could use a self-join based on UPDATE, but I'm not sure how to implement a change in data across the columns.
Any help would be hugely appreciated!
You need to use the Lower and Replace functions for this.
See: Lower and Replace in the docs.
Update <table_name>
set `short-name` = REPLACE(LOWER(`name-displayed`), ' ','-')
where <conditions>;
In case you want this done automatically, you'll need to write a trigger as Walter_Ritzel suggests.
delimiter //
CREATE TRIGGER auto_set_short_name BEFORE INSERT ON account
FOR EACH ROW
BEGIN
SET NEW.`short-name` = REPLACE(LOWER(`name-displayed`), ' ','-');
END;//
delimiter ;
You could use triggers: Triggers
A trigger Before Insert/Update could solve that easily.
delimiter //
CREATE TRIGGER ins_sum BEFORE INSERT OR UPDATE ON table
FOR EACH ROW
begin
SET New.`short-name` = REPLACE(LOWER(NEW.`name-displayed`), ' ','-') ;
end;
//
Use backticks or this char: ```, to make sure the - is not interpreted as a minus sign.
update table a join table b on a.id = b.id
set a.short-name = b.name-displayed;
I understand you need to set name-displayed same as short-name,
if that is not the case
care to explain.
what you want to amend ??

PL SQL Trigger to update start time for row when a single column is updated

I'm fairly new to triggers and have already tried searching for a solution to my question with little results. I want to update a single row's start time column whenever it's active column is set to 1.
I have two columns ACTIVE (number) and START_TIME (timestamp) in my_table. I would like to create a PL/SQL trigger that updates the START_TIME column to current_timestamp whenever an update statement has been applied to the ACTIVE column - setting it to 1.
So far I have only seen examples for inserting new rows or updating entire tables which isn't what I'm looking to do. I'd have thought there would be a fairly simple solution to my problem.
This is what I've got so far from other examples. I know the structure of my solution is poor and I'm asking for any input to modify my trigger to achieve my desired result.
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (my_table.ACTIVE = 1)
begin
insert my_table.start_time = current_timestamp;
end;
\
you can use like this .it may help you
write the update query instead of insert query
CREATE OR REPLACE TRIGGER routine_active
AFTER UPDATE ON my_table
FOR EACH ROW
WHEN (new.ACTIVE = 1)
begin
update my_table set start_time =current_timestamp;
end;
I think it should be a BEFORE UPDATE, not AFTER UPDATE, so it saves both changes with a single action. Then you don't need the INSERT or UPDATE statements. I also added the "OF active" clause, so it will only start this trigger if that column was updated, which may reduce the workload if other columns get updated.
CREATE OR REPLACE TRIGGER routine_active
BEFORE UPDATE OF active ON my_table
FOR EACH ROW
BEGIN
IF active = 1
THEN
:NEW.start_time = current_timestamp;
END IF;
END;

MySQL automatic update for specific column instead of whole record

I have a table APPLEVARIETY.
It has the following columns:
id
family
color
description (TEXT)
description_last_update_time (TIMESTAMP)
For the latter column I would like to use 'ON UPDATE CURRENT_TIMESTAMP'. However, this will cause the timestamp to be updated if at least one of the other columns are updated (id, family, color).
Instead I would like to specify that the description_last_update_time will get the CURRENT_TIMESTAMP if only the description column is updated. It should ignore updates on the rest of the columns.
I have looked through the MySQL manual, other questions on stackoverflow and of course used google extensively but found nothing.
So in other words: is there some way to specify a certain column with ON UPDATE? Is it even possible or am I moving in the wrong direction?
No thats not possible using the ON UPDATE CURRENT_TIMESTAMP this will get updated when any column is updated.
You may however achieve the same using a trigger. For that you will first need to remove ON UPDATE CURRENT_TIMESTAMP from the table definition.
The trigger will look like
delimiter //
create trigger APPLEVARIETY_update before update on APPLEVARIETY
for each row
begin
if new.description <> old.description then
set new.description_last_update_time = now();
end if;
end;//
delimiter ;

MySQL Trigger Update with select from another table

Just learning about triggers and I'm created the following trigger;
CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where msisdn = new.msisdn order by dat DESC limit 1);
END;
However the value does not appear to be getting updated. Any ideas?
I've actually managed to solve this myself. Here is the updated code
CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON `incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where crm_record.msisdn = new.msisdn order by dat DESC limit 1);
END;
I needed to specify the table name prior to the column value on line 5.
Looks like you have a typo.
You have entered incremental with a trailing backtick instead of enclosing it in backticks.
It is likely that your trigger is now bound to a table called incremental` which I am assuming does not exist.
Since you have ruled out the above. I see that the UPDATE keyword is missing. Add UPDATE table before your SET line. Replace table with the name of your table.

MYSQL auto increase column entity by 1 on update?

I have a table: ID,name,count,varchar(255)
Now, what i'd like is to increase the "count" each time that row in the table is updated.
Of course, the easy way is to read first, get the value, increase by 1 in php, then update with the new value. BUT!
is there any quicker way to do it? is there a system in mysql that can do the ++ automatically? like autoincrement, but for a single entity on itself?
I see two options:
1.
Just add this logic to every update query
UPDATE `table` SET
`data` = 'new_data',
`update_counter` = `update_counter` + 1
WHERE `id` = 123
2.
Create a trigger that will do the work automatically:
CREATE TRIGGER trigger_name
AFTER UPDATE
ON `table`
FOR EACH ROW
BEGIN
UPDATE `table`
SET `update_counter` = `update_counter` + 1
WHERE `id` = NEW.id
END
Create a trigger:
http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Triggers are pieces of code that are "triggered" by the database on certain events. In your case, the event would be an update. Many RDBMS support triggers, so does MySQL. The advantage of using a trigger is that every piece of your PHP logic that updates this entity, will implicitly invoke the trigger logic, you don't have to remember that anymore, when you want to update your entity from a different piece of PHP logic.
you can look up at the trigger
or can do with the extra mysql query
update table set count=count+1 ;
UPDATE table SET name='new value', count=count+1 WHERE id=...
An SQL update can use fields in the record being updated as a source of data for the update itself.