DELETE using TRUNCATE command and Delete VS. Truncate - mysql

Delete a single row of a table in MYSQL using truncate command difference between truncate and delete we can delete a single row but how to delete a single row using truncate?

You can't truncate a single row. Read up on the mysql reference:
TRUNCATE [TABLE] tbl_name TRUNCATE TABLE empties a table completely.
It requires the DROP privilege as of MySQL 5.1.16. (Before 5.1.16, it
requires the DELETE privilege).
Logically, TRUNCATE TABLE is equivalent to a DELETE statement that
deletes all rows, but there are practical differences under some
circumstances.
Reference:
13.1.34 TRUNCATE TABLE Syntax

Just for your knowledge DELETE is a DML Command. and TRUNCATE is a DDL command

No truncate can't be used for one single row. It has to be used for the entire table.
proper working/practical differences are :
With Delete command structure of the table is not purged from database.
you can delete all the data of the database but not the format of the table. that remains stable.
Delete is a DML command.
DELETE handles rows chosen with a WHERE statement. Its use is part of the discipline of running production applications. For example, you might DELETE all rows that have been marked "complete" more than a month ago.
Truncate removes/purge the structure of the table itself. It also purges sets the auto_increment to its initial value (that is zero by default).
Truncate is a DDL command
TRUNCATE rapidly removes all rows from a table while maintaining the table definition. (Row by row DELETE can take time, especially for tables with lots of keys.)
It comes in handy for such things as log tables that start out empty for each week's production.
It has the convenient side effect of resetting the indexes and releasing unused disk storage. I have used TRUNCATE to wipe out the contents of a log table that used to contain millions of rows, to switch to an operational discipline where it only contains a weeks' worth of rows, for example.

TRUNCATE removes all the rows from the Table.
DELETE statement deletes table rows and returns number of rows deleted.we can delete selected no of records from table using DELETE command.
TRUNCATE drops the table and re-create it. It is much faster than deleting rows one by one.
Once u use TRUNCATE command then u cannot retrieve the data again from the table.TRUNCATE removes all the rows from the Table.
Drop - deletes the data as well as structure.
The difference between DROP and DELETE table is that, after executing DELETE statement the contents of table are removed but the structure remains same, but in case of DROP statement both the contents and structure are removed.

When you want to delete a single row you use Delete command. When you want to clear the entire table data you can use Truncate Table command.

Related

Truncate questions in SQL

How can I remove a row by using Truncate in SQL instead of delete by using WHERE condition?
Truncate only use to remove the table or row? if can remove the row by truncate, let me know anyone
You can't. Only DELETE statements can have a WHERE condition on them, TRUNCATE removes all rows.
From MSDN :
Removes all rows from a table or specified partitions of a table,
without logging the individual row deletions. TRUNCATE TABLE is
similar to the DELETE statement with no WHERE clause; however,
TRUNCATE TABLE is faster and uses fewer system and transaction log
resources.
You can only remove a single row with truncate if that row is the only one in the table.
Truncate is not like delete.
You can't use truncate to delete specific rows.
In fact, the statement is truncate table - you can't truncate anything other then a full table.
Truncate will remove all rows from the table, and is only allowed if the table is not referenced by foreign keys, is not used as the basis of an indexed view, and is not published by transactional replication or merge replication.
Also, truncate table can't be executed inside a transaction.
Truncate table will also reset the identity column of the table (if one exists).

MySQL: Does `DROP TABLE` completely remove the table or just the structure?

I am under the impression that the MySQL command DROP TABLE User will just remove the data, columns, and relevant constraints. However, the table shell still exists. Is this correct?
Using DROP TABLE will remove the entire table and all records contained inside it. If you want to retain the table structure but remove all data, then consider using TRUNCATE TABLE. Truncating a table is implemented by dropping the entire table and then recreating it. This is faster than doing DELETE FROM yourTable, which removes records one-by-one.
After Drop Table, the table will not exist anymore, so no data and no table definition(which you called 'table shell'); TRUNCATE TABLE keep your table definition and delete all the data ,and reset table auto-increment as well, but be careful about TRUNCATE if the table size is huge, it will expand your tablespace and not easy to shrink.
As mysql manual on drop table says:
Be careful with this statement! It removes the table definition and all table data.
So, no shell (whatever that should mean) remains after dropping a table.
What do you mean by table shell?
From MqSQL dev site:
DROP TABLE removes one or more tables. You must have the DROP privilege for each table.
Be careful with this statement! It removes the table definition and all table data. For a partitioned table, it permanently removes the table definition, all its partitions, and all data stored in those partitions. It also removes partition definitions associated with the dropped table.

