Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all records created today?

I am dealing with a very big database ~ 6 Million records. I've added ~30,000 bad records today. How can I delete all of the records created today in MySQL?

like image 308
JZ. Avatar asked Mar 25 '12 22:03

JZ.


People also ask

How do I clear my entire database?

Expand Databases, right-click the database to delete, and then click Delete. Confirm the correct database is selected, and then click OK.

Which SQL command delete all records?

The DELETE statement is used to delete existing records in a table.

How do I delete multiple records in a database?

Another way to delete multiple rows is to use the IN operator. DELETE FROM table_name WHERE column_name IN (value 1, value 2, value 3, etc...); If you want to delete all records from the table then you can use this syntax.

How do you delete table records?

To delete an entire record/row from a table, enter delete from followed by the table name, followed by the where clause which contains the conditions to delete. If you leave off the where clause, all records will be deleted.


2 Answers

It seems created_at is a datetime. Try:

delete from table
where date(created_at) = curdate()

Of course, run a select * prior to run this query and make sure the data you're going to delete is the one you really want to delete.

like image 136
Mosty Mostacho Avatar answered Sep 30 '22 16:09

Mosty Mostacho


The condition

WHERE created_at >= '2012-03-25' 
  AND created_at < '2012-03-26'

could be used to identify the rows (and quite efficiently if there is an index on created_at).

Before deleting, make sure you backup the table (or even better, the whole database). Additionally, you can use some (temp or permament) table to have the rows stored, before deleting them from your table. Then, you delete this temp table when you are sure you have erased the offending data - and nothing else:

CREATE TABLE wrong_data AS
  SELECT *
  FROM tableX
  WHERE created_at >= '2012-03-25' 
    AND created_at < '2012-03-26' ;

DELETE t
FROM tableX AS t
  JOIN wrong_data AS w
    ON w.PK = t.PK ;
like image 25
ypercubeᵀᴹ Avatar answered Sep 30 '22 17:09

ypercubeᵀᴹ