检查用户定义的类型是否已存在于 PostgreSQL 中

编程入门 行业动态 更新时间:2024-10-27 06:25:55
本文介绍了检查用户定义的类型是否已存在于 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 中

本文发布于:2023-10-14 11:42:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1490960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   类型   用户   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!