检查用户定义的类型是否已存在于 PostgreSQL 中 [英] Check if a user-defined type already exists in PostgreSQL

查看:62
本文介绍了检查用户定义的类型是否已存在于 PostgreSQL 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在数据库中创建了一些用户定义的类型,

Say I have created some user-defined types in the DB,

创建类型 abc ...

那么是否可以确定用户定义的类型是否存在?也许,使用任何 postgres 信息表?

Is it then possible to determine if the user-defined type exists or not? Perhaps, using any of the postgres information tables?

这样做的主要原因是因为 PostgreSQL 似乎不支持 CREATE OR REPLACE TYPE ...,并且如果某个类型被创建多次,我希望能够删除首先现有的,然后重新加载新的.

The main reason for this is since PostgreSQL does not seem to support CREATE OR REPLACE TYPE ..., and if a certain type gets created more than once, I want to be able to drop the existing one first, then re-load the new one.

推荐答案

我在这里添加了在简单脚本中创建类型的完整解决方案,而无需为此目的创建函数.

I add here the complete solution for creating types in a simple script, without the need of creating a function just for this purpose.

--create types
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'my_type') THEN
        CREATE TYPE my_type AS
        (
            --my fields here...
        );
    END IF;
    --more types here...
END$$;

这篇关于检查用户定义的类型是否已存在于 PostgreSQL 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