EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

SQL Clear Table

Home » Data Science » Data Science Tutorials » SQL Tutorial » SQL Clear Table

SQL Clear Table

Introduction to SQL Clear Table

We can clear the table contents in SQL using two ways. One of the ways of doing so is to use the data definition language command named TRUNCATE that deletes all the records present in the particular table that exists in our SQL database server. Another way of clearing the table is by using the Data manipulation language command DELETE FROM in which we can delete all the contents of the particular table by skipping any restriction on the columns of the table in the where clause and clearing the contents of the table. In this article, we will see how we can clear the table contents in SQL by using truncate command and delete command.

Using Truncate Command

We can use the ready-made method that is provided in the data definition language commands of SQL named TRUNCATE to remove all the contents of the table in SQL.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of Truncate Command:

TRUNCATE TABLE name of table;

In the above syntax, the name of the table is the table name of which we want to clear all the contents and records that exist in that table. The keyword TABLE needs to be used when using the TRUNCATE command.

Let us consider one example. The table named educba_identity1 which contains 5 rows and has the structure and contents as shown in the output of the below query statement.

Code:

SELECT * FROM `educba_identity1`;

Output:

SQL Clear Table 1

Now to delete all the rows and clear the table named educba_identity1 using the truncate command, we can make the use of the following query statement.

Code:

TRUNCATE TABLE `educba_identity1`;

Output:

SQL Clear Table 2

Let us once again check the contents of the table educba_identity1 and see whether the table is cleared using the following SQL command.

Code:

SELECT * FROM `educba_identity1`;

Output:

SQL Clear Table 3

We can observe that the table named educba_identity1 has been cleared successfully and there is none of the record present in the table.

Using Delete Command

We can clear the contents of the table by using the delete command which is one of SQL command of the set of the data manipulation language commands that are available in SQL. This command is most often used to delete the selected rows present in the table.

Syntax of Delete Command:

DELETE FROM name_of_table;

In the above syntax, the name of the table is the table name of which we want to clear all the contents and records that exist in that table. The keyword TABLE needs to be used when using the TRUNCATE command.

Let us consider one example. The table named educba_identity2 which contains 5 rows and has the structure and contents as shown in the output of the below query statement.

Code:

SELECT * FROM `educba_identity2`

Output:

SQL Clear Table 4

Now to delete all the rows and clear the table named educba_identity2 using the truncate command, we can make the use of the following query statement.

Code:

DELETE FROM `educba_identity2`;

Output:

SQL Clear Table 5

Let us once again check the contents of the table educba_identity1 and see whether the table is cleared using the following SQL command.

Code:

SELECT * FROM `educba_identity2`;

Output:

SQL Clear Table 6

We can observe that the table named educba_identity2 has been cleared successfully and there is none of the record present in the table.

Using Drop Table

We can alternatively use the drop command to delete the whole table along with its contents and structure that completely vanishes the existence of that table in our database in the SQL. But we will have to create the same table again using the CREATE table command in SQL. Note that drop command not only clears the table contents but also the structure of the table.

Let us consider one example in which we have one existing table named educba_writers_demo that has the columns and the contents of the table that are as shown in the output of the below query statement.

Code:

SELECT * FROM `educba_writers_demo`;

Output:

SQL Clear Table 7

Now, let us drop the table by using the drop command in SQL that clears the table contents as well as the table existence in our database.

Code:

DROP TABLE educba_writers_demo;

Output:

drop the table by using the drop command

Let us check the contents of the table by using the following query statement.

Code:

SELECT * FROM `educba_writers_demo`;

The execution of output of the above query statement is as shown below that shows the error saying that the table does not exist.

Output:

error saying

Let us recreate the table using the following create table command.

Code:

CREATE TABLE `educba_writers_demo` (
`id` INT(11) NOT NULL,
`expert_name` VARCHAR(10) NOT NULL,
`rate` DECIMAL(5,2) DEFAULT NULL,
`joining_date_time` DATETIME DEFAULT NULL
) ;

Output:

recreate the table

This successfully creates the table named educba_writers_demo that has the same structure. Let us try rechecking the contents of the table.

Code:

SELECT * FROM `educba_writers_demo`;

Output:

educba_writers_demo

This shows that the table contents have been cleared.

Conclusion – SQL Clear Table

We can clear the contents of the table in SQL by using multiple methods that include the usage of the data definition language command name truncates or data manipulation command named DELETE. An alternative method of clearing the contents of the table involves dropping the table that deleted the table as well as the contents of the table and recreates the table again using the CREATE table command.

Recommended Articles

This is a guide to SQL Clear Table. Here we discuss the introduction to SQL Clear Table, using truncate and delete command, using drop table. You may also have a look at the following articles to learn more –

  1. SQL TRUNCATE()
  2. SQL WITH AS Statement
  3. SQL CASE Statement
  4. SQL DML Commands

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
SQL Tutorial
  • Advanced
    • SQL Formatter
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • SQL REVOKE
    • SQL Select Distinct Count
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL BIGINT
    • SQL Crosstab
    • SQL Wildcard Character
    • SQLAlchemy Filter
    • SQLAlchemy SQLite
    • SQLAlchemy DateTime
    • SQLAlchemy create_engine
    • SQL INSTR()
    • SQL now
    • SQL synonyms
    • SQLite?export to csv
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • ROLLUP in SQL
    • Escape Character SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
    • SQL if then else
    • SQL ignore-case
    • SQL Matches
    • SQL Search String
    • SQL Column Alias
    • SQL extensions
    • SQL Substring Function
    • Charindex SQL
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • SQL Server Versions
    • SQL Case Insensitive
    • SQL Expressions
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL WAITFOR
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • SQL GROUP BY WHERE
    • SQL ROW
    • SQL EXECUTE
    • SQL EXCLUDE
    • SQL Performance Tuning
    • SQL UUID
    • Begin SQL
    • SQL Update Join
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • sqlplus set commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DESC
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • Alternate Key in SQL
    • SQL Super Key
  • Functions
    • SQL Date Function
    • SQL String Functions
    • SQL Compare String
    • Timestamp to Date in SQL
    • SQL Window Functions
    • SQL CONCAT
    • SQL ALTER TABLE
    • SQL MOD()
    • SQL Timestamp
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • SQL HOUR()
    • SQLite?functions
    • ANY in SQL
    • LIKE Query in SQL
    • SQL NOT NULL
    • SQL NOT IN
    • SQL MAX()
    • SQL MIN()
    • SQL SUM()
    • SQL COUNT
    • SQL identity
    • SQL DELETE Trigger
    • SQL Declare Variable
    • SQL Text Search
    • SQL COUNT DISTINCT
    • SQL TEXT
    • SQL Limit Order By
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL MOD()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Server Permission
    • T-SQL INSERT
    • T-SQL Stuff
    • T-SQL ADD Column
    • SQL Ranking Function
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • LEFT OUTER JOIN in SQL
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • NoSQ
    • NoSQL Databases List
    • NoSQL Injection
    • NoSQL vs SQL Databases
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - JDBC Training Course Learn More