How to automatically update database fields with random values in MYSQL - mysql

I understand how to fill a field with random values, but I need my field to automatically update with a random value for each new column. For some reason, I'm having trouble figuring out how to do this.
By the way, as it says above, this is for a database in MySQL.
Thanks everyone

You can use a trigger to update a specific colum after insert like this:
DELIMITER |
CREATE TRIGGER randtrigger AFTER INSERT ON your_table
FOR EACH ROW BEGIN
UPDATE your_table SET some_column = rand()
WHERE id = NEW.id;
END;
|
DELIMITER ;

Related

Use a mysql trigger to replace text in column

everybody!
I have a table with name order and into order have column payment_method, I need find in this columns specific text (Bank transfer) and replace with (bank) after into insert to table value Bank transfer, how I do this via MySQL triggers?
I tried to set trigger with this value
delimiter |
FOR EACH ROW
BEGIN
IF order.payment_method like %Bank transfer% THEN
SET order.payment_method = 'Bank';
END IF;
END
|
delimiter ;
And trigger parametr
Time:AFTER
Event:UPDATE
Please help!
Thanks!
CREATE TRIGGER tr_bi_order
BEFORE INSERT
ON order
FOR EACH ROW
SET NEW.payment_method = CASE WHEN NEW.payment_method LIKE '%Bank transfer%'
THEN 'Bank'
ELSE NEW.payment_method
END;
And the same trigger on BEFORE UPDATE event.

MYSQL Stored procedure for concatenating code

I have a table which contain some fields(id,code,name,...), when I want to insert new record the code field should be like (ABC-001) and next record should be like (ABC-002) and so on.
I want to write a MYSQL procedure for this but I don't know how, any body has any idea? thanks in advance
First User Insert Query like:
Insert Into YourTable Values(..,'ABC',...);
Now write trigger after update which set Code value in expected format:
CREATE TRIGGER TriggerNmae AFTER INSERT ON YourTable
FOR EACH ROW
Begin
UPDATE YourTable
SET code = concat(code,'-',LPAD(NEW.id,3,'0'));
WHERE id = New.id ;
END;

History field in MySQL database

Trying to create a field in a table that will get the old value of another field. My table is very similar to this:
------------ -------------------- ------------------------
| id int(10) | price decimal(5,2) | price_old decimal(5,2) |
----------------------------------------------------------
What I am trying to get is the value of price field to get copied in price_old field ON UPDATE of the current record.
Is it possible to achieve this only with mysql?
PS: The data contained in that cell is not critical. The usage is to store the previous price of a item and after to be able to show how it changed. I will only need the last value, not the full history of the table entry. (My MySQL server is 5.0, if this matters)
Yes, you can use a BEFORE UPDATE trigger (documentation).
I think this syntax is right but haven't used MySQL in a while so you may have to toy with it. If it's wrong and you get it working please let me know so I can edit it.
DELIMITER $$
CREATE TRIGGER `price_trigger`
BEFORE UPDATE ON `products`
FOR EACH ROW
BEGIN
IF NEW.price != OLD.price
SET price_old = OLD.price;
END IF;
END$$
DELIMITER ;
Use triggers on update rule. There set value of price to price_old
CREATE TRIGGER `update_price_old`
BEFORE UPDATE ON `table_name` FOR EACH ROW
BEGIN
UPDATE `table_name` SET price_old = OLD.price where OLD.id = id;
END

MySQL triggers cannot update rows in same table the trigger is assigned to. Suggested workaround?

MySQL doesn't currently support updating rows in the same table the trigger is assigned to since the call could become recursive. Does anyone have suggestions on a good workaround/alternative? Right now my plan is to call a stored procedure that performs the logic I really wanted in a trigger, but I'd love to hear how others have gotten around this limitation.
Edit: A little more background as requested. I have a table that stores product attribute assignments. When a new parent product record is inserted, I'd like the trigger to perform a corresponding insert in the same table for each child record. This denormalization is necessary for performance. MySQL doesn't support this and throws:
Can't update table 'mytable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. A long discussion on the issue on the MySQL forums basically lead to: Use a stored proc, which is what I went with for now.
Thanks in advance!
You can actually up the rows in the same table as the trigger. The thread you linked to even has the solution.
For example:
TestTable ( id / lastmodified / random )
create trigger insert_lastmod
before insert on TestTable
for each row
set NEW.lastmodified = NOW();
insert into TestTable ( `random` ) values ( 'Random' );
select * from TestTable;
+----+---------------------+---------------------+
| id | lastmodified | random |
+----+---------------------+---------------------+
| 1 | 2010-12-22 14:15:23 | Random |
+----+---------------------+---------------------+
I suppose you could call the stored proc in your trigger. HOwever, if you want to update some fields in the same records that you are changing (such as an updatedby or lastupdated column) then you can do this in a beofre trigger according to the refernce manual. http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
This is a common operation for triggers and I find it difficult to believe it isn't supported.
If you want to update column that you don't read in trigger function, then as a workaround, you could put that column into separate table.
You can actually do that
The below is an example for same
DELIMITER $$
create trigger test2
before insert on ptrt
for each row
begin
if NEW.DType = "A" then
set NEW.PA = 500;
elseif NEW.DType = "B" then
set NEW.PA = 1000;
else
set NEW.PA = 0;
END IF;
END;$$
DELIMITER;
This worked for me :D
On Before / Update.
BEGIN
SET NEW.DateTimeUpdated = NOW();
END

MySQL grouping columns

is there any way i can group several of my columns together?
in one for my scripts i need to set several of my columns to the same value, is there a better way than typing out all the column names in my update query?
thanks
If these columns MUST always have the same value (that seems pointless) you could set an UPDATE TRIGGER on the column
delimiter |
CREATE TRIGGER somename BEFORE INSERT ON table1
FOR EACH ROW BEGIN
SET table1.col2=NEW.col1, table1.col3=NEW.col1;
END;
delimiter ;
But if that was the case, it would really make sense to just use ONE column instead of three.
Or, if its a Rights issue (this user shouldn't have the permission to make the columns different) you could build a Stored Procedure for doing the update
delimiter |
CREATE STORED PROCEDURE somename(IN val INT,IN whereval INT)
BEGIN
UPDATE table1 SET table1.col1=val, table1.col2=val, table1.col3=val
WHERE table1.id=whereval;
END;
delimiter ;
You'll need to type each column's name, there is no workaround.
UPDATE mytable
SET col1 = 1,
col2 = 1,
…
No, this is not possible.
See the syntax for update:
...
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}
...
Update syntax for MySQL requires each column to be named explicitly
update table set a = 'value', b = 'value'