You use a cfquery
tag with a SQL DELETE statement to delete data from a database. ColdFusion has no cfdelete
tag.
To delete a single record, use the table's primary key in the WHERE condition of a SQL DELETE statement. In the following procedure, Emp_ID is the primary key, so the SQL Delete statement is as follows:
DELETE FROM Employee WHERE Emp_ID = #Form.Emp_ID#
You often want to see the data before you delete it. The following procedure displays the data to be deleted by reusing the form page used to insert and update data. Any data that you enter in the form before submitting it is not used, so you can use a table to display the record to be deleted instead.
form
tag so that it appears as follows:<form action="delete_action.cfm" method="Post">
<html> <head> <title>Delete Employee Record</title> </head> <body> <cfquery name="DeleteEmployee" datasource="CompanyInfo"> DELETE FROM Employee WHERE Emp_ID = #Form.Emp_ID# </cfquery> <h1>The employee record has been deleted.</h1> <cfoutput> You have deleted #Form.FirstName# #Form.LastName# from the employee database. </cfoutput> </body> </html>
http://localhost/myapps/delete_form.cfm?Emp_ID=3.Click Delete Record.
ColdFusion deletes the record in the Employee table and displays a confirmation message.
The following table describes the code and its function:
You can use a SQL condition to delete several records. The following example deletes the records for everyone in the Sales department (which has Dept_ID number 4) from the Employee table:
DELETE FROM Employee WHERE Dept_ID = 4
To delete all the records from the Employee table, use the following code:
DELETE FROM Employee
Caution: Deleting records from a database is not reversible. Use DELETE statements carefully.