参考博文:https://blog.csdn.net/xiaofengtoo/article/details/84395199

修复了其函数中的bug,支持生成包含:字段(支持数组类型字段)、约束、索引(支持生成唯一索引,支持全类型索引)在内的建表语句。

生成的sql指定scheme为:【sch_租户id】,不同scheme生成规则或者不需要指定scheme直接修改下相关代码即可使用。

CREATE OR REPLACE FUNCTION "public"."findattname"("dbinstancename" varchar, "namespace" varchar, "tablename" varchar, "ctype" varchar)
RETURNS "pg_catalog"."varchar" AS $BODY$ declare
tt oid ;
aname character varying default ''; begin
tt := oid from pg_class where relname= tablename
and relnamespace =(select oid from pg_namespace where nspname=namespace and nspowner=(select datdba from pg_database where datname=dbinstancename) ) ;
aname:= array_to_string(
array(
select a.attname from pg_attribute a
where a.attrelid=tt and a.attnum in (
select unnest(conkey) from pg_constraint c where contype=ctype
and conrelid=tt and array_to_string(conkey,',') is not null
)
),',')
; return aname;
end $BODY$
LANGUAGE plpgsql VOLATILE
COST 100
CREATE OR REPLACE FUNCTION "public"."showcreatetable"("dbinstancename" varchar, "namespace" varchar, "tablename" varchar, "tenantid" varchar)
RETURNS "pg_catalog"."varchar" AS $BODY$
declare
tableScript character varying default '';
tableNum int2 ; begin
-- check db extist
tableNum:= count(*) from pg_class where relname=tablename and relnamespace =(
select oid from pg_namespace where nspowner=(select datdba from pg_database where datname=dbinstancename) and nspname = namespace
);
IF tableNum=0 then
return '' ;
end if;
-- columns
tableScript:=tableScript || ' CREATE TABLE IF NOT EXISTS '|| '"sch_'||tenantid||'".'||tablename|| ' ( '|| array_to_string(
array(
select concat( c1, c2, c3, c4, c5, c6 ) as column_line
from (
select '"'||column_name||'"' || ' ' || case when data_type='ARRAY' then ltrim(udt_name,'_')||'[]' else data_type end as c1,
case when character_maximum_length > 0 then '(' || character_maximum_length || ')' end as c2,
case when numeric_precision > 0 and numeric_scale < 1 then null end as c3,
case when numeric_precision > 0 and numeric_scale > 0 then null end as c4,
case when is_nullable = 'NO' then ' NOT NULL' end as c5,
case when column_default is not Null then ' DEFAULT' end || ' ' || replace(column_default, '::character varying', '') as c6
from information_schema.columns
where table_name = tablename
and table_catalog=dbinstancename
and table_schema=namespace
order by ordinal_position
) as string_columns
),' , ') ||',' ; -- 约束
tableScript:= tableScript || array_to_string(
array(
select concat(' CONSTRAINT ','"'||conname||'"' ,c ,u,p,f) from (
select conname,
case when contype='c' then ' CHECK('|| consrc ||')' end as c ,
case when contype='u' then ' UNIQUE('|| ( select findattname(dbinstancename,namespace,tablename,'u') ) ||')' end as u ,
case when contype='p' then ' PRIMARY KEY ('|| ( select findattname(dbinstancename,namespace,tablename,'p') ) ||')' end as p ,
case when contype='f' then ' FOREIGN KEY('|| ( select findattname(dbinstancename,namespace,tablename,'u') ) ||') REFERENCES '||
(select p.relname from pg_class p where p.oid=c.confrelid ) || '('|| ( select findattname(dbinstancename,namespace,tablename,'u') ) ||')' end as f
from pg_constraint c
where contype in('u','c','f','p') and conrelid=(
select oid from pg_class where relname=tablename and relnamespace =(
select oid from pg_namespace where nspowner=(select datdba from pg_database where datname=dbinstancename) and nspname = namespace
)
)
) as t
) ,',' ) || ' ); '; --
/** **/
--- 获取非约束索引 column
-- CREATE UNIQUE INDEX pg_language_oid_index ON pg_language USING btree (oid); -- table pg_language
tableScript:= tableScript || array_to_string(
array(
select 'CREATE '||case when is_unique_index=true then 'UNIQUE INDEX' else 'INDEX' end ||'"' || indexrelname ||'"' || ' ON ' || '"sch_'||tenantid||'".'||tablename|| ' USING '||index_type|| '(' || attname || ');' from (
SELECT
i.relname AS indexrelname , x.indkey,
( select array_to_string (
array(
select a.attname from pg_attribute a where attrelid=c.oid and a.attnum in ( select unnest(x.indkey) ) )
,',' ) )as attname, x.indisunique is_unique_index,am.amname index_type FROM pg_class c
JOIN pg_index x ON c.oid = x.indrelid
JOIN pg_class i ON i.oid = x.indexrelid
join pg_am am on am.oid = i.relam
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname=tablename and i.relname not in
( select constraint_name from information_schema.key_column_usage where table_name=tablename )
and c.relnamespace =(
select oid from pg_namespace where nspowner=(select datdba from pg_database where datname=dbinstancename) and nspname = namespace
)
)as t
) ,'' ); return tableScript; end
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100

