-4
I have the following table (Example), where the ID user 151 has 2 records:
id (Primary Key) | user name |
---|---|
1 | 151 |
2 | 123 |
3 | 985 |
4 | 151 |
5 | 652 |
I need to delete this user’s 2 records. The problem is that as the column user name does not have Primary Key, Mysql does not allow because of safe update. I needed to get around this without having to touch the settings.
I tried in the following ways, but without success, the error persists.
DELETE FROM tabela WHERE id in (SELECT id FROM tabela WHERE coluna1 = 151);
DELETE FROM tabela WHERE id = (SELECT id FROM tabela WHERE coluna1 = 151);
I don’t understand your question !
– novic
Also this confused...
DELETE FROM tabela WHERE usuario_id = '151'
wouldn’t solve the problem? Will delete all records whereusuario_id
be 151.– Junior Like
That’s what I said, Mysql does not allow, because the user column has no Primary Key
– Henry Victor
It has nothing to do with
Primary Key
, what’s the mistake?– novic
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.
– Henry Victor
Execute
SET SQL_SAFE_UPDATES = 0;
. After executing theDELETE
.– Clarck Maciel
DELETE FROM tabela WHERE id = 151
doesn’t solve?– novic
if the same table is only complicating, can do as commented here,
DELETE FROM tabela WHERE usuario_id= 151
, if there are two tables, your example is bad, because in the from both are called "table" which leads to error– Ricardo Pontual
Clarck’s answer worked, the rest couldn’t understand the question, but thank you anyway.
– Henry Victor
SET SQL_SAFE_UPDATES = 0;
is recommended only to run in a tool, in Workbench for example, not to put in the code, if you are going to use this delete in the code you will need to change that– Ricardo Pontual
@Ricardopunctual yes, I got that. I opened another question focusing more on the subject, if I can help, thank you: https://answall.com/questions/514150/deletar-registro-sem-primary-key-no-mysql-via-ajax
– Henry Victor
It’s not because his answer decided that this correct, it worked by coincidence, turn off the SAFE_UPDATES nor should it be an answer, it became more like "technical support" and also has no sense to run a subquery without need, where the columns are specific to the same records, there is simply something wrong, it is much more likely that the problem is only occurring because there is data in other tables related to foreign keys, i.e., to delete a record in table A you have to delete the "linked".
– Guilherme Nascimento
... but regardless of this the question does not present clear details for someone to be able to solve objectively and for the question to serve future users. We have years of experience here and so was created the guide, to help act on the site in a way that is beneficial to yourself, as well as to future visitors, making the content clear and profitable.
– Guilherme Nascimento