是否有更好的方法将一个列从footballer\u id重命名为id。(从表的所有列中删除footballer)

ALTER TABLE public."Footballer" RENAME COLUMN "footballer_id" TO "id"
ALTER TABLE public."Footballer" RENAME COLUMN "footballer_team" TO "team"

最佳答案

您可以编写查询来获取列名,然后使用结果格式化并运行命令。它使用psql\gexec参数。

SELECT format('ALTER TABLE footballer RENAME COLUMN %I to %I', column_name, substring(column_name,length('footballer_')+1))
FROM information_schema.columns
WHERE table_schema = 'public'
  AND table_name   = 'footballer'
  AND column_name like 'footballer_%';\gexec

关于postgresql - Postgres pgAdmin多列一次重命名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55380741/

10-11 17:22