Download presentation
Presentation is loading. Please wait.
1
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL Delete Queries Lecturer: Mukhtar Mohamed Ali “Hakaale”
2
Delete Query The DELETE statement is used to delete existing records in a table. In the database structured query language (SQL), the DELETE statement removes one or more records from a table. A subset may be defined for deletion using a condition, otherwise all records are removed. A subset may be defined for deletion using a condition, otherwise all records are removed. Some DBMSs, like MySQL, allow deletion of rows from multiple tables with one DELETE statement (this is sometimes called multi-table DELETE).
3
Usage The Delete statement follows the syntax: Delete FROM TABLE NAME
WHERE Condition Any rows that match the WHERE Condition will be removed from the table. If the WHERE clause is omitted, all rows in the table will removed.
4
Usage Cont.. The Delete statement should thus be used with caution.
The Delete statement doesn’t return any rows; that is , it will not generate a result set Executting a Delete Statement can cause triggers to run that can cause deletes in other tables. For example, if two tables are linked by a foreign key and rows in the referenced table are deleted, then it is common that rows in the referencing table would also have to be deleted to maintain referential integrity.
5
Examples Delete rows from table pies where the column flavour equals Lemon Meringue: Delete rows in trees, if the value of height is smaller than 80. DELETE FROM pipes WHERE flavor = 'Lemon Meringue'; DELETE FROM trees WHERE height < 80;
6
Examples Delete all rows from table.
Delete rows from mytable using a subquery in the where condition: DELETE FROM Mytable; DELETE FROM mytable WHERE id IN ( SELECT id FROM mytable2 );
7
Deleting all rows from a table can be very time consuming.
Related commands Deleting all rows from a table can be very time consuming. Some DBMS offer a TRUNCATE TABLE command that works a lot quicker, as it only alters metadata and typically does not spend time enforcing constraints or firing triggers. DELETE only deletes the rows. For deleting a table entirely the DROP command can be used. Note: It is very important to understand to be carefull when you delete a row from a table, be aware to forget the WHERE Condition.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.