To visit my site




HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#



To contact us

© 2002 - 2015 All Rights Reserved Total Application Works


  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
  • <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/SQL/MY_SQL_DELETE.html">
    SQL - delete</a>
    </table>
    </body>
    </html>



SQL - Delete Keyword

Objectives

The objective of the lesson is to describe the SQL DELETE command and present examples that show how to use it. An example can be found at Tutorial: Basic Client/Server Application IV in the source file tcpServer.java found in zip file . The DELETE command is used in the source file tcpServer.java on line 567.

Introduction

So far we have used the SQL SELECT command to retrieve data from a database. Once data has been retrieved, it can be displayed or edited within an application program. However, a question arises: "How do we remove a record or row from a database?" In this tutorial, we will describe how data is removed or deleted from a database using the DELETE command.

Delete Keyword

The SQL DELETE command is used to delete a row of data from a SQL compliant table. The syntax for this command is:


DELETE FROM table_name
  [ WHERE expression ]
  [ LIMIT limit_amount ]

In my Basic Client Server Address Book application, I use a Java application to delete rows or records from my database (see method deleteName() in referenced application tcpServer). In this application, I am using the following form of the SQL DELETE
  
DELETE FROM table_name
  [ WHERE expression ]
 
Before the delete, the table looks like:

 ID  
 FirstName  
 LastName  
 Address  
 City  
 StateOrProvince  
 PostalCode  
 Country  
 EmailAddress  
 HomePhone  
 FaxNumber  
14JulieSmith123 Oak St.RaleighNC27500USAMyID@MyISP919-555-3782919-555-5782
15MaryEasley123 Walnut St.ApexNC27500USAMyID@MyISP919-555-5552919-555-6552
16CatoWillingham123 Jones St.DurhamNC27500USAMyID@MyISP919-555-3492919-555-5492
17MikeJordan123 Saunders St.ApexNC27500USAMyID@MyISP919-555-3882919-555-5882
18CaroleFusemi123 Oak LaneRaleighNC27500USAMyID@MyISP919-555-3472919-555-5472
19PeterWie123 Oak WayGarnerNC27500USAMyID@MyISP919-555-3642919-555-5642
20DonaldHill123 Oak Blvd.CaryNC27500USAMyID@MyISP919-555-3452919-555-5452
21ReginaBell123 Walnut LaneApexNC27500USAMyID@MyISP919-555-3342919-555-5342
22AltheaKeyes123 Walnut WayRaleighNC27500USAMyID@MyISP919-555-3332919-555-5332
23CharlesLawrence123 Whatis St.GarnerNC27500USAMyID@MyISP919-555-3222919-555-5222
24RobertUrengo123 Academy St.DurhamNC27500USAMyID@MyISP919-555-3112919-555-5112
25JohnWieland123 MaIn St.ApexNC27709USAMyID@MyISP(919)-444-9088(919)-444-9089
26KeithMakey404 Walnut Ave.RaleighNC27510USAMyID@MyISP(919)-555-6789(919)-444-9089


Since I am a programmer by profession, I use a program to do my database manipulations. When I use my Basic Client Server Address Book application, to delete a row in the database, I see a screen that looks like:



which is the main Client screen. When I push the Delete button, I see a screen that looks like:



In this example, I will delete row/record with ID = 26 from the database.

The code looks like:
  
try {
   statement = connect.createStatement();
   String query = "DELETE FROM Addresses WHERE id="
                           + 26;
   int result = statement.executeUpdate( query );

   if ( result == 1 )
                  display.append("\nThe record was successful in deleteName()." ) ;

   statement.close();
}
  



Summary

  • Use the command DELETE to remove a row of data from a database. It is of the format:
      
    DELETE FROM table_name
      [ WHERE expression ]
      [ LIMIT limit_amount ]
     
  • The result of the expression must be unique or all values that meet the result will be deleted.
  • When performing a delete, enclose character values in single quotes.
  • Do not enclose numeric values in single quotes.












HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2015 All Rights Reserved Total Application Works