How to backfill a column in MySQL? - mysql

I have a MySQL database for production environment. Recently we added a new column to a table. So in this table, some rows have NULL value for this column. Now we want all the rows to have a non-null value so we have to backfill the data.
What is the common approach to do backfilling? If I want to write a SQL script to do the backfilling, is there a way to abort the whole operation if any error happens?
Thank you!

Just update the table with non-null value.

Related

Using trigger to check conditional uniqueness (MySQL)

I have a MySQL table, that has a unique index constraint over multiple (2) columns: "username", and "provider". The index works fine and does not allow inserting rows with duplicated usernane+provider combination.
But I do want to allow insertion of the same usernane+provider combination in case another column, "active", has the value false. I do not have a problem with multiple duplicate rows all marked as active FALSE.
MySQL, unlike Microsoft SQL Server, does not support conditional/filterable indexing.
While I could not find a more appropriate solution, I thought to use a trigger (after insertion to the table) to check whether the value that was just inserted is duplicated, considering the "active" value.
Can I get the trigger to fail the INSERT statement? Is there a better solution for this case?

Do you need to update the data when it doesn't change

I want to update a row in the tableļ¼Œbefore updating, do I need to check if there is any change in each column?
In MySQL, you do not need to check the value. MySQL does not update the record if there is no change. That means that MySQL does not incur the overhead of logging or writing the data. There is a slight cost of checking if any values changed, and I think it does run the triggers.
Other databases behave differently.
This is in an arcane location in the documentation, where mysql_affected_rows() is described:
For UPDATE statements, the affected-rows value by default is the
number of rows actually changed.
. . .
An integer greater than zero indicates the number of rows affected or
retrieved. Zero indicates that no records were updated for an UPDATE
statement, no rows matched the WHERE clause in the query or that no
query has yet been executed.
That would be totally up to you to check if anything is in there before hand. You can do updates based on a single row, multiple, or all of them.
An example where you update a specific row is like this:
UPDATE your_table
SET Username='TestName123'
WHERE UserID='12486';
where you would be changing the username where the userid is 12486
OR you can update all of the rows with data you want like
UPDATE Customers
SET Country='USA'
This would update every record to have the Country column be filled with USA.

phpMyAdmin: Create column that increments after updating any data in its row

I have an SQL database with over 800 entries and 40 values for each entry. I would like to add a column that increases every time any of that row's values change. It would be like a version number for each individual row so I know which rows have edited data.
I know little to nothing about sql coding, I just use phpmyadmin to hold my data. Is something like this possible without adding some sort of function, and if not, how would I go about implementing something like this. Any input would be appreciated.
You can use triggers to do this job. Run the following query in phpmyadmin after editing the table name and column name for count
CREATE TRIGGER incr_on_update BEFORE UPDATE ON yourtablename
FOR EACH ROW SET NEW.count =OLD.count+1;

Is it possible to give condition in MYSQL database column?

I am using MYSQL database for my application. Is it possible to give condition in each columns to accept values within the range.
For example, Lets say I am having column 'age' of integer type in a table. During insert statement this column should accept value only from 0-100. i.e it should not accept negative values or greater than 100. If so, it should return me an error.
How could I setup this condition in MYSQL database?
Is there any alternative option to do it?
Thanks in advance.
MySQL doesn't support CHECKs, you must do it in your application or use a Trigger or Stored Procedure to check for wrong data:
The CHECK clause is parsed but ignored by all storage engines.

MySQL: UPDATE trigger. Obtain the value of a column used in UPDATE's where clause if it fail to match any row?

MySQL: In update trigger's body, can I obtain the value of a column that is specified in the where clause of the triggering query if the where clause does not match any rows at all?
I have to do the following, but NOT USING direct query such as ON DUPLICATE KEY UPDATE so on:
If I have:
UPDATE my_table SET idiotism_level=5 WHERE name='Pencho'
... and the where clause match NO ROWS, I'd want to automatically trigger an insertion of a row having name='Pencho' before the update, and then the UPDATE would presumably match, and work properly.
Is it possible ?
This could be make in a RULE in other database systems (PostgreSQL), that does not exists in MySQL. It's a Rule and not a trigger as you should analyse the query and not the result of the query.
But for MySQL you can make pre-query jobs by using MySQL-Proxy. You should be able to alter your update query and build an insert, By running some 'check row exists' extra query from the MySQL-Proxy (I'm not saying this is a nice solution, but if you have no way to make the code to act better you can fix it at this level).
No. An update trigger fires once for each row that gets updated, not once for each update command that's executed. There's no way to make the trigger fire if nothing is updated. You would need to handle this in your application by checking the number of updated rows returned by your query.
If name has a unique index on it you can use REPLACE
REPLACE INTO my_table (idiotism,name) VALUES ( 5,'Pencho');