postgreSQL生成建表语句的更多相关文章

  1. sqlserver 表中记录生成insert,可以加条件,可以生成建表语句

    sqlserver 表中记录生成insert,可以加条件,可以生成建表语句 create PROCEDURE [sp_getinsert] ( ) , --如果非默认架构,可以加上架构名 例如:sch ...

  2. 【SQL】ORACLE在sqlplus中使用spool方式生成建表语句

    在实际生产中有时我们需要将一张表的数据导入到另外一张表,如果有PLSQL,我们可以通过PLSQL工具将数据导出为sql脚本,然后再在另外一个数据库中执行这个脚本.但有时在实际生产中我们没有PLSQL这 ...

  3. Hibernate根据配置文件,生成建表语句

    import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ...

  4. sqlite3 根据实体自动生成建表语句

      public class BuildSqlTool { public static string GetCreateTableSql(object t) { //CREATE TABLE &quo ...

  5. Oracle 自动生成hive建表语句

    从 oracle 数据库导数到到 hive 大数据平台,需要按照大数据平台的数据规范,重新生成建表的 SQL 语句,方便其间,写了一个自动生成SQL的存储过程. ① 创建一张表,用来存储源表的结构,以 ...

  6. 脚本工具---自动解析mysql建表语句,生成sqlalchemy表对象声明

    常规建表语句: CREATE TABLE `test_table` ( `id` int(11) NOT NULL, `name` char(64) NOT NULL, `password` char ...

  7. 基于表的数据字典构造MySQL建表语句

    表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...

  8. 根据javabean转换为mysql建表语句与mapper内容

    原文地址:  https://www.cnblogs.com/Jeffscnblog/p/10072483.html 一般上,我们会使用数据库表转换为javabean.dao.或是mapper,就叫逆 ...

  9. excel的宏与VBA实践——建表语句

    一.建表语句 不带分区版本:V1.0: Sub createTableDDL() '自动创建建表语句 '定义换行和TAB Ln = ) + ) TB = ) '定义脚本目录 Dim dir AS St ...

随机推荐

  1. python 访问sql server数据库

    访问数据库 cnxn = pyodbc.connect("Driver={SQL Server};Server=localhost;Database=用户名;uid=sa;pwd=密码&qu ...

  2. Spark学习之路 (九)SparkCore的调优之数据倾斜调优[转]

    调优概述 有的时候,我们可能会遇到大数据计算中一个最棘手的问题--数据倾斜,此时Spark作业的性能会比期望差很多.数据倾斜调优,就是使用各种技术方案解决不同类型的数据倾斜问题,以保证Spark作业的 ...

  3. css实现带边框的冒泡提示框

    需求是实现这种效果, 因为内容是动态的,使用图片不是很好: 原因: 如果内容确定只是一行,可以效果图裁剪3部分,分别是两侧和中间部分,然后用backgroud插入三张图片,但是要是内容是2行就不好处理 ...

  4. 关于学习java虚拟机的知识整理一:jvm内存区域

    之前由于考研,对于虚拟机的认识疏忽了太多,现在重新整理回顾一下. 如上图所示,jvm的内存区域(运行时数据区)共分为5处:方法区(Method Area).虚拟机栈(vm Stack).本地方法栈(N ...

  5. 解决ios手机中input输入框光标过长的问题

    修改前css部分代码: .receiving-info .receiving-info-list input { display: inline-block; width: 70%; font-siz ...

  6. BDA3 Chapter 1 Probability and inference

    1. uncertainty aleatoric uncertainty 偶然不确定性 epistemic uncertainty 认知不确定性 2. probability VS likelihoo ...

  7. Dalsa 8K彩色相机Camera link C#采图

    一个采图工具,所以界面做的很简单. private SapAcquisition m_Acquisition; private SapBuffer m_Buffers; private SapAcqT ...

  8. LaTeX技巧005:定制自己炫酷的章节样式实例

    示例一: 实现代码: \usepackage[Lenny]{fncychap} 示例二: 实现代码: \usepackage[avantgarde]{quotchap} \renewcommand\c ...

  9. Babel 7 主要改变

    1.不支持Node:0.10,0.12,4,5版本 2.更换命名-@babel/xxx 3.移除以年份命名的presets,统一更换成@babel/preset-env 4.移除 ’Stage‘ pr ...

  10. MVC理解(全程白话不拽词)

    我所发的所有博客只为了给想干程序员,但是基础没有或者很差,刚入职场的人 所有的观点不一定准确,我不懂的或者不理解的都会备注出来 先说MVC MVC理解为:M  =  Model  =  数据库表里面每 ...