DROP
The DROP statement removes a catalog entry added previously with the CREATE command.
Examples
Delete the table with the name tbl:
DROP TABLE tbl;Drop the view with the name view1; do not throw an error if the view does not exist:
DROP VIEW IF EXISTS view1;Drop function fn:
DROP FUNCTION fn;Drop index idx:
DROP INDEX idx;Drop schema sch:
DROP SCHEMA sch;Drop sequence seq:
DROP SEQUENCE seq;Drop macro mcr:
DROP MACRO mcr;Drop macro table mt:
DROP MACRO TABLE mt;Drop type typ:
DROP TYPE typ;Drop text search dictionary dict:
DROP TEXT SEARCH DICTIONARY dict;Syntax
Dependencies of Dropped Objects
SereneDB performs limited dependency tracking for some object types.
By default or if the RESTRICT clause is provided, the entry will not be dropped if there are any other objects that depend on it.
If the CASCADE clause is provided then all the objects that are dependent on the object will be dropped as well.
CREATE SCHEMA myschema;
CREATE TABLE myschema.t1 (i INTEGER);
DROP SCHEMA myschema;error (?i)cannot drop entry "myschema" because there are entries that depend on it
The CASCADE modifier drops both myschema and myschema.t1:
CREATE SCHEMA myschema;
CREATE TABLE myschema.t1 (i INTEGER);
DROP SCHEMA myschema CASCADE;The following dependencies are tracked and thus will raise an error if the user tries to drop the depending object without the CASCADE modifier.
| Depending object type | Dependent object type |
|---|---|
SCHEMA | FUNCTION |
SCHEMA | MACRO TABLE |
SCHEMA | MACRO |
SCHEMA | SCHEMA |
SCHEMA | SEQUENCE |
SCHEMA | TABLE |
SCHEMA | TYPE |
SCHEMA | VIEW |
TABLE | VIEW |
TEXT SEARCH DICTIONARY | INDEX |
Dependencies on Views
Views that reference a table are tracked as dependents of that table. If a view references a table and the table is dropped with RESTRICT (the default), then the drop is rejected and the view stays valid:
CREATE TABLE tbl (i INTEGER);
CREATE VIEW view1 AS SELECT i FROM tbl;
DROP TABLE tbl RESTRICT;
SELECT * FROM view1;db error: ERROR: Cannot drop entry "tbl" because there are entries that depend on it. view "view1" depends on table "tbl". Use DROP...CASCADE to drop all dependents. i
Dependencies across Databases
A view or macro may read objects of another database, and a table may take a column default (nextval) or a column type from one. Those references are tracked the same way: dropping the referenced object with RESTRICT is rejected, and so is DROP DATABASE while an object of another database depends on one of its objects. CASCADE also drops a dependent view or macro in the other database; a dependent table stays, and a column default that used a dropped sequence is removed:
CREATE DATABASE sales;
CREATE TABLE sales.public.orders (id INTEGER, amount INTEGER);
CREATE VIEW sales_report AS SELECT sum(amount) AS total FROM sales.public.orders;
DROP TABLE sales.public.orders;
DROP DATABASE sales;
DROP TABLE sales.public.orders CASCADE;
DROP DATABASE sales;db error: ERROR: Cannot drop entry "orders" because there are entries that depend on it. view "sales_report" depends on table "orders". Use DROP...CASCADE to drop all dependents. db error: ERROR: Cannot drop entry "sales" because there are entries that depend on it. view "sales_report" depends on database "sales". Use DROP...CASCADE to drop all dependents.
DROP DATABASE ... CASCADE drops the dependents in other databases together with the database:
CREATE DATABASE sales;
CREATE TABLE sales.public.orders (id INTEGER, amount INTEGER);
CREATE VIEW sales_report AS SELECT sum(amount) AS total FROM sales.public.orders;
DROP DATABASE sales CASCADE;
SELECT count(*) AS reports FROM information_schema.views WHERE table_name = 'sales_report'; reports
---------
0The same holds between concurrent transactions: when one transaction drops an object and another adds a reference to it from another database, whichever commits second fails at COMMIT and changes nothing.
Limitations on Reclaiming Disk Space
Running DROP TABLE should free the memory used by the table, but not always disk space.
Even if disk space does not decrease, the free blocks will be marked as free.
For example, if we have a 2 GB file and we drop a 1 GB table, the file might still be 2 GB, but it should have 1 GB of free blocks in it.
To check this, use the following PRAGMA and check the number of free_blocks in the output:
PRAGMA database_size;To reclaim space after dropping a table, use the CHECKPOINT statement or compact the database by creating a fresh copy with the COPY FROM DATABASE statement.