Home

Record Maintenance: Deleting Records

   

Deleting a Record

Record maintenance includes viewing records, looking for one or more records, modifying one or more records, or deleting one or more records.

If you find out that a record is not necessary, not anymore, or is misplaced, you can remove it from a table.

To visually delete a record in SQL in Microsoft SQL Server Management Studio or Microsoft Visual Studio, open the table to show its records. On the table, you can right-click the gray box on the left of a record and click Delete:

Deleting a Record

You can also first select the record and press Delete. You would receive a warning to confirm your intention.

In SQL, to delete a record, use the DELETE FROM statement associated with the WHERE operator. The formula to follow is:

DELETE FROM TableName
WHERE Condition(s)

The TableName factor is used to identify a table whose record(s) would be removed.

The Condition(s) factor allows you to identify a record or a group of records that carries a criterion. Make sure you are precise in your criteria so you would not delete the wrong record(s).

Here is an example used to remove a particular record from the table:

private void btnDatabase_Click(object sender, EventArgs e)
{
    using (SqlConnection connection =
	new SqlConnection("Data Source=(local);" +
			  "Database='Exercise1';" +
			  "Integrated Security=yes;"))
    {
	SqlCommand command =
		new SqlCommand("DELETE FROM Videos " +
			       "WHERE VideoTitle = 'The Lady Killers';",
				connection);
	connection.Open();
	command.ExecuteNonQuery();
    }
}

Deleting Many Records

Instead of one, you can delete more than one record at a time. To do this, first select the group of records, either in a range or at random, then either right-click the selection and click Delete or press Delete:

Deleting Many Records

After clicking Delete, you would receive a warning. If you still want to delete the records, you can click OK.

To programmatically delete a group or records, apply the DELETE FROM table formula and use a WHERE condition that can identify each one of the records.

Deleting all Records

If you think all records of a particular table are, or have become, useless, you can clear the whole table, which would still keep its structure.

To visually delete all records from a table, open it in design view, first select all of them, and press Delete. You would receive a warning. If you still want to delete the records, click Yes. If you change your mind, click No.

Using SQL, to clear a table of all records, use the DELETE operator with the following formula:

DELETE TableName;

When this statement is executed, all records from the TableName factor would be removed from the table. Be careful when doing this because once the records have been deleted, you cannot get them back.

Practical LearningPractical Learning: Deleting Records

  1. On the form double-click the Close button and implement it as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
        string strConnection =
    	"Data Source=(local);" +
    	"Database='CPAR2';" +
    	"Integrated Security=yes;";
        string strRepairOrder = "DELETE RepairOrders;";
    
        using (SqlConnection connection = new SqlConnection(strConnection))
        {
    	SqlCommand command = new SqlCommand(strRepairOrder, connection);
    
    	connection.Open();
    	command.ExecuteNonQuery();
    
    	MessageBox.Show("All repair orders have been deleted.");
        }
    
        Close();
    }
  2. Execute the application to display the form
  3. Click the Close button
     
    Deleting Records
  4. Return to your programming environment and change the code of the Close button as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  5. Save all
 

Published on Thursday 03 January 2008

 

Previous Copyright © 2007 FunctionX, Inc. Next