PostgreSQL
PostgreSQL DROP TABLE
Permanently remove an existing PostgreSQL table and all data stored in it.
The DROP TABLE statement
The DROP TABLE statement removes an existing table from the database.
Unlike DELETE, which removes rows while keeping the table,
DROP TABLE removes the table structure as well.
Warning: Dropping a table permanently removes all rows, columns,
indexes, and constraints belonging to that table. Confirm the table name and
create a backup before running this statement.
Drop the products table
To remove the existing products table, run:
DROP TABLE products;
Result
PostgreSQL reports:
DROP TABLE
This confirms that the table was removed successfully.
Display the table
If you try to query the removed table:
SELECT * FROM products;
PostgreSQL returns an error because the table no longer exists:
ERROR: relation "products" does not exist
LINE 1: SELECT * FROM products;
Avoid an error when the table is missing
DROP TABLE IF EXISTS products;
IF EXISTS allows the statement to complete without an error when
the table has already been removed.
Further Reading
Continue learning with these related tutorials:
- PostgreSQL Introduction
- PostgreSQL pgAdmin 4
- PostgreSQL Create Table
- PostgreSQL Insert Data
- PostgreSQL Select Data
- PostgreSQL ADD COLUMN
- PostgreSQL UPDATE
- PostgreSQL ALTER COLUMN
- PostgreSQL DROP COLUMN
- PostgreSQL DELETE
- PostgreSQL DROP TABLE
- PostgreSQL Operators
- PostgreSQL SELECT
- PostgreSQL SELECT DISTINCT
- PostgreSQL WHERE
- PostgreSQL ORDER BY
- PostgreSQL LIMIT
- PostgreSQL MIN and MAX
- PostgreSQL COUNT
- PostgreSQL SUM
- PostgreSQL AVG
- PostgreSQL LIKE
- PostgreSQL IN
- PostgreSQL BETWEEN
- PostgreSQL AS
- PostgreSQL Joins
- PostgreSQL INNER JOIN
- PostgreSQL LEFT JOIN
- PostgreSQL RIGHT JOIN
- PostgreSQL FULL JOIN
- PostgreSQL CROSS JOIN
- PostgreSQL UNION
- PostgreSQL GROUP BY
- PostgreSQL HAVING
- PostgreSQL EXISTS
- PostgreSQL ANY
- PostgreSQL ALL
- PostgreSQL CASE
Continue learning
