What is the difference between DROP and DELETE for tables? - mysql

Which one will delete all data from the table, and which one will remove the table from the database?
What is the right thing to do if I don't want this table in my database any more?
Should I drop or delete?

DROP command deleting the table and its structure from the data base.
DROP TABLE tbl_user;
DELETE command used for deleting the records from the table,and it removing the table space which is allocated by the data base, and returns number of rows deleted.
DELETE FROM tbl_user WHERE id = 1;
TRUNCATE command is also delete the records but it doesn't delete the table space which is created by the data base, and does not return number of deleted rows.
TRUNCATE TABLE tbl_user;

DROP is used to remove tables (and databases).
DELETE is used to delete rows from tables.

Maybe are you talking about TRUNCATE and DELETE ?
TRUNCATE TABLE users;
is equivalent (logically) to
DELETE FROM users;
This will erase all data in table users. If you want to delete the whole table structure you should write:
DROP TABLE users;
But, DELETE is DML command while TRUNCATE and DROP are DDL commands. There is also some other differences in different RDBMS. More info - here
And another useful link: Difference between TRUNCATE, DELETE and DROP commands

drop removes the contents and the table (but not user permissions on the table). This is what you want if you want to completely remove the table from your schema.
delete selectively (or not) removes rows from a table. It does not alter the table structure.

There's no such thing as DELETE TABLE. You should use DROP TABLE to delete your table.

The DROP TABLE statement is used to delete a table.
DROP TABLE table_name
The DELETE statement is used to delete data(records) in a table.
DELETE FROM table_name WHERE some_column=some_value;
So, as you want to delete table from database then you will go for DROP Command

DELETE is used to delete one or several rows from the table. DROP TABLE would remove the entire table from the database, so if you want to remove the table, you should use DROP TABLE.
DROP TABLE reference

Delete will update the log, while drop does not.
Delete is used to remove the rows, while drop is used to remove the tables and DB.

delete
- removes the rows from the table - where clause can be used to delete specific rows.
- If where is not specified, then all the rows in the table will be removed
- rollback can be done.
without using where condition
DELETE * FROM empl;
using where condition
DELETE FROM empl WHERE job = 'Manager';
truncate
- removes all the rows from the table.
- rollback cannot be done.
SQL> TRUNCATE TABLE empl;
drop
- removes a table from the database.
- rows, indexes and privileges will also be removed.
- rollback cannot be done.
SQL> DROP TABLE empl;

Delete remove all data from a specific table and drop remove whole database and also remove specific table from database.
If you don't want the table in database any more then you should drop the table.

Related

Delete from a specific row to the last one

I have a table (cars) that has 26500 rows. Is it possible to delete from the row number 10001 through the end?
in InnoDB Tables
If you are deleting many rows from a large table, you may exceed the lock table size for an InnoDB table. To avoid this problem, or simply to minimize the time that the table remains locked, the following strategy (which does not use DELETE at all) might be helpful:
Step 1: Select the rows not to be deleted into an empty table that has the same structure as the original table:
INSERT INTO `cars_copy` SELECT * FROM `cars` LIMIT 10000 ;
Step 2: Use RENAME TABLE to atomically move the original table out of the way and rename the copy to the original name:
RENAME TABLE `cars` TO `cars_old`, `cars_copy` TO `cars` ;
Step 3: Drop the original table:
DROP TABLE `cars_old`;
No other sessions can access the tables involved while RENAME TABLE executes, so the rename operation is not subject to concurrency problems.
When your Rows are labelled with an ID you can just do this:
DELETE FROM cars WHERE ID > 10000

I am using this sql statement to delete the total record from a table

DELETE FROM Orders WHERE OrderID BETWEEN 1 AND 1000;
but if the table record is unknown to me than how can i delete all record from the table without deleting table.
TRUNCATE Orders;
This is all what you need to delete all the content of your selected table
Use "TRUNCATE" syntax.
It will empties a table completely.
Check this document:
http://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
DELETE
When we have to use DELETE Command then the data deleted can be retrieved when you ROLLBACK.
DELETE * FROM Orders;
TRUNCATE
It drops the table and re-create it. It is much faster than deleting
rows one by one. Once you use TRUNCATE command then you cannot retrieve
the data again from the table.
TRUNCATE Orders;
If you want to delete all the records, you can do this with:
TRUNCATE TABLE Orders
The table still exists but will be empty.
But if you want to delete a range of records you could probably do:
DELETE FROM Orders WHERE OrderID BETWEEN 1 AND (SELECT max(OrderID) FROM table)
In the last case all OrderID's must be unique.

