如何获取postgresql中所有表的序列更改查询

时间:2014-08-01 07:12:10

标签: postgresql

跟进我之前的question's answer
我需要编写脚本来改变我所有表的序列(100+) 下面给出的是表gcompany

的示例
alter sequence seq_gcompany_id owned by company.companyid;

如何针对所有Alter获取此sequence个查询?

提示 :所有序列名称均以seq_g

开头

1 个答案:

答案 0 :(得分:0)

试试这个。

Select  'alter sequence '||_seq||' owned by '||_table||'.'||_column||';' FROM (SELECT   
  n.nspname AS _schema,
  c.relname AS _table,
  a.attname AS _column,
  SUBSTRING(d.adsrc FROM E'nextval\\(''([^'')]+)''') AS _seq
FROM pg_attrdef       AS d
  JOIN pg_attribute AS a ON a.attrelid = d.adrelid AND a.attnum = d.adnum
  JOIN pg_class     AS c ON c.oid = d.adrelid
  JOIN pg_namespace AS n ON n.oid = c.relnamespace
WHERE adsrc LIKE 'nextval(''%') t;

并以pgScript

执行输出

阅读 关于 pg_class pg_attribute