*此脚本适用于postgresql
这是一个通用的解决方案,适用于所有情况
此查询可查找任何数据库中所有表字段的描述。
WITH description_bd AS (select colum.schemaname,coalesce(table_name,relname) as table_name , column_name, ordinal_position, column_default, data_type, is_nullable, character_maximum_length, is_updatable,description from
( SELECT columns.table_schema as schemaname,columns.table_name, columns.column_name, columns.ordinal_position, columns.column_default, columns.data_type, columns.is_nullable, columns.character_maximum_length, columns.character_octet_length, columns.is_updatable, columns.udt_name
FROM information_schema.columns
) colum
full join (SELECT schemaname, relid, relname,objoid, objsubid, description
FROM pg_statio_all_tables ,pg_description where pg_statio_all_tables.relid= pg_description.objoid ) descre
on descre.relname = colum.table_name and descre.objsubid=colum.ordinal_position and descre.schemaname=colum.schemaname )
这个查询提出了一个解决方案,以修复所有数据库表的顺序(这会在req字段中生成一个查询,用于修复不同表的顺序)。
它找到表的记录数,然后将该数字增加一。
SELECT table_name, column_name, ordinal_position,column_default,
data_type, is_nullable, character_maximum_length, is_updatable,
description,'SELECT setval('''||schemaname||'.'|| replace(replace(column_default,'''::regclass)',''),'nextval(''','')||''', (select max( '||column_name ||')+1 from '|| table_name ||' ), true);' as req
FROM description_bd where column_default like '%nextva%'
id
列,添加一个新的序列id
列;对于引用 FK,也是同样的操作,然后使用 {oldid, newid} 更新引用 FK,最后删除 {oldid, oldFK}。重命名的顺序可以变化;在极端情况下,旧的和新的 id 和 FK 可以共存,使得旧方案在工作进行中仍然存在。我需要详细说明吗? - joop