MySQL Update table with Select/Join - mysql

Please note: I am using MySQL Workbench for this so some errors may be related to that specifically (I'm not 100% sure I'm afraid)
Hi,
I am working on a project for my Uni where I accept a CSV file containing a list of guests and information such as the course area they are interested in. The CSV file will contain the actual name of the Area of Interest as this comes from a registration web page (for Open Days).
I am trying to take the Area of Interest in my Guests (joinTest) table, do a join to the Interests table (that contains the ID numbers for them as well as the Interest Name) and populate the InterestID column in the Guests table.
I am using a test table for now until I've got the SQL right. This is the SQL I am currently using:
UPDATE joinTest
SET interestID =
(
SELECT interest.interestID FROM joinTest
LEFT JOIN interest on joinTest.interestName = interest.interestName
)
WHERE interestID IS NULL;
I've tried that and get the error
Error Code: 1093. You can't specify target table 'joinTest' for update in FROM clause
So I'm assuming I can't use a SELECT clause while join a join in an UPDATE.
I then tried this:
UPDATE joinTest
INNER JOIN interest
ON jointest.interestname = interest.interestName
SET joinTest.interestID = interest.interestID;
I get this error:
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 Queries and reconnect.
I'm completely stumped now. I'm not sure where to go from here.
If any more information is required, please let me know.

SQL_SAFE_UPDATES is disabled by default but Workbench enables it. Like the error message says: toggle the option in Preferences -> SQL Queries and reconnect.
This error shouldn't exist unless you specifically turn on SQL_SAFE_UPDATES in your web app (php).

It's MySQL Workbench specific thing. You can disable this behavior in settings as your error message advices you: ... toggle the option in Preferences -> SQL Queries and reconnect.
PS You can safely run this query in your code without SET SQL_SAFE_UPDATES = 0;.

Related

Update\Insert data from grafana to mysql

Is is possible to update data or insert data from grafana to mysql. I have a requirement to insert/update information in mysql using a UI. Now I am already using grafana so wanted to know if there is any way we can use grafana to update or insert information
There is no input panel for taking user input and inserting that data to MySQL or any other. (Grafana v7.0)
For very minimalist input data you can use grafana variables as a hack.
Create example table for storing input in MySQL
CREATE TABLE `grafana_variable` (`variable` VARCHAR(64)) ENGINE = InnoDb;
In Grafana dashboard click settings icon:
then click:
add variable:
return to dashboard and you should see at the top:
create a new visualization panel or add another query in any already existing panel and add SQL query:
INSERT INTO `grafana_variable` VALUES ('${myvar}');
now every time you type into that field
and click away from it or use ie tab key the data will be inserted to the MySQL database.
mysql> select * from grafana_variable;
+---------------------+
| variable |
+---------------------+
| this is just a test |
+---------------------+
1 row in set (0.08 sec)
The downside of this hack is that the visualization tile will have in its upper left corner error icon:
with information (when you hover over it):
Found no column named time or time_sec
because the sql query of the visualization tile is inserting data instead of selecting it from database therefore there is no time or time_sec data.
Grafana also does not handle multiple MySQL statements so you cannot repair that by adding before or after the INSERT INTO ... statement second one with SELECT FROM. Maybe this could be somehow patched by using sub queries or something similar but I did not investigate that further.
Because the error as above can be due to multiple reasons not related to the hack it will be better to use the hack query in separate visualization tile - the one that you will remember (or even give descriptive panel tile) so you know that the error there is nothing unusual.
See grafana varialbes for more info about variables.
There is also useful list of already existing variables you can use same way as myvar I created. See section MACROS here For eg:
$__timeFrom()
and
$__timeTo()
have the start and the end of displayed time range.
Permissions and Security
The whole hack works because the MySQL user that grafana uses is allowed to execute INSERT statement, but if grafana is able to execute ANY statement then make sure that MySQL user that grafana uses is not allowed for example to execute statements like DROP ... or any other that is not related to INSERT data into the grafana_variable table as in example.
If you use MySQL as datasource for displaying data the grafana user should also be able to execute SELECT statements.
But nothing else than that.
Read about MySQL user premissions
Must hide Update as temporary table
I am working with postgres and Grafana. Probably it is quite similar.
With postgres you have to hide the update like this:
WITH UPDAT_TEMP AS (UPDATE MyTable SET MY_ROW='MY_TEXT' WHERE ID=99)
SELECT 1;
If you want to update various rows, you have to put various temporary tables.
The
Update my_table (rows) values(...)
does not work for me under Grafana.
Therefore:
WITH UPDAT_TEMP AS (UPDATE MyTable SET MY_ROW='MY_TEXT' WHERE ID=99),
WITH UPDAT_TEMP2 AS (UPDATE MyTable SET MY_ROW2='MY_TEXT2' WHERE ID=99)
SELECT 1;

sequelize you are using sql safe update error

