Open In App

SQL Server DELETE and DROP TABLE Commands

Last Updated : 29 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In SQL Server, managing data involves not only inserting and updating records but also removing them when they’re no longer needed. Two important commands for data removal are DELETE and DROP TABLE. These commands play crucial roles in maintaining database integrity and managing database structures.

In this article, we’ll learn about the DELETE and DROP TABLE commands in SQL Server, exploring their syntax, use cases, and so on.

Introduction to DELETE and DROP TABLE Commands

DELETE Command

  • The DELETE command is used to remove rows from a table based on specified criteria. It allows you to selectively delete data while keeping the table structure undamaged.
  • The DELETE command is transactional which means we can wrap it in a transaction to ensure that either all the specified rows are deleted or none are.
  • SQL Server logs DELETE operations which can be useful for auditing purposes or in case we need to recover deleted data using database backups or transaction logs.
  • The DELETE command can be used to remove rows from a table that is referenced by foreign key constraints in other tables.

Syntax:

DELETE FROM table_name
WHERE condition;

DROP TABLE Command

  • The DROP TABLE command is used to delete an entire table from the database. This command removes both the table structure and all associated data, effectively deleting the table from the database.
  • It is a straightforward way to delete an entire table without the need to delete individual rows.
  • Dropping a table releases the disk space occupied by the table and its data, which can be useful for managing database storage.

Syntax:

DROP TABLE [IF EXISTS] table_name;

Example of SQL Server DELETE and DROP TABLE Commands

Let’s start by creating a sample products table in SQL Server and then performing the DELETE and DROP TABLE tasks.

-- Create the products table
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
price DECIMAL(10, 2),
quantity INT
);

-- Insert sample data into the products table
INSERT INTO products (product_id, product_name, price, quantity)
VALUES
(1, 'Laptop', 999.99, 10),
(2, 'Smartphone', 699.99, 20),
(3, 'Tablet', 399.99, 15),
(4, 'Headphones', 99.99, 30);
product_id product_name price quantity
1 Laptop 999.99 10
2 Smartphone 699.99 20
3 Tablet 399.99 15
4 Headphones 99.99 30

This SQL script creates a products table with columns for product_id, product_name, price, and quantity, and inserts some sample data into the table.

Example Using the DELETE Command

-- Delete all products with a price less than 500
DELETE FROM products
WHERE price < 500;

Output:

product_id product_name price quantity
1 Laptop 999.99 10
2 Smartphone 699.99 20

In this example, the DELETE command removes all rows from the orders table where the total_amount column is less than 500.

Example Using the DROP TABLE Command

-- Drop the products table
DROP TABLE products;

In this example, the DROP TABLE command deletes the products table from the database, including all its columns and data.

Dropping a Table with Constraints

When a table has foreign key constraints referencing it, we must first drop those constraints before dropping the table itself. Otherwise, we will encounter an error. Here’s how to drop a table with constraints:

-- Create a new table with a foreign key constraint referencing the products table
CREATE TABLE orders (
order_id INT PRIMARY KEY,
product_id INT,
FOREIGN KEY (product_id) REFERENCES products(product_id)
);

-- Drop the orders table
DROP TABLE orders;

In this example, we first create a new table orders with a foreign key constraint referencing the products table. Then, we drop the orders table before dropping the products table.

Conclusion

The DELETE and DROP TABLE commands are essential tools for managing data in SQL Server databases. While the DELETE command removes specific rows from a table based on specified criteria, the DROP TABLE command deletes entire tables from the database, including all data and table structure. In this article, we explored the concepts of DELETE and DROP TABLE commands in SQL Server, provided examples of their usage, and discussed the output of these commands. As you continue to work with SQL Server, mastering these commands will enable you to effectively manage data and database structures in your applications. Experiment with these concepts in your SQL Server environment to deepen your understanding and enhance your database management skills.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads