Use is very simple and imprtant command for working in database at manual time. It used to select database in sql.
mysql> SELECT * FROM personal; +------+--------+ | id | fname | +------+--------+ | 1 | Ram | | 2 | Sham | | 3 | Kajal | | 4 | Pooja | | 5 | Shruti | +------+--------+ 5 rows in set (0.00 sec) mysql> DELETE FROM personal; Query OK, 5 rows affected (0.02 sec) mysql> SELECT * FROM personal; Empty set (0.00 sec) mysql>
If you want to delete specifice row data you need to use WHERE clause that help to conition with Operator with delete data. WHERE is optional part.
mysql> SELECT * FROM personal; +------+--------+ | id | fname | +------+--------+ | 1 | Ram | | 2 | Sham | | 3 | Kajal | | 4 | Pooja | | 5 | Shruti | +------+--------+ 5 rows in set (0.00 sec) mysql> DELETE FROM personal WHERE id = 5; Query OK, 1 rows affected (0.02 sec) mysql> SELECT * FROM personal; +------+--------+ | id | fname | +------+--------+ | 1 | Ram | | 2 | Sham | | 3 | Kajal | | 4 | Pooja | +------+--------+ 4 rows in set (0.00 sec) mysql>
Truncate is DDL command. When you truncate a table SQL server doesn't delete the data, it drops the table and recreates it, there by deallocating the pages so there is a chance to recover the truncated data before the pages where overwritten.
mysql> SELECT * FROM personal; +------+--------+ | id | fname | +------+--------+ | 1 | Ram | | 2 | Sham | | 3 | Kajal | | 4 | Pooja | | 5 | Shruti | +------+--------+ 5 rows in set (0.00 sec) mysql> TRUNCATE personal; Query OK, 5 rows affected (0.02 sec) mysql> SELECT * FROM personal; Empty set (0.00 sec) mysql>
If AUTOCOMMIT set to false and the transaction not committed, the changes will be visible only for the current connection. After COMMIT statement commits the changes to the table, the result will be visible for all connections.
If anything went wrong in your query execution, ROLLBACK in used to revert the changes.
mysql> COMMIT; Query OK, 0 rows affected (0.00 sec) mysql> TRUNCATE mytable; Query OK, 0 rows affected (0.02 sec) mysql> ROLLBACK; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM mytable; +------+ | id | +------+ | 2 | | 3 | +------+ 2 rows in set (0.00 sec) mysql>