DELETE query results in 'Query Interrupted' MySQL Workbench? - mysql

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.

Related

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.

prevent delete * from table unless primary key specified

I want to prevent user from using deleting * from table unless primary key specified, one of our team member accendently used "delete * from table_name" i want to prevent such scenarios in future.
Would safe updates be viable for you? This is an option you can enable on the command line, in the option file or set a variable in SQL code that prevents updates and deletes without a where clause that includes the key columns defining the rows to change.
In MySQL Workbench there is a setting in Preferences -> SQL Editor -> Safe Updates (rejects UPDATEs and DELETEs with no restriction). I believe this is even on by default.

MySQL - Automatic ordering by ID not working

I'm using MySQL database to store student projects (every single project has its own unique ID, as the first screenshot shows).
The 'ID' column is set as auto_increment and PRIMARY KEY.
All projects must be ordered (only) by their ID ascending. But every time I insert a new project into my database and set lower value in the 'Year' field (lower value than I entered last time, when I was inserting my previous project), my projects become automatically ordered by this 'Year' field (as shown on the second screenshot).
Is there any way, how to set my table for automatic ordering all newly added projects only by the 'ID' column? Yeah, I know that I can change the ordering with ALTER TABLE tablename ORDER BY columnname ASC;after I place every new record, but can it be done automatically?
Thx to everyone who helps.
Q: Is there any way, how to set my table for automatic ordering all newly added projects only by the 'ID' column?
A: There is no "automatic ordering" in a MySQL table. (Some storage engines, such as InnoDB, are "index organized" structures, and do store rows in order by the cluster key.) But this organization does not define or specify the order of rows returned by a SELECT statement.
Without an ORDER BY clause on a SELECT statement, then the MySQL server can return rows in any order it chooses to. There is no guarantee of any "automatic" or "default" ordering.
When we run a SQL SELECT statement without an ORDER BY clause, we do observe that rows tend to be returned in a consistent order. This behavior isn't guaranteed, and it isn't "automatic".
This consistent "ordering" behavior we observe is due to the MySQL server performing a consistent set of operations, on a consistent set of data.
Performing an ALTER TABLE statement to rebuild the entire table is not the solution to the "automatic ordering" issue.
If you want the MySQL server to return rows in a specific order, then the solution is to add an ORDER BY clause to the SELECT statement.
The client that executes the SELECT statement is free to do whatever it wants with the rows it retrieves. The client can perform operations such as filtering, ordering, or whatever, to come up with what gets returned in the user interface.
Some clients (like the mysql command line) don't implement any functions for "filtering" or "ordering" rows, so the only way for the client to specify an order that rows should be returned in is the ORDER BY clause on statement itself. (The MySQL command line client returns rows in the user interface in the same order that they are retrieved.)
I expect that phpMyAdmin does the same thing, it displays the rows in the same order that they are returned from the MySQL server.
Order in query results should be determined by the ORDER clause. Don't rely on default order applied by phpmyadmin (or some other tool).
You have to distinct retrieving from inserting/updating.
About automatic order INTO phpmyadmin, maybe using bookmarks on queries : How can I set a default sort for tables in PHPMyAdmin (i.e. always "Primary key - Descending")

Is it possible to edit the query result in workbench?

I need to delete some records that comes out of the query result. But the query result is marked as Read Only. even when I query the primary key. The problem is my DB is so large (not much, 1.5 million record) that I can not right click on the table and choose Edit table data as I get the error no. 2008: mysql client ran out of memory which I could not solve (though, my pc has 8 GB RAM). My DB is loaded in the localhost so the client is the server. Please, help.
MySQL Workbench is by default set into secure mode (they call it "Safe updates" mode) meaning that one cannot update or delete rows within SQL editor until WHERE clause with (primary) key or LIMIT parameter is not explicitly defined:
So the query:
DELETE FROM table WHERE name='xyz'
will not work (will not really delete rows) until you define WHERE with PK:
DELETE FROM table WHERE id=100
or
DELETE FROM table WHERE name='xyz' LIMIT 1
To disable secure mode in Workbench go into Edit->Preferences->SQL Editor tab and uncheck "Safe updates" checkbox. But be careful then! :-)
To edit a big table:
You can right click the "Send to SQL Editor -> SELECT All Statement".
Then before running, this, add a LIMIT 100 in that SELECT or a WHERE condition to filter the rows returned.
You can also go to "Preferences -> SQL Editor" and mark the "Limit Rows" checkbox. If you set it to, say 1000, then the "Edit Table" command will fetch the first 1000 rows. You can then move to the next "page" by clicking on the "Fetch next frame of records from the data source" button.
Your other question:
It's not always possible to edit a result set. If it involves GROUP BY or data from more than one tables, then the result set may be Read-only. In that case, you could write a DELETE or UPDATE statement to do the deletion or updates for you.

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