A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://neon.com/postgresql/postgresql-tutorial/postgresql-drop-table below:

PostgreSQL DROP TABLE

Summary: in this tutorial, you will learn how to use the PostgreSQL DROP TABLE statement to remove one or more tables from the database.

To drop a table from the database, you use the DROP TABLE statement as follows:

DROP TABLE [IF EXISTS] table_name
[CASCADE | RESTRICT];

In this syntax:

If you remove a table that does not exist, PostgreSQL issues an error. To avoid the error, you can use the IF EXISTS option.

If the table is used in other database objects such as views, triggers, functions, and stored procedures, you cannot remove it. In this case, you have two options:

To remove multiple tables simultaneously, you can place the tables separated by commas after the DROP TABLE keywords:

DROP TABLE [IF EXISTS]
   table_name_1,
   table_name_2,
   ...
[CASCADE | RESTRICT];

Note that you need to have the roles of the superuser, schema owner, or table owner to drop tables.

Let’s take some examples of using the PostgreSQL DROP TABLE statement.

The following statement removes a table named author in the database:

DROP TABLE author;

PostgreSQL issues an error because the author table does not exist.

[Err] ERROR:  table "author" does not exist

To avoid the error, you can use the IF EXISTS option like this.

DROP TABLE IF EXISTS author;
NOTICE:  table "author" does not exist, skipping DROP TABLE

The output indicates that PostgreSQL issued a notice instead of an error.

The following creates new tables called authors and pages. The pages table has a foreign key that references the authors table.

CREATE TABLE authors (
  author_id INT PRIMARY KEY,
  firstname VARCHAR (50) NOT NULL,
  lastname VARCHAR (50) NOT NULL
);

CREATE TABLE pages (
  page_id SERIAL PRIMARY KEY,
  title VARCHAR (255) NOT NULL,
  contents TEXT,
  author_id INT NOT NULL,
  FOREIGN KEY (author_id) REFERENCES authors (author_id)
);

The following statement uses the DROP TABLE to drop the authors table:

DROP TABLE IF EXISTS authors;

Because the authors table has a dependent object which is a foreign key that references the pages table, PostgreSQL issues an error message:

ERROR:  cannot drop table authors because other objects depend on it
DETAIL:  constraint pages_author_id_fkey on table pages depends on table authors
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

In this case, you need to remove all dependent objects first before dropping the author table or use CASCADE option as follows:

DROP TABLE authors CASCADE;

This statement deletes the authors table as well as the constraint in the pages table.

If the DROP TABLE statement removes the dependent objects of the table that are being dropped, it will issue a notice like this:

NOTICE:  drop cascades to constraint pages_author_id_fkey on table pages
DROP TABLE

The following statements create two tables for the demo purposes:

CREATE TABLE tvshows(
  tvshow_id INT GENERATED ALWAYS AS IDENTITY,
  title VARCHAR,
  release_year SMALLINT,
  PRIMARY KEY(tvshow_id)
);

CREATE TABLE animes(
  anime_id INT GENERATED ALWAYS AS IDENTITY,
  title VARCHAR,
  release_year SMALLINT,
  PRIMARY KEY(anime_id)
);

The following example uses a single DROP TABLE statement to drop the tvshows and animes tables:

DROP TABLE tvshows, animes;

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4