Deleting Data from Tables

Team-Fly    

 
Sams Teach Yourself SQL in 24 Hours, Third Edition
By Ronald R. Plew, Ryan K. Stephens
Table of Contents
Hour  5.  Manipulating Data


The DELETE command is used to remove entire rows of data from a table. The DELETE command is not used to remove values from specific columns; a full record, including all columns , is removed. The DELETE statement must be used with cautionas it works all too well. The next section discusses methods for deleting data from tables.

To delete a single record or selected records from a table, the DELETE statement must be used with the following syntax:

 graphics/syntax_icon.gif delete from table_name [where condition]; graphics/input_icon.gif  DELETE FROM ORDERS_TBL   WHERE ORD_NUM = '23A16';  graphics/output_icon.gif 1 row deleted. 

Notice the use of the WHERE clause. The WHERE clause is an essential part of the DELETE statement if you are attempting to remove selected rows of data from a table. You rarely issue a DELETE statement without the use of the WHERE clause. If you do, your results will be similar to the following example:

 DELETE FROM ORDERS_TBL;  11 rows deleted. 
graphics/cautions_icon.gif

If the WHERE clause is omitted from the DELETE statement, all rows of data are deleted from the table. As a general rule, always use a WHERE clause with the DELETE statement.

Also, remember that the DELETE command may have a permanent impact on the database. Ideally, it should be possible to recover erroneously deleted data via a backup, but in some cases, it may be difficult or even impossible to recover data. If data cannot be recovered, it must be re-entered into the databasetrivial if dealing with only one row of data, but not so trivial if dealing with thousands of rows of data. Hence, the importance of the WHERE clause.


graphics/note_icon.gif

The temporary table that was populated from the original table earlier in this hour can be very useful for testing the DELETE and UPDATE commands before issuing them against the original table.



Team-Fly    
Top
 


Sams Teach Yourself SQL in 24 Hours
Sams Teach Yourself SQL in 24 Hours (5th Edition) (Sams Teach Yourself -- Hours)
ISBN: 0672335417
EAN: 2147483647
Year: 2002
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net