how to delete duplicate records in mysql table

I'm having an issue with finding and deleting duplicate records, I have a table with IDs called CallDetailRecordID which I need to scan and delete records, the reason there are duplicates is that I'm exporting data to special arching engine works with MySQL and it doesn't support indexing.
I tried using "Select DISTINCT" but it dosn't work, is there is another way? I'm hoping I can create a store procedure and have it run weekly to perform clean up.
your help is highly appreciated.
Thank you
CREATE TABLE tmp_table LIKE table
INSERT INTO tmp_table (SELECT * FROM table GROUP BY CallDetailRecordID)
RENAME table TO old_table
RENAME tmp_table to table
Drop the old table if you want, add a LOCK TABLES statement at the beginning to avoid lost inserts.

Delete Contents Of A table

How Can I delete Contents of A table.. If I have 100 rows in a table and i want to delete all of them without droping a table how would I?
Use DELETE without a WHERE clause:
DELETE FROM tablename
This may fail if you have other tables that refer to it via foreign key references.
Both given answers are correct.
The main difference between
TRUNCATE TABLE tablename
and
DELETE FROM tablename
is that TRUNCATE will reset the auto_increment value back to 1. It may make a difference if things have to be unique.
You can also use TRUNCATE
http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
http://www.postgresql.org/docs/current/static/sql-truncate.html (Thanks for PostgreSQL link to rfusca)
TRUNCATE TABLE tablename
You can use Truncate also,it's faster than DELETE but TRUNCATE will not work under a table lock or transaction.
have a look at
http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
http://dev.mysql.com/doc/refman/5.0/en/delete.html

Delete, Truncate or Drop to clean out a table in MySQL

I am attempting to clean out a table but not get rid of the actual structure of the table. I have an id column that is auto-incrementing; I don't need to keep the ID number, but I do need it to keep its auto-incrementing characteristic. I've found delete and truncate but I'm worried one of these will completely drop the entire table rendering future insert commands useless.
How do I remove all of the records from the table so that I can insert new data?
drop table will remove the entire table with data
delete * from table will remove the data, leaving the autoincrement values alone. it also takes a while if there's a lot of data in the table.
truncate table will remove the data, reset the autoincrement values (but leave them as autoincrement columns, so it'll just start at 1 and go up from there again), and is very quick.
TRUNCATE will reset your auto-increment seed (on InnoDB tables, at least), although you could note its value before truncating and re-set accordingly afterwards using alter table:
ALTER TABLE t2 AUTO_INCREMENT = value
Drop will do just that....drop the table in question, unless the table is a parent to another table.
Delete will remove all the data that meets the condition; if no condition is specified, it'll remove all the data in the table.
Truncate is similar to delete; however, it resets the auto_increment counter back to 1 (or the initial starting value). However, it's better to use truncate over delete because delete removes the data by each row, thus having a performance hit than truncate. However, truncate will not work on InnoDB tables where referential integrity is enforced unless it is turned off before the truncate command is issued.
So, relax; unless you issue a drop command on the table, it won't be dropped.
Truncate table is what you are looking for
http://www.1keydata.com/sql/sqltruncate.html
Another possibility involves creating an empty copy of the table, setting the AUTO_INCREMENT (with some eventual leeway for insertions during the non-atomic operation) and then rotating both :
CREATE TABLE t2_new LIKE t2;
SELECT #newautoinc:=auto_increment /*+[leeway]*/
FROM information_schema.tables
WHERE table_name='t2';
SET #query = CONCAT("ALTER TABLE t2_new AUTO_INCREMENT = ", #newautoinc);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
RENAME TABLE t2 TO t2_old, t2_new TO t2;
And then, you have the extra advantage of being still able to change your mind before removing the old table.
If you reconsider your decision, you can still bring back old records from the table before the operation:
INSERT /*IGNORE*/ INTO t2 SELECT * FROM t2_old /*WHERE [condition]*/;
When you're good you can drop the old table:
DROP TABLE t2_old;
I've just come across a situation where DELETE is drastically affecting SELECT performance compared to TRUNCATE on a full-text InnoDB query.
If I DELETE all rows and then repopulate the table (1million rows), a typical query takes 1s to come back.
If instead I TRUNCATE the table, and repopulate it in exactly the same way, a typical query takes 0.05s to come back.
YMMV, but for whatever reason for me on MariaDB 10.3.15-MariaDB-log DELETE seems to be ruining my index.