mysql update table set field equal php function - mysql

Is there a way to update a field of a table with the value coming from php built-in function?
UPDATE table SET field1 = hash("ripemd256", field2)

Related

Sum up columns using group by and store as a new column (permanently) in MySQL

I have 50 columns in the MySQL table. I want to sum all these columns and make a new column into the same table (sum50).
This should be stored permanently into the MySQL table whenever I update or insert new data.
I know how to sum up while performing the query but it does not store into the table permanently.
CREATE TRIGGER `name`
AFTER INSERT ON `table`
FOR EACH ROW
UPDATE `table` SET `table`.`coulms` = `table`.`col1`+.... `table`.`col50`+
I am trying the above trigger, but not working.
Should I have a blank column inserted into the table and perform trigger? Which trigger would be correct?
Instead of the trigger, add a generated column to your table:
alter table tablename add(sum50 int generated always as (col1 + col2 + ...) stored);
See a simplified demo.

How to auto increment in phpmydamin with custom number and character string

How can I set my custom value in auto increment column when insert query fire in phpMyAdmin
I want like when I insert a data in MySQL at that time the auto increment column value add with like something ABC001 and next record will be ABC002
It there any way to setup this functionality
This is my table structure
I want new code for every insert query for red mark field
Yes, you can use a TRIGGER like this:
CREATE DEFINER=`root`#`localhost` TRIGGER `setDrinkk` BEFORE INSERT ON `yourTable`
FOR EACH ROW BEGIN
SET NEW.DrinkCode = CONCAT('ABC-', LPAD(NEW.db_id,4,'0'));
END
You'll have to add 2 more fields to the table..One field(eg:drink_text)containing the text 'ABC' and other field (Eg:drink_no) with autoincrement.
Set default value of drink_text as ABC.
Set autoincrement primary key for drink_no
ALTER TABLE tablename AUTO_INCREMENT=101;
Now in insert query you have to concatenate both the fields to drink_code
//your insert query
INSERT INTO tablename( user_id,club_id) VALUES ('xyz','abc');
//drink_code with custom string
UPDATE tablename SET drink_code = concat( drink_text, drink_no ) ;

MySQL update trigger - how to detect unpassed values?

Let's say I have these possible queries:
UPDATE table SET field1 = 2 WHERE id = 1;
UPDATE table SET field1 = 4, field2 = 8 WHERE id = 16;
table has a BEFORE UPDATE trigger that calculates a new value for field2 based on field1, but I want it to only do this if I don't specifically pass a value.
I am detecting a change to field1 with IF OLD.field1 != NEW.field1 THEN ..., but how do I detect field2 being passed or not, regardless of the value actually changing?
If MySQL does not receive a value through the UPDATE for field2 it will contain the old value in NEW.field2.
In Oracle you will get a NULL if the field2 is not passed trough the update.
If NEW and OLD values are the same you can assume that no update is required for that field. MySQL will also return 0 rows updated if all NEW values match what is already there in the table.

SQL Server 2008 - Use IDENTITY field when inserting a row

My table has 3 columns ID, ISOCode and CountryName.
Column ID is an IDENTITY column.
When I insert new records into this table, I want to populate the ISOCode field - on occasion - with the same value as the ID field.
I've tried SCOPE_IDENTITY(), ##IDENTITY and IDENT_CURRENT but none of these seems to work.
Is there a way I can do this?
you could write a trigger to update the ISOCode column
In Trigger use the code below
update T set T.ISOCode=I.ID
from your_table T
join INSERTED I
on T.ID=I.ID

Insert the same fixed value into multiple rows

I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this?
I've tried INSERT INTO table (table_column) VALUES ("test"); but that only populates that last row. How do I do all of the rows at once?
You're looking for UPDATE not insert.
UPDATE mytable
SET table_column = 'test';
UPDATE will change the values of existing rows (and can include a WHERE to make it only affect specific rows), whereas INSERT is adding a new row (which makes it look like it changed only the last row, but in effect is adding a new row with that value).
This is because in relational database terminology, what you want to do is not called "inserting", but "UPDATING" - you are updating an existing row's field from one value (NULL in your case) to "test"
UPDATE your_table SET table_column = "test"
WHERE table_column = NULL
You don't need the second line if you want to update 100% of rows.
To update the content of existing rows use the UPDATE statement:
UPDATE table_name SET table_column = 'test';
What you're actually doing is adding rows. To update the content of existing rows use the UPDATE statement:
UPDATE table SET table_column = 'test';
UPDATE `table` SET table_column='test';
The SQL you need is:
Update table set table_column = "test";
The SQL you posted creates a new row rather than updating existing rows.
To create a new empty column and fill it with the same value (here 100) for every row (in Toad for Oracle):
ALTER TABLE my_table ADD new_column INT;
UPDATE my_table SET new_column = 100;