Deleting Data

Deleting data in a database can be done with a single delete page. The delete page contains a CFQUERY tag with a SQL delete statement.

Note To delete one record from a database:
  1. Open the file updateform.cfm in Studio.
  2. Modify the file by changing the FORM tag so that it appears as follows:
    <FORM ACTION="deletepage.cfm" METHOD="Post">
    
  3. Save the modified file as deleteform.cfm.
  4. Create a new application page in Studio.
  5. Enter the following code:
    <CFQUERY NAME="DeleteEmployee"
        DATASOURCE="CompanyInfo">
        DELETE FROM Employees
        WHERE Employee_ID = #URL.EmployeeID#
    </CFQUERY>
    
    <HTML>
    <HEAD>
        <TITLE>Delete Employee Record</TITLE>
    </HEAD>
    <BODY>
    <H3>The employee record has been deleted.</H3>
    
    </BODY>
    </HTML>
    
  6. Save the page. as deletepage.cfm.
  7. View deleteform.cfm in a browser, enter values, and click the Submit button.
    The employee is deleted from the Employees table and the message appears.
    

To delete several records, you would specify a condition. The following example demonstrates deleting the records for everyone in the Sales department from the Employee table. The example assumes that there are several Employees in the sales department.

DELETE FROM Employees
WHERE Department = 'Sales'

To delete all the records from the Employees table, you would use the following:

DELETE FROM Employees
Note Deleting records from a database is not reversible. Use delete statements carefully.