删除 postgresql 中的所有表
在开发应用程序时,开发人员必须了解他们可以用来与数据库交互的不同方式。这些知识将有助于提高开发效率,因为他们知道解决特定问题的最佳方法。
删除 postgresql 数据库中的所有表
在本教程中,我们将学习可以用来删除特定数据库中的表而不删除数据库本身的不同方法。
我们将首先在 postgresql 数据库管理系统中创建一个数据库,添加几个表,并删除我们使用不同方法创建的表。
使用以下命令登录 postgresql。
>psql-upostgres
创建一个数据库,我们将使用以下查询来保存我们的数据库表。
postgres=#createdatabasecustomer_db;createdatabase
查看我们使用 \l
命令创建的数据库,该命令将返回所有数据库的集合。
postgres=#\l
输出:
list of databases
name | owner | encoding | collate | ctype | access privileges
------------- ---------- ---------- -------------------- -------------------- -----------------------
customer_db | postgres | utf8 | english_kenya.1252 | english_kenya.1252 |
postgres | postgres | utf8 | english_kenya.1252 | english_kenya.1252 |
template0 | postgres | utf8 | english_kenya.1252 | english_kenya.1252 | =c/postgres
| | | | | postgres=ctc/postgres
template1 | postgres | utf8 | english_kenya.1252 | english_kenya.1252 | =c/postgres
| | | | | postgres=ctc/postgres
(4 rows)
注意我们创建的名为 customer_db
的数据库显示在上表的第一行,我们可以使用 \c
命令连接到它。
postgres=#\ccustomer_db;
输出:
you are now connected to database "customer_db" as user "postgres".
我们需要在我们的数据库中有一些数据库表才能删除它们;我们可以使用以下查询在数据库 customer_db
中创建三个表。
customer_db=#createtablecustomer(idserialnotnullunique,firstnamevarchar(50),lastnamevarchar(50),emailvarchar(50),primarykey(id));
customer_db=#createtableproduct(idserialnotnullunique,productnamevarchar(50),productdescriptionvarchar(50),productpriceinteger,primarykey(id));
customer_db=#createtablecart(idserialnotnullunique,productvarchar(50),productpriceinteger,productquantityinteger,totalcostinteger,primarykey(id));
上面的查询创建了三个表,我们可以使用 \dt
来验证这些表是否已经创建。
customer_db=#\dt
输出:
list of relations
schema | name | type | owner
-------- ---------- ------- ----------
public | cart | table | postgres
public | customer | table | postgres
public | product | table | postgres
(3 rows)
在 postgresql 中删除单个模式中的所有表
请注意,我们上面执行的 \dt
命令返回数据库中的表列表,并且该表的模式被列为 public
。
我们将删除此模式中的所有表,重新创建模式,并恢复模式的默认权限,如下所示。
customer_db=#dropschemapubliccascade;
输出:
notice: drop cascades to 3 other objects
detail: drop cascades to table customer
drop cascades to table product
drop cascades to table cart
drop schema
删除模式会将删除操作级联到我们数据库中的三个表。使用以下命令再次创建模式并将所有角色授予 postgres
和 public
模式,如下所示。
customer_db=#createschemapublic;customer_db=#grantallonschemapublictopostgres;customer_db=#grantallonschemapublictopublic;
在 postgresql 中通过指定多个表来删除所有表
如下所示,我们可以使用 sql drop
语句通过使用逗号分隔列表列出我们要删除的所有表来删除表。
在这个查询之后,\dt
表没有找到任何关系证明我们在数据库中的所有表都已成功删除。
customer_db=#droptableifexistscustomer,product,cart;
在 postgresql 中使用带有 pg_tables
的 sql 查询删除所有表
由于在上面的示例中删除了我们的表,因此使用提供的查询再次重新创建三个表以删除下面的所有表。
我们可以编写一个 sql 查询来选择我们要删除的所有表,并对这些表执行级联操作。
customer_db=#select'drop table if exists "'||tablename||'" cascade;'frompg_tableswhereschemaname='public';
输出:
?column?
------------------------------------------
drop table if exists "customer" cascade;
drop table if exists "product" cascade;
drop table if exists "cart" cascade;
(3 rows)
在 postgresql 中使用 table_catalog
和 table_schema
删除所有表
我们可以像在上面的示例中那样编写 sql 查询,并提供作为我们的数据库的目录和用于删除表的公共模式。
customer_db=#select'drop table "'||table_schema||'"."'||table_name||'" cascade;'frominformation_schema.tableswheretable_catalog='customer_db'andtable_schema='public';
输出:
?column?
-----------------------------------------
drop table "public"."customer" cascade;
drop table "public"."product" cascade;
drop table "public"."cart" cascade;
(3 rows)
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在一个 postgresql 查询中使用多个 with 语句
发布时间:2023/03/20 浏览次数:337 分类:postgresql
-
在本教程中,我们将学习如何使用多个 with 语句在 postgresql 中使用两个临时表执行查询。
发布时间:2023/03/20 浏览次数:185 分类:postgresql
-
本文介绍如何在 ubuntu 上找到 postgresql 数据库的配置文件。
发布时间:2023/03/20 浏览次数:409 分类:数据库
-
本文解释了如何直接从终端/命令行或 psql shell 运行 sql 文件。为此,你需要指定主机名、端口、用户名和数据库名称。
发布时间:2023/03/20 浏览次数:89 分类:postgresql
-
本文展示了如何列出 postgresql 上的活动连接。
发布时间:2023/03/20 浏览次数:966 分类:postgresql
-
在 pl/sql 中,你可能需要在 postgres 中使用循环。我们可以使用 for 和 while 语句来创建循环。
发布时间:2023/03/20 浏览次数:141 分类:postgresql
-
本文介绍如何在 postgresql 中仅使用单个查询来重命名列以及更改其类型。
发布时间:2023/03/20 浏览次数:233 分类:postgresql
-
本文介绍如何在 postgresql 中使用 select 方法连接列。
发布时间:2023/03/20 浏览次数:281 分类:postgresql
-
本文展示了如何使用 case 语句并给出了 postgresql 中的示例。