Introduction to Postgres DROP Table
In this article, we will learn about how we can manage to drop a table and deleting the table in PostgreSQL database. Only the owner of the table can drop the table using the DROP TABLE command. This command entirely deletes the table structure, its records, associated objects like Views defined on that table, its indexes, constraints, and restrictions. In case, if you just want to delete the contents of the table you can use TRUNCATE or DELETE command. Both these commands will keep everything intact related to the target table and just remove the records present in the target table. Let us begin with the help of the syntax of the DROP TABLE command.
DROP TABLE [IF EXISTS] name_of_the_table [CASCADE | RESTRICT];
- IF EXISTS– It is an optional parameter. This keyword can be used to prevent any error which is resulted while using the above command when there is no table that exists in the current database having the name “name_of_the_table”. If the “IF EXISTS” keyword is used in your query and none of the table named “name_of_the_table” is present in the current server then it will simply notify you about the same and won’t result in any error.
- name_of_the_table– It is the name of the table that you wish to drop and is located in the current database.
- CASCADE– This is used when a foreign key constraint comes into the picture. When the target table has any foreign constraints over other table or the target table is used in any Views then CASCADE property needs to be mentioned for handling the same. When CASCADE is used then all the related views to the target table are also deleted permanently while in the case of foreign key constraints the related table is not affected. All its contents remain intact in the table.
- RESTRICT– This is the default value in the DROP TABLE query. When this is specified or none out of CASCADE or RESTRICT are specified in the query statement then dropping the table is not allowed if any foreign constraint exists on any other table or the target table is used in any other views.
Example of Postgres DROP Table
sudo su - postgres
We can check all the databases present in our database server using the query \l which results in the following output currently for my server –
Now, to check what are all the tables present in the Postgres database, we can use \dt command. Here’s how they result in –
Let us try to delete a certain table in postgres database say table named demo. As it does not contain any table named demo, it will throw the error when we fire the query statement –
DROP TABLE demo;
As can be seen, it gives the error saying table “demo” does not exist. Now, is we use IF EXISTS in our above query in the following way then instead of error it will display a notice shown below –
DROP TABLE IF EXISTS demo;
Let us insert some tables in it. The example will contain two tables namely teams and developers. Each developer belongs to one or the other team and one team is made of many developers. While maintaining the records of both in the database we will firstly create tables for each one of them. Firstly let us create a table for teams which will be our main referenced parent table.
CREATE TABLE teams (
id SERIAL PRIMARY KEY,
department VARCHAR (100)
Now, we will create a table for developers which will act as the referencing or child table. There will be one to many relationships between teams and developers table. team_id will be our referencing key which will refer to the id of the team’s table.
CREATE TABLE developers (
developer_id INTEGER NOT NULL,
team_id INTEGER REFERENCES teams (id),
name VARCHAR (100),
position VARCHAR (100),
technology VARCHAR (100),
PRIMARY KEY (developer_id,team_id)
The output of the above two tables is as follows –
Let us verify the table creation by using \dt command that now results in –
The above query will create just a referencing table ‘developers’ for teams. But while dropping table teams it will restrict us to drop and not allow if any of the referencing tables are present such as developers table for the field id of the team’s record as the default CASCADE/RESTRICT value is RESTRICT. Let us see the query for dropping the table teams –
DROP TABLE teams;
The above query results in the following output which suggests us use CASCADE.
Now, if we want to delete the table teams with a particular id along with the foreign constraint referencing table developers then we will use option CASCADE. Here is the syntax using which we can do so while dropping the table teams.
DROP TABLE teams CASCADE;
that gives the following output –
As can be seen, unlike the previous case this doesn’t throw any error instead it drops the table teams and also deleted the foreign constraint of the developer’s table and gives a notice instead of the error to just inform us that the foreign constraint is also deleted. Now, if we check all the tables in postgres database using \dt then it gives the output –
Now, we will insert some records in the developers table.
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(1,2,'Payal','senior SD','Java');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(2,1,'Heena','Developer','Angular');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(3,2,'Sayali','Developer','Hibernate');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(4,3,'Rahul','Support','Digital Marketing');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(5,3,'Siddhesh','Tester','Maven');
and check by selecting records of the developer’s table.
Let us create a view on the table developers named
CREATE VIEW team2 AS SELECT name,position,technology FROM developers WHERE team_id = 2;
as can be seen after selecting from view team2, two records are retrieved. Now, if we drop the table developers using the command
DROP TABLE developers ;
It gives us an error saying a view named team2 exists and when we use CASCADE in the following manner –
DROP TABLE developers CASCADE;
and select the tables using \dt command then the output is –
Hence, the table developers are deleted and the view team2 is also deleted which can be confirmed by getting values from the view using the query
SELECT * from team2;
which gives output –
The table can be completely deleted along with its existence, structure, records, restrictions, and indexes using the DROP TABLE query and CASCADE can be used for handling any dependencies of the target table such as foreign constraints of other tables on target table or its related views. IF EXISTS helps us to throw a notice instead of an error.
This is a guide to Postgres DROP Table. Here we discuss the introduction of Postgres DROP Table, syntax, parameters, and examples with code implementation. You may also have a look at the following articles to learn more –