Insert data to table with existing data, then delete data that was not touched during session

I am making bunch of INSERT ON DUPLICATE KEY UPDATE to a table filled with data.
I need to fill table with that data AND remove data that I haven't filled (I mean remove rows that was not mentioned in my INSERTs).
What I tried and what was working:
create new timestamp column in table
During INSERTs insert or update this column with CURRENT_TIMESTAMP, so that all rows I touched have newest timestamps
Run delete query that deletes all rows that are older than the starting time of my script.
This idea works perfectly, but there is one problem: my replication binary log get filled with unnececary data on both modes (ROW and STATEMENT). I don't need that timestamps at all to be replicated...
I don't want to do TRUNCATE TABLE before inserts because my app should deliever a non-stop access to data (old or new). If I do TRUNCATE TABLE tables can be without data for some time.
I can also save all primary key values that I insert in scripts memory or temporary table, and then delete the rows that are not in that table, but I hope there is a more optimized and clever way to do that.
Do you have any idea how can I achieve that goal so I can update data, delete only untouched rows and replicate only changes (I guess in ROW mode)?
I'm not very familiar with replication binary logs, sorry in advance if won't work. I assumed that logging can be set differently for tables.
I would do the following:
create a table for the new data with the same primary key column with
the old table
delete all rows from old table where not found in the new table
update rows in the old table according to the new table
This way wouldn't be unnecessary log inserts.
This assumes that you have the required space in the server, but can work.

Resetting data in MySql database

I am faced with a scenario where I would like to remove all existing data from one of my tables and then reuse the table again and fill it with new data.
My Question: Should I destroy/recreate the exact same table to do this? Or is there an easier way? -Side Note: I am storing user id's and would like for the id's to be set back again as opposed to continuing at the number in which last data was stored.
I'm using PhpMyAdmin.
If you use TRUNCATE TABLE.
TRUNCATE TABLE empties a table completely. Logically, this is equivalent to a DELETE statement that deletes all rows, but there are practical differences under some circumstances.
...
it resets any AUTO_INCREMENT counter to zero
So if you want to keep the counter use DELETE FROM
If you delete all rows in the table with DELETE FROM tbl_name (without a WHERE clause) in autocommit mode, the sequence starts over for all storage engines except InnoDB and MyISAM.
You can also look here: How to prevent mySQL from resetting auto increment value?
You have to truncate your table, see documentation here: https://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
In phpmyadmin there's a button in the table list called: Empty

How to automatically run a query when a table is amended by INSERT/UPDATE/DELETE?

I have a query which basically "syncs" all the data from a table in one database, to a replicated table in another database.
Here is the simple query:
TRUNCATE TABLE [Database2].[dbo].[USER_SYNC]
INSERT INTO [Database2].[dbo].[USER_SYNC]
SELECT * FROM [Database1].[dbo].[USER]
Now, after some research, I had a look into using a trigger to do this, however, I read up that stored procedures and heavy queries such as this should not be used within a trigger.
Therefore, what is the best way in which I can automatically run this query from within SQL, whenever a record in database1 is inserted, amended or deleted?
And if what I read up about triggers was incorrect, then how would I go about creating one for my procedure? Thanks.
If you need to sync tables you do not need to truncate one every time on update, delete or insert.
Create identical copy of user table.
Create on update, on delete, on insert triggers on the original user table.
In the trigger update, delete or insert to the duplicate table only one row at a time - the one that was updated, deleted or inserted to the original user table. This will not be a heavy query.
UPDATE:
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html