SQL Server DELETE and DROP TABLE Commands
Last Updated :
29 Apr, 2024
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.
Similar Reads
SQL Server ALTER TABLE DROP COLUMN In SQL Server, there could be some situations when we may have to delete or drop a column from a table. Sometimes the column in a table was created wrongly or maybe it is no longer required and has become obsolete. So, to drop a column from a table, the ALTER TABLE, DROP COLUMN SQL query is used. In
4 min read
Deleting a Column in SQL Server Structure Query Language (SQL) is a standard language to manipulate and manage the database. SQL is a very powerful language for managing the database. SQL Server Delete command is one of the SQL commands that is used to remove the data that is not useful or due to which inconsistency occurred in th
5 min read
Delete Views in SQL Server In the area of relational databases, SQL Server is one of the most powerful and popular systems. It is flexible to make possible the development of complex data structures and their manipulation. SQL Server offers a crucial tool for managing data which is Delete Views. They allow users to delete row
4 min read
SQL Query to Delete a Data From a Table Based on Date Many of the time we have to delete data based on the date. These dates can be some older dates. For this purpose, we can use delete query along with where clause. This approach helps us to delete some old data in our database. In this article, we are going to delete the data of employees based on th
2 min read
How to Delete Column in SQL In SQL, deleting a column from an existing table is a straightforward process, but it's important to understand the implications and the correct syntax involved. While there is no direct DELETE COLUMN command in SQL, we can achieve this by using the ALTER TABLE command combined with DROP COLUMN.In t
5 min read
SQL Query to DELETE Certain Rows Based on Comparisons with Other Tables In this article, we will see, how to delete certain rows based on comparisons with other tables. We can perform the function by using a subquery in place of the condition in WHERE Clause. A subquery is a query that appears inside another query. It can also be called a nested query. Syntax: SELECT *
2 min read