Microsoft SQL Server

Chapter 6 - DML (Data Manipulation Language)

DELETE

In Microsoft SQL Server, the DELETE statement is used to remove one or more rows from a table. This is an essential part of Data Manipulation Language (DML) operations. It is crucial to include a WHERE clause when using the DELETE statement to ensure you are removing only the rows you want. If no WHERE clause is provided, all the rows in the table will be deleted. Beginners should use this operation carefully to avoid unintentional data loss.

Here’s a breakdown of how to use the DELETE statement with examples:

  1. Basic Syntax of the DELETE Statement:
    • The basic syntax for deleting rows from a table is:
    DELETE FROM table_name
    WHERE condition;
    
  2. Steps for Using the DELETE Statement:
    1. Specify the target table:
      • You begin by specifying the table from which you want to delete rows. This is done using the DELETE FROM clause.
    2. Add a condition to filter the rows:
      • To avoid deleting all rows, use the WHERE clause to specify which rows to delete. Without the WHERE clause, the DELETE statement will remove all rows in the table.
  3. Example - Deleting a Specific Customer:
    • Suppose you want to delete a customer with a specific CustomerID from the Customers table:
    DELETE FROM Customers
    WHERE CustomerID = 10;
    
    • This will delete the customer with CustomerID 10 from the Customers table.
  1. Example - Deleting Multiple Rows:
    • You can delete multiple rows by specifying a condition that matches more than one row. For example, if you want to delete all products in the Products table that belong to a specific category:
    DELETE FROM Products
    WHERE Category = 'Electronics';
    
    • This will remove all products categorized under 'Electronics' from the Products table.
  2. Example - Deleting All Rows (Be Cautious!):
    • If you want to delete all rows from a table, you can do so by omitting the WHERE clause:
    DELETE FROM Sales;
    
    • This will delete every row in the Sales table. Be careful when using this syntax.
  3. Using Subqueries in the DELETE Statement:
    • You can also use a subquery in the WHERE clause to delete rows based on values from another table. For example, if you want to delete all customers who haven't made any purchases:
    DELETE FROM Customers
    WHERE CustomerID NOT IN (SELECT DISTINCT CustomerID FROM Sales);
    
    • This query deletes customers who are not listed in the Sales table.
  4. Best Practices:
    • Always use a WHERE clause: Unless you're absolutely sure, always include a WHERE clause to prevent deleting all rows unintentionally.
    • Use transactions: When deleting a large number of rows, consider wrapping the DELETE statement in a transaction (BEGIN TRANSACTION, ROLLBACK, and COMMIT) to ensure the changes can be reviewed before committing.
    • Backup your data: Before performing significant delete operations, it's a good idea to back up the data, especially in production environments.
    • Use TOP for large deletes: When deleting a large number of rows, it can be more efficient to delete in smaller batches using TOP:
    DELETE TOP (1000) FROM Sales
    WHERE SaleDate < '2020-01-01';
    
    

By following these guidelines and examples, beginners can safely and effectively use the DELETE statement in SQL Server to manage their database records.

Tansy SQL Course - DELETE - Video Thumbnail
Comments(0 comments)

Comments Not Found