Wednesday, January 1, 2025

How can I drop multiple tables with a single query in SQL?

 In SQL, you can drop multiple tables with a single query by listing them in the DROP TABLE statement, separated by commas. Here's the syntax:

DROP TABLE table1, table2, table3;

This will drop table1, table2, and table3 in a single query.

Notes:

  • Ensure that the tables you are dropping exist in the current database.
  • Be cautious, as dropping tables will permanently remove the tables and all their data.
  • You may also use CASCADE if you want to automatically drop objects dependent on the tables, such as foreign key constraints or views. For example:
DROP TABLE table1, table2 CASCADE;

However, be careful when using CASCADE since it will also remove dependent objects.

Example:

DROP TABLE employees, departments, payroll;

This will drop the employees, departments, and payroll tables.

No comments:

Post a Comment