I am getting this error randomly . I am deleting from activeusers table on the basis of username it gives me error sometime and works smoothly sometime.
here is my code for deleting
let say
data={nickname:'asad',id:1}
activeusers.destroy({where:{username:data.nickname} }).then(res=>{
});
I have turn off the sql safe update from sql workbench but problem still exists
how can i permanently get rid from this error
This is a common problem with updates in MySQL. Here is your query:
DELETE FROM ActiveUsers WHERE username = 'mazhar.hayat#ibexglobal.com'
The error stems from that you are not using a primary key column in the WHERE clause. You would also see this error if you had no WHERE clause at all. MySQL has a mode which views a DML query like this as unsafe, because it is broad and runs the risk of corrupting your data.
There is a hack solution to this which might work. You could modify the query to mention the primary key column as follows:
DELETE
FROM ActiveUser
WHERE username = 'mazhar.hayat#ibexglobal.com' AND id=id
This might spoof MySQL into thinking the query is safe, because it mentions the primary key column id in the WHERE clause.
But what I would recommend to you is turning off safe updates mode directly in MySQL. Edit your startup script and make sure that --safe-updates and --i-am-a-dummy are not mentioned in the script.
Edit:
If you wanted to handle this from destroy, then the query option is the only option:
Post.findAll(
{ where: ["username = ? AND id = id", data.nickname] }
).success()

MySQL error code: 1175 during UPDATE (MySQL-Workbench vs. console)

Am very aware of that this issue can be resolved with disabling safe update mode enabled (e.g. see here: MySQL error code: 1175 during UPDATE in MySQL Workbench). However, I do not wish to disable safe update mode (and there are many many solutions that propose this).
Similarly, I am aware that setting the WHERE clause to KEY-value that matches everything is supposed to work. However, doesn't appear to work on mysql-workbench - at least not the way I hoped (or the way it did work on the console).
For example, the following didn't work on mysql-workbench (but did on the console):
UPDATE FUEL_SOURCES AS FS
INNER JOIN
FUEL_CATEGORY FC ON FC.FUEL_CATEGORY = FS.FUEL_CATEGORY
SET
FS.FUEL_CATEGORY_ID = FC.ID
WHERE
FC.ID <> 0 AND FS.ID <> 0
...If I explicitly / exactly set the ID's (e.g. WHERE FC.ID = 20 AND FS.ID <> 10 for example) it would work in mysql-workbench. But doing this would involve iterating through every key-pair combination.
Be intereted to know what is causing this behaviour, or if I am doing something horribly wrong. Using mysql-workbench 6.3
From https://dev.mysql.com/doc/workbench/en/workbench-faq.html#faq-workbench-delete-safe
By default, Workbench is configured to not execute DELETE or UPDATE
queries that do not include a WHERE clause on a KEY column.
Such configuration prevents you from deleting or updating table mistakenly, since you are doing a batch update on data without a key.
To resolve this, as you may be already aware the following options.
Open your Workbench Preferences, select the SQL Editor section, and disable the following preference: "Safe Updates" - Forbid UPDATEs and DELETEs with no key in WHERE clause or no LIMIT clause.
Run SET SQL_SAFE_UPDATES=0;
If you are using workbech, you can first execute
SET SQL_SAFE_UPDATES = 0;
And then execute delete statement
If you want to still update your data with safe update on, you must retool your where clause so that it includes references to the table's primary key(s). See this page.

WordPress database error The table is full for query UPDATE

In my wordpress multisite, in the main website, I got error when I tried to update any post or widget. When I put my site in debug mode, I found many error with: The table is full for query UPDATE or INSERT as:
WordPress database error The table 'wp_1_options' is full for query UPDATE
WordPress database error The table 'wp_1_comments' is full for query INSERT
etc
And I think each table of my db seem limit contain only 3MB if I checked to the content of those error tables.
I use a database plugin for my wordpress site to check the db, I found most of them got "Overhead" size of 3MB.
What is the exactly issue with above info? How we could solve it?
What does Overhead mean? How can we solve that overhead issue?
The issue solved the same way as stated in the question: ERROR 1114 (HY000): The table is full
I asked my host to change:
innodb_data_file_path = ibdata1:10M:autoextend:max:512M
It solved my problem already.
So What your asking about is actually not possible in MySQL as I recently discovered. A cursor is declared at the top of a stored procedure with the rest of your declarations and MySQL's parse engine cannot manage conditional variable declaration, its all or none.
I came up with the following hack that let me get around this which is that I just declared every cursor I needed separately. I only had two, so this was not crazy:
DECLARE curs CURSOR FOR SELECT DISTINCT(user_id), applied_at FROM application WHERE job_id = JOB_ID_INPUT ORDER BY applied_at DESC;
DECLARE curs_all CURSOR FOR SELECT user_id, applied_at, job_id FROM application WHERE 1 ORDER BY applied_at DESC;
I would suggest that if you plan to have hundreds of conditionally created cursors then you may want to find another way to achieve your task in MySQL, or find a different tool if you cannot. Good luck.
Check this out for more detail:
http://dev.mysql.com/doc/refman/5.0/en/cursors.html

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);