How to use replace to update the column items - mysql

I'd like to change the ',' char into '.' at one of my column . I can make the replace syntax but it just a view. And i want to update it for the all records!
select cast(replace(egysegar,',','.')as decimal (10,3)) from temporary
i'll tried the next one to update
UPDATE temporary set egysegar = replace(egysegar,',','.') where egysegar is not null
but after i get an error :
you are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.

Most probably you're using MySQL Workbench and you actually have to disable the safe mode or add in the WHERE clause a key column, exactly as it says in the message.
Or you can run SET SQL_SAFE_UPDATES=0; before your update query.

Possible answer is that MySql run with safe-updates option. It mean you can't update or delete record without key primary key on where clause.
SET SQL_SAFE_UPDATES=0;
or use primary key in where clause

Related

Why does MySQL safe update mode not allow a LIKE predicate?

Please consider the following scenario:
I have a MySQL table called actor. (Yes, it's from the Sakila sample DB).
In the table there's a varchar column called last_name.
There is a (non-PRIMARY) KEY on the column.
MySQL safe update mode is turned on.
When I run this query, it works:
DELETE FROM actor WHERE last_name = 'foo';
When I run this query:
DELETE FROM actor WHERE last_name LIKE '%fo' OR last_name LIKE 'fo%';
It fails with this error message:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
Why does safe update mode stop the second command and allow the first command?
According to the documentation:
It is possible for UPDATE and DELETE statements to produce an error in safe-updates mode even with a key specified in the WHERE clause, if the optimizer decides not to use the index on the key column.
A test like LIKE 'fo%' will normally use the index (the index can be used to match the beginning of a column), so it shouldn't cause the error. But a test like LIKE '%fo' cannot be indexed (it doesn't specify the beginning of the column, so a full scan is necessary), so you get an error.

DELETE query results in 'Query Interrupted' MySQL Workbench?

I can successfully delete records manually by click-selecting & deleting row(s) but executing delete queries result in 'Query Interrupted'.
My deletion queries are in the form:
DELETE FROM table where column = value;
The select statement uses the same values:
SELECT * FROM table WHERE column = value;
and returns desired results.
What could be causing the delete statement to fail? Are there limits on the amount of records you can delete at once in workbench?
If you wish to delete the entire contents of a table you can use Truncate.
TRUNCATE [TABLE] tbl_name
Please see the docs: https://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
Using the DELETE function is usually used for deleting single rows.
According to the documentation, in the Preferences >> SQL Editor >> Other, the Safe Updates setting is on by default.
Safe Updates (rejects UPDATEs and DELETEs with no restrictions)
Enabled by default. Prevents UPDATE and DELETE queries that lack a corresponding key in a WHERE clause, or lack a LIMIT clause, from executing. This option requires a MySQL server reconnection.
When selected, this preference makes it possible to catch UPDATE and DELETE statements with keys that are not used properly and that can probably accidentally change or delete a large number of rows.
I think what this says is that if the setting is on, then the column you are filtering by in the DELETE or UPDATE statement must be the primary key, it cannot be just any column.
If you change the setting to off, then you might need to restart MySQL Workbench for the change to take effect (at least under Linux).
There is a default thousand-row limit in MySQL-Workbench. The SELECT query will return results but DELETE will fail if the number of records to be deleted exceeds one thousand. One option is to limit the results in the query itself or you can adjust the settings as stated in the documentation.

MySQL Add Column with Online DDL

I'm currently trying to add a column to a table of ~25m rows. I need to have near-0 down time, so was hoping to use online DDL. It runs for a while, but eventually runs into the issue:
"Duplicate entry '1234' for key 'PRIMARY'"
[SQL: u'ALTER TABLE my_table ADD COLUMN my_coumn BOOL NOT NULL DEFAULT false']
I think this is happening because I'm running INSERT ... ON DUPLICATE KEY UPDATE ... operations against the table while running the operation. This seems to be a known limitation.
After this didn't work, I tried using the Percona pt-online-schema-change tool, but unfortunately, because my table has generated columns, that didn't work either with error:
The value specified for generated column 'my_generated_column' in table '_my_table_new' is not allowed.
So, I'm now at a loss. What are my other options for adding a column without blocking DML operations?
Your Alter statement is creating a non nullable column with a default of false. I'd suspect this to place an exclusive lock on your table, attempt to create the column, then setting it to False across each row.
If you don't have any available downtime, I'd suggest you
Add the column as nullable and with no default
ALTER TABLE my_table ADD COLUMN my_coumn BOOL NULL;
Update the values for existing rows to false
update my_table set my_coumn=false;
Alter the table a second time to be not nullable and with a default.
ALTER TABLE my_table modify my_coumn BOOL NOT NULL DEFAULT false;
Alternatively you could use something like Percona which manages schema changes using triggers and is meant to offer the ability to update schemas without locking the table.
Either option I'd suggest you test in your development environment with some process writing to the table to simulate user activity.

Where clause using key column still gives an error

I have a table that serves as a foreign key lookup from another table. The table is very simple, containing a ID column with is the primary key and a JSON column. I wish to remove abandoned entries from this table.
I tried running this script:
DELETE
FROM `ate`.`test_configuration`
WHERE `ate`.`test_configuration`.`ID` NOT IN (SELECT DISTINCT `ate`.`index`.`TestID` from `ate`.`index`);
But encountered an error stating my I wasn't using a where clause that uses the key column:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
This is confusing as my where clause does use the primary key column. I am aware that I can disable safe mode as part of my script as a workaround, but would still like to understand why I'm getting this error. I'd like to avoid unsafe updates if possible.
I believe Optimizer just unable to use index effectively for such query - so it does full table scan.
How many rows are in the test_configuration and how many of them will be deleted?
(You might try to use index hints to force optimizer to use index for the query, just not sure if they are supported in your version of mysql).

error appear when I execute a SQL request

I have a SQL request that I can't execute ,this error appear:
To disable safe mode, toggle the option in Preferences -> SQL Editor -> Query Editor and reconnect.
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
My request:
update customfieldvalue
set stringvalue="****"
where customfieldvalue.customfield IN
(select customfield.id
from customfield
where customfield.cfname="Resoltion"
and customfieldvalue.issue=12345);
how can I fix this problem,because in my production environment I can't do this manipulation?
PS:I'am using mysql
To disable it just for the current session run:
SET SQL_SAFE_UPDATES=0;
To fix this problem:
To disable safe mode:
Toggle the option in Preferences -> SQL Editor -> Query Editor and reconnect.
The cause:
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
(Did you read the message? It explains everything)
If you can't turn off safe update in production, you'll need to rework your query to involve at least one key column in customfieldvalue. Without it, you'll be performing a table scan; using key column #1, you might end up with the logical equivalent ( WHERE customfieldvalue.keycol1 IS NOT NULL ), but that will satisfy the safe update constraints.
SET SQL_SAFE_UPDATES=0;
update customfieldvalue
set stringvalue="****"
where customfieldvalue.customfield IN
(select customfield.id
from customfield
where customfield.cfname="Resoltion"
and customfieldvalue.issue=12345);