MySql update table with replace string1 string2 - mysql

I need replace string1 to string2 in column big table. My table weight is1.7GB. For update I will use:
UPDATE table
SET column = REPLACE( column, 'search', 'replace' );
So, I have several questions:
Will table lock during procedure? Should I hide the public part of my site?
How long will this procedure be executing?
How better to run this? Maybe 'screen'? if my ssh connection will closed.
MySQL 5.5.53

You shouldn't run replace on all the rows of the table.
Instead add a where condition as #reds said in the comments.
This will make your update much more efficient and is the correct way to do it.
UPDATE table SET column = 'replace' where column = 'search';

In my case:
UPDATE table SET column=REPLACE(column, 'search', 'replace') WHERE column LIKE "%search%";

Related

in SQL is there a code to fill in a new column when you add a new column to a table?

I find it difficult to fill a new column that I just added to an already existing table in SQL
I tried the Insert command, to fill in the table afresh, but couldn't see it true because the table has 1445 rows
You could specify a default value when you add the column:
ALTER TALE mytable ADD COLUMN new_column VARCHAR(10) DEFAULT 'my_value'
Alternatively, once you added the column, you could use an update statement:
UPDATE mytable
SET new_column = 'my_value'
Just like the answer above you can use ALTER TABLE to add a new column
ALTER TABLE dummy
ADD temp_column VARCHAR(50);
Then you can use the update and add a where statement to be more specific
UPDATE dummy
-> SET temp_column = 'DEFAULT'
-> where id > 10;
Alternatively you could try using Five, you can import your database in it as an SQL dump, it actually allows you to do way more with new columns, like copy data from an existing field in the table or fill in the values from a query
here is an image of Five prompting you to fill in values for a new column
there are other things as well like you can write queries directly in Five, reuse the queries and the tables can be generated with simple point and click.
Disclaimer: I work for Five

Replace a string in MySQL column using insert or update

I am trying to replace a string like "example.com" with "newdomain.com" in a MySQL table called "wp_options" with a column name "option_value".
I have seen a lot of other questions about replacement but the problem is that they are all using the REPLACE function, which damages the structure of my table because it removes the row (if matches) and inserts a new one(1), which makes the primary key and unique id disappears (for example, this is causing me to lose my theme configuration).
Is there a way to replace such string using the INSERT function? or the UPDATE?
A simple update statement should do it:
UPDATE wp_options
SET option_value = 'newdomain.com'
WHERE option_value = 'example.com'
EDIT:
If the requirement is to search within the value as clarified by the comment below, you can use the replace function:
UPDATE wp_options
SET option_value = REPLACE(option_value, 'example.com', 'newdomain.com')
WHERE option_value LIKE '%example.com%'

Replace text through the entire database without assigning table or column

I've been using the following query to replace certain data:
UPDATE wx3_t1 SET umo = REPLACE(umo, 'stringbefore', 'string after');
wx3_t1 = Table
umo = Column
I am looking for a way to update across the entire database, without needing to put the table or the column into the query.
Something as simple as just REPLACE('stringbefore', 'string after')
I realize that doing it this way is really aggressive but that's fine.
You can create a simple script that query information_schema.COLUMNS to get list of all your columns by table:
select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from information_schema.COLUMNS;
Then you have to iterate on the result to play your UPDATE query.

Fill all empty rows with a certain single value in mysql table?

I have a database, a table that is having empty rows '' nothing in them.
I need to fill all those empty cells with certain singe value.
How can I do it?
Thanks in advance.
You can't batch edit all columns one column at each time
UPDATE table_name SET COLUMN_name ='value' WHERE column_name=''
If you want to update all columns at once, write a php code for that and in a loop perform sql query!
Hope it helps!
You have several options, based on your definition of "empty rows".
if empty means null:
update tableName
set columnName = 'value'
where
columnName is null;
if empty means not null, instead an empty string:
update tableName
set columnName = 'value'
where
columnName = '';
UPDATE table_name
SET column1=value
WHERE length(column1)=0;
This should do you job.

Unique index not based on words order?

Is it possibile to have a mysql UNIQUE index on a varchar field not based on the words order?
I mean if there is a row with key1 key2, and I try to insert key2 key1 mysql should throw an error.
You could achieve this effect by adding before insert and before update triggers to the table; they could check whether a row with the fields reversed exists or not before inserting/updating if it doesn't, or forcing an error if it does.
See here for more information.
If you set the indexed to UNIQUE, you will not be able to enter the identical data twice - numbers, words or otherwise.
If you wish to achieve something otherwise, you'll have to set a new non-unique field in your database then you'll be able achieve it using external code and queries (PHP, C#).
Create a trigger to do this.
i.e.
CREATE TRIGGER trigger_name BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
IF ((SELECT COUNT(*) FROM table WHERE col2=NEW.col1 OR col1=NEW.col2) <> 0)
THEN
CALL this_procedure_does_not_exist();
END IF
END