I accidentally inserted the same value in the same field for many rows as illustrated in the image below:
I updated my table and the same file name was uploaded into every row in the table.
What SQL command can I use to revert this unintended modification?
Generally what I like to do when doing a delete statement that isn't by primary key is to select the result first..
SELECT * FROM table WHERE name='Sprouts';
If that result set is correct, then you can feel fairly safe swapping in the delete
DELETE FROM table WHERE name='Sprouts';
Related
I'm executing SQL against a MySQL database and trying to write commands to delete all rows in a table. When I run "DELETE FROM MyTable" I get the error 1175 because a safety feature is turned on by default that forces you to specify a WHERE clause on the primary key.
I could turn this feature off, but I kind of like it, so I'm trying to modify my command to have the same effect by adding the WHERE clause. So, this works:
DELETE FROM MyTable WHERE MyID > 0
In deleting all rows greater where MyID is greater than zero, but I'm looking for a truly universal one that would work for any MyID values. I tried these
DELETE FROM MyTable WHERE MyID = ANY
DELETE FROM MyTable WHERE MyID = *
But they are syntax errors. The doc on the WHERE clause says it just accepts =,<,>,>=,<=,<>,BETWEEN,LIKE,and IN, but nothing that looks like it accepts anything.
Is there a proper syntax for the WHERE conditional that accepts any value?
thank you.
Use
TRUNCATE MyTable;
then you don't need any condition, as you should have DELETE privileges
TRUNCATE TABLE empties a table. It requires the DROP privilege. Logically, TRUNCATE TABLE is similar to a DELETE statement that deletes all the rows, or a sequence of DROP TABLE and CREATE TABLE statements.
see manual
The error is related to the sql_safe_updates client option (see https://dev.mysql.com/doc/refman/8.0/en/mysql-tips.html#safe-updates).
One way to circumvent the safe updates mode is to use a LIMIT clause on your query. You can use any value, even a value that is far larger than the number of rows in your table.
DELETE FROM MyTable LIMIT 9223372036854775807;
You can also disable the safe updates mode, then run the DELETE normally:
SET sql_safe_updates=0;
DELETE FROM MyTable; -- returns no error
TRUNCATE TABLE is a better solution, if you want to delete all the rows.
Make it a compound condition.
WHERE id IS NULL OR id IS NOT NULL;
Then again, maybe the safety condition is there for an important reason that you should not get around.
I have database like the following with 10K rows. How to delete duplicate if all fields are same. I don't want to search for any specific company. Is there a way to search and find any multiple entries with all same fields get deleted. Thanks
This command adds a unique key, and drops all rows that generate errors (due to the unique key). This removes duplicates.
ALTER IGNORE TABLE table ADD UNIQUE KEY idx1(title);
Note: This command may not work for InnoDB tables for some versions of MySQL. See this post for a workaround. (Thanks to "an anonymous user" for this information.)
OR
Simply creates a new table without duplicates. Sometimes this is actually faster and easier than trying to delete all the offending rows. Just create a new table, insert the unique rows (I used min(id) for the id of the resulting row), rename the two tables, and (once you are satisfied that everything worked correctly) drop the original table
This below query used to find the duplicate entry using all fields:
Select * from Table group by company_name,city,state,country having count(*)>1;
When I imported a table with 30 million rows from a text file to a MYSQL table it only took 1 minute. However, I realized that I missed a column and that I needed to add it to the table. From the MYSQL command line, I wrote the following command:
create tableC as(tableA.T1, tableB.ZID from tableA, table B where A.ZID = B.ZID)
It's been over one hour and the command has not terminated. Does anyone know the reason why? TableB was already in the MYSQL server.
Not 100% sure but you might be better off altering the table first and adding the column, then doing an update to populate that column
ALTER TABLE tableB ADD COLUMN colA yourColumnDefinition;
UPDATE tableB SET colA = <however you do it>;
I'm not totally sure which table you are adding the column to or how you are computing it. Based on the query you posted it looks like you are creating a mapping table to map two table's IDs to each other. If that is the case you would probably be better off putting a foreign key in one of the tables.
Again, this might not be exactly what you are looking for but if you just want to add a column to a table you already created this might be a better approach.
I have this query that works fine. Its deletes records that are old based on current time.
$cleanacc_1 = "DELETE FROM $acc_1
WHERE `Scheduled` < DATE_SUB(UTC_TIMESTAMP(), INTERVAL 30 SECOND)";
$result = mysql_query($cleanacc_1);
However, there are over 100 tables (accounts) that need deleting and I was wondering if I can combine them into one query. If possible how?
This implies you create a new table for every account. Why are you not creating a record for each account within a single table?
For example...
create table account (id int unsigned primary key auto_increment, other fields...);
If you alter your table structure you will be able to delete individual account records with a single query...
delete from account where condition=true;
Individual transaction records for each account are then stored in another table and contain the account id they relate to...
create table transaction (id, account_id, other transaction fields);
If you don't change the database design you'll need to write PHP code that loops through each table and runs your delete query. This is very inefficient and I urge you to redesign the table as suggested.
If you don't understand why my table redsign suggestion is a better approach, post more information about your database and I'll explain in more detail with a working example.
No way to do that, AFAIK; anyways, I don't think it would be a big problem to run 100 queries, assuming you are not running that for each request or so..
Are you expecting performance issues? If that's the case, I'd probably use a cron job to run that query every X minutes..
You could setup a view of the tables and do then run the delete sql against the view. That should delete the underlying table data as well. Your table schema and permissions could have an affect whether this will work or not. Check out this answer, it might help as well.
Does deleting row from view delete row from base table - MYsql?
Please consider the following example.
I have three tables in following structure.
Table names : t1,t2,t3
Fields : Id, name
Im going to perform delete query with one condition which recode id must less than 10.
DELETE FROM t1, t2,t3 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id<10 and t2.id<10 and t3.id<10.
The query has been successfully executed ( MySql ). I got the expected output.
So please try the same way with your condition.
Is there a way to remove all repeat rows from a MySQL database?
A couple of years ago, someone requested a way to delete duplicates. Subselects make it possible with a query like this in MySQL 4.1:
DELETE FROM some_table WHERE primaryKey NOT IN
(SELECT MIN(primaryKey) FROM some_table GROUP BY some_column)
Of course, you can use MAX(primaryKey) as well if you want to keep the newest record with the duplicate value instead of the oldest record with the duplicate value.
To understand how this works, look at the output of this query:
SELECT some_column, MIN(primaryKey) FROM some_table GROUP BY some_column
As you can see, this query returns the primary key for the first record containing each value of some_column. Logically, then, any key value NOT found in this result set must be a duplicate, and therefore it should be deleted.
These questions / answers might interest you :
How to delete duplicate records in mysql database?
How to delete Duplicates in MySQL table.
And idea that's often used when you are working with a big table is to :
Create a new table
Insert into that table the unique records (i.e. only one version of the duplicates in the original table, generally using a select distinct)
and use that new table in your application ; or drop the old table and rename the new one to the old name.
Good thing with this principle is you have the possibility to verify what's in the new table before dropping the old one -- always nice to check that sort of thing ^^