【问题标题】:How to retrieve DDL of a postgres table using Java JDBC?如何使用 Java JDBC 检索 postgres 表的 DDL?
【发布时间】:2021-03-28 05:07:52
【问题描述】:

需要通过截断然后使用转储文件再次填充表来频繁更新表。这样做时,我首先删除了引用键。这减少了填充表的时间。我可以检索以下类似的输出,即使用 Java JDBC 的表的 DDL?我尝试了 DatabaseMetaData 方法来获取表的 DDL,但没有找到可以做到这一点的有用方法。

health=# \d system_user
                        Table "public.system_user"

|    Column    |          Type          | Collation | Nullable | Default |
|--------------+------------------------+-----------+----------+---------|
| user_id      | bigint                 |           | not null |         |
| email        | character varying(255) |           | not null |         |
| first_name   | text                   |           | not null |         |
| last_name    | text                   |           | not null |         |
| password     | character varying(255) |           | not null |         |
| phone_number | character varying(255) |           | not null |         |
| provider_id  | bigint                 |           |          |         |

Indexes:
    "system_user_pkey" PRIMARY KEY, btree (user_id)
Foreign-key constraints:
    "fka48037dbd736797" FOREIGN KEY (provider_id) REFERENCES provider(provider_id)
Referenced by:
    TABLE "annotation_project_annotator" CONSTRAINT "annotation_project_annotator_fk_1" FOREIGN KEY (annotator) REFERENCES system_user(user_id) ON DELETE RESTRICT
    TABLE "annotation_project" CONSTRAINT "annotation_project_fk_2" FOREIGN KEY (supervisor) REFERENCES system_user(user_id) ON DELETE RESTRICT
    TABLE "annotation_project" CONSTRAINT "annotation_project_fk_3" FOREIGN KEY (created_by) REFERENCES system_user(user_id) ON DELETE RESTRICT
    TABLE "annotation_project" CONSTRAINT "annotation_project_fk_4" FOREIGN KEY (updated_by) REFERENCES system_user(user_id) ON DELETE RESTRICT
    TABLE "grammar" CONSTRAINT "fk10b467a7b7ea696d" FOREIGN KEY (user_id) REFERENCES system_user(user_id)
    TABLE "manual_annotation" CONSTRAINT "fk2a02e148b7ea696d" FOREIGN KEY (user_id) REFERENCES system_user(user_id)

【问题讨论】:

    标签: postgresql jdbc ddl database-metadata


    【解决方案1】:

    使用-E 选项启动psql 并运行\d system_user,然后您将获得psql 用于检索输出所需数据的SQL 语句。这应该可以帮助您查询元数据。

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 2020-01-10
      • 1970-01-01
      相关资源
      最近更新 更多