Skip to main content

Difference between DELETE, DROP and TRUNCATE in SQL Vijay Sinha The Crazy Programmer

In this article, we look into the difference between DELETE, DROP and TRUNCATE commands in SQL. We will also see the general syntax related to each of the commands and the corresponding queries to implement these commands in SQL.

DELETE

The SQL DELETE command is used to delete existing records or rows from a table. The query can be used to delete one or multiple records from an existing table depending on the condition specified. The DELETE command is a Data Manipulation Language (DML) command. This command returns the number of records deleted after execution. With this command, we can also delete all the records of the table if we do not specify the condition in the WHERE clause.

DELETE Command Syntax to REMOVE all Rows:

DELETE FROM Table_Name;

This query deletes all records from the given table name.

DELETE Command Syntax with WHERE Clause:

DELETE FROM Table_Name WHERE [Conditions];

Note: We do not need to list fields or column names of the table because the entire row or tuple is deleted.

Example Queries:

This query removes all records or rows where Student_Id>100 (Student_Id is a column or attribute of table Student)

DELETE FROM Student WHERE Student_Id>100;

This query deletes all rows from table Student as WHERE clause and the conditions are not specified.

DELETE FROM Student;

DROP

The SQL DROP command is used to drop the whole table. In other words, it deletes the entire records as well as the schema and structure of the table along with its named elements. The DROP command is a Data Definition Language (DDL) command. In addition, this command can also be used to drop databases. Databases or tables once dropped are wiped out of existence and cannot be recovered.

DROP Command Syntax:

DROP TABLE Table_Name;

Example Queries:

This query will drop the table Student from the database

DROP TABLE Student;

This query drops/deletes the entire Database School.

DROP DATABASE School;

TRUNCATE

The SQL TRUNCATE command is used to delete all the records/rows from the table without affecting the schema and structure of the database. The TRUNCATE Command is a Data Definition Language (DDL) command. This command cannot be used to delete a single row in a database because the TRUNCATE command cannot use the WHERE clause.

TRUNCATE Command Syntax:

TRUNCATE TABLE Table_Name;

Example Queries:

This query removes all records from table Student.

TRUNCATE TABLE Student;

Difference between DELETE, DROP and TRUNCATE

Now, we compare and contrast between key features of DELETE, DROP and TRUNCATE commands respectively.

DELETE DROP TRUNCATE
1. It is a DML command. 1. It is a DDL command. 1. It is a DDL command.
2. It is used to delete rows or records based on conditions specified in the WHERE clause. 2. It is used to delete the entire table along with its schema and structure respectively. 2. It is used to delete the entire records of a table without affecting the schema of the table.
3. If the WHERE clause is not specified with conditions it deletes all the records of the table. 3. There is no WHERE clause. 3. There is no WHERE clause.
4. It is a DML command. As a result, the operation can be rolled back. 4. It is a DDL command. As a result, the changes cannot be rolled back or undone. 4. It is a DDL command. As a result, the changes cannot be rolled back or undone.
5. It scans every row before deleting making it slower and time-consuming. 5. It is faster and time-saving. 5. It is faster than DELETE in execution because it does not scan every row before deleting which makes it the least time-consuming.
6. Syntax: DELETE FROM TABLE Table_Name WHERE [CONDITIONS]; 6. Syntax: DROP TABLE Table_Name; 6. Syntax: TRUNCATE TABLE            Table_Name;

That’s it for the article you can create a sample table with some dummy records and execute each of the commands to see their working. You can ask your queries in the comment section below.

The post Difference between DELETE, DROP and TRUNCATE in SQL appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/3jFHDia

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20