postgresql - 如何将一列添加到 postgresql 模式中的所有表
问题描述
问题很简单,但似乎无法在任何地方找到具体答案。
我需要更新我的 postgresql 架构中的所有表,以包含一个带有默认 NOW() 的时间戳列。我想知道如何通过查询来做到这一点,而不必去每个单独的表。架构中有数百个表,它们都只需要添加一个带有默认值的列。
任何帮助将不胜感激!
解决方案
使用 psql 的简单方法,运行查询以生成命令,保存并运行结果
-- Turn off headers:
\t
-- Use SQL to build SQL:
SELECT 'ALTER TABLE public.' || table_name || ' add fecha timestamp not null default now();'
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND table_schema='public';
-- If the output looks good, write it to a file and run it:
\g out.tmp
\i out.tmp
推荐阅读
- android - Android 发布 - 不支持框架版本
- azure-monitor-workbooks - Azure Workbooks (Application Insights) 中来自同一数据源的多个同时可视化
- c++ - 使用具有私有成员的对象调用模板化成员函数
- mysql - 这个 SQL 查询如何将大学与学生的最高 GPA 配对?
- swift - 当我更改字符串属性时,kCTRubyAnnotationAttributeName 不显示 ruby 文本
- uwp - 删除不必要的依赖 AppxManifest.xml UWP 应用
- c++ - C++ 信号器等待回调
- javascript - 即使删除了与此样式关联的类,Css 样式仍保持不变
- python - 如何在每个 epoch 后正确打乱 Tensorflow 中的数据集
- python - 将变量值传递给表单字段