last day (16 days later) » 

8:49 PM
0
Q: How to prevent unexpected results in SQL query when using delete.?

PoojanI recently had a situation where I deleted all the rows of a table while someone else was executing a query using that table. I didn't understand why MySql allows this. Or how to prevent it. I was using Dbeaver's client for executing the query. delete from table This is the query I ran and it...

 
@RaymondNijland Then there might be some problem from my side. But i am sure that i checked session manager after that delete statement and there was one on going query. Also a co worker reported all of his join failed because the data was suddenly unavialable in between query. Is this possible situation in mysql? Or there can be some other issue that caused this problem. Thanks
 
Don't worry about locking, MySQL should handle it for you.. if somebody does a SELECT * FROM t and somebody does DELETE FROM t while that SELECT is still running .. That DELETE has to wait until MySQL has run that SELECT * FROM t .
.. but if somebody does a SELECT * FROM t WHERE id BETWEEN 1 AND 10000 and somebody does DELETE FROM t WHERE id > 10001 while that SELECT is still running .. MySQL can run that DELETE together with that SELECT assuming InnoDB engine offcource..
 
@RaymondNijland Ok i will try to reproduce problem i have and open question again. For now i am closing this question because problem is not clearly defined.
 
"For now i am closing this question because problem is not clearly defined." Or you trying to prevent running DELETE FROM t completly?
 
@RaymondNijland No nothing like that. I just got told from a co-worker that his join query failed because data suddenly disappeared from the table. So i thought my delete statement caused this problem.
 
8:49 PM
O i think what you might mean transaction isolation levels..
 
yes i am looking for something like that. but dont understand what that is. If you can write a answer explaining what that is it will be really helpful. Thanks
 
wiki already has one..
In database systems, isolation determines how transaction integrity is visible to other users and systems. For example, when a user is creating a Purchase Order and has created the header, but not the Purchase Order lines, is the header available for other systems/users (carrying out concurrent operations, such as a report on Purchase Orders) to see? (Refers to current, not past database systems) A lower isolation level increases the ability of many users to access the same data at the same time, but increases the number of concurrency effects (such as dirty reads or lost updates) users mig...
i think what happend where Phantom reads your co-worker should have used SERIALIZABLE transaction
Meaning running SET TRANSACTION ISOLATION LEVEL SERIALIZABLE before the query
 
yeah probably. But later i had talk with him and he mensioned something like 3 phase commit. for which i also didnt find any good resources. Is it something specific to DBMS or some general CS concept. Thank you for you help till now.
 
transaction isolation is defined in the ANSI standard for SQL language, so yes this is cross RDMS
there might be implementation differences between RDMS, still the MySQL manual -> dev.mysql.com/doc/refman/8.0/en/sql-syntax-transactions.html should also talk about those isolation levels...
The default isolation level is REPEATABLE READ. Other permitted values are READ COMMITTED, READ UNCOMMITTED, and SERIALIZABLE. For information about these isolation levels, see Section 15.7.2.1, “Transaction Isolation Levels”.
 
I will read the transaction isolation levels to understand what cause the problem and update my question accordingly. Thank you.
 
9:02 PM
Well the transaction level SERIALIZABLE would have locked all records which where expected to be needed for the join and should not be allowed to be deleted by a other transaction..
 
Yeah as again since the problem is not reproducible for now the question is incomplete. I will do some research.
 
the other thing could also happen, his join could also return more then it should when you add or update records to match his join with the wrong isolation level..
 

  last day (16 days later) »