0% found this document useful (0 votes)
2 views31 pages

SQL Delete Statement Notes

The document explains the SQL DELETE statement, which removes records from a database based on specified conditions. It also compares the DROP, TRUNCATE, and DELETE commands, highlighting that DROP removes the table entirely, TRUNCATE removes all records quickly and resets auto-increment values, while DELETE removes records row by row without resetting auto-increment values. Additionally, it discusses the implications of foreign key constraints with ON DELETE CASCADE.

Uploaded by

ganamb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

SQL Delete Statement Notes

The document explains the SQL DELETE statement, which removes records from a database based on specified conditions. It also compares the DROP, TRUNCATE, and DELETE commands, highlighting that DROP removes the table entirely, TRUNCATE removes all records quickly and resets auto-increment values, while DELETE removes records row by row without resetting auto-increment values. Additionally, it discusses the implications of foreign key constraints with ON DELETE CASCADE.

Uploaded by

ganamb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

THE SQL DELETE

STATEMENT
The DELETE Statement
the DELETE statement
removes records from a database

DELETE FROM table_name


WHERE conditions;
FOREIGN KEY Constraint
ON DELETE CASCADE
if a specific value from the parent table’s primary key has been deleted, all the records from the
child table referring to this value will be removed as well
DROP VS
TURNCATE VS
DELETE
DROP vs TRUNCATE vs DELETE
DROP

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
DROP

column_1
1
indexes
2
3
4
+ + constraints


10
DROP vs TRUNCATE vs DELETE
DROP

column_1
1
indexes
2
3
4
+ + constraints


10
DROP vs TRUNCATE vs DELETE
DROP
- you won’t be able to roll back to its initial state, or to the last COMMIT statement

use DROP TABLE only when you are sure you aren’t going to use the table in question anymore
DROP vs TRUNCATE vs DELETE
TRUNCATE

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE ~ DELETE without WHERE

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE ~ DELETE without WHERE

column_1
1
2
3
4
+

10
DROP vs TRUNCATE vs DELETE
TRUNCATE ~ DELETE without WHERE

column_1
1
2
3
4
+

10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1
1
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11 1
2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11 1
2 12
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 11 1
2 12 2
3 TRUNCATE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset

column_1 column_1
1 1
2 2
3 TRUNCATE 3
4 4

… …
10 10
DROP vs TRUNCATE vs DELETE
DELETE
removes records row by row

DELETE FROM table_name


WHERE conditions;

TRUNCATE ~ DELETE without WHERE


DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- the SQL optimizer will implement different programmatic approaches when we are using
TRUNCATE or DELETE

TRUNCATE delivers the output much quicker than DELETE


row by row row by row
DROP vs TRUNCATE vs DELETE

TRUNCATE vs DELETE without WHERE


- the SQL optimizer will implement different programmatic approaches when we are using
TRUNCATE or DELETE

TRUNCATE delivers the output much quicker than DELETE


row by row row by row
DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- auto-increment values are not reset with DELETE
DROP vs TRUNCATE vs DELETE

TRUNCATE vs DELETE without WHERE


- auto-increment values are not reset with DELETE

column_1
1
2
3
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- auto-increment values are not reset with DELETE

column_1
1
2
3 DELETE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- auto-increment values are not reset with DELETE

column_1 column_1
1
2
3 DELETE
4


10
DROP vs TRUNCATE vs DELETE
TRUNCATE vs DELETE without WHERE
- auto-increment values are not reset with DELETE

column_1 column_1
1 11
2 12
3 DELETE 13
4 14

… …
10 20

You might also like