修改PostgreSQL表中的字段排列顺序

二、 通过修改系统表(pg_attribute)达到字段重新排序的目的
有关系统表的概述及用途可以查看官网: http://www.pgsqldb.org/pgsqldoc-cvs/catalogs.html

|----------------------------------------------------------------------------------------------|------------------|
| 表名字 | 表用途 |
| pg_class | 表,索引,序列,视图("关系") |
| pg_attribute | 表的列("属性","字段") |

通过pg_class 查找[表,索引,视图等的名字],[表在磁盘上的文件的名字]
SELECT relname, relfilenode FROM pg_class WHERE relname='order_change_table';
查询结果为:order_change_table | 12666
通过pg_attribute 查找[此列/字段所属的表],[字段名字],[字段数目]
SELECT attrelid, attname, attnum FROM pg_attribute WHERE attrelid=12666;
查询结果为:12666 | id | 1 12666 | name | 2 12666 | password | 3 12666 | new_field | 4
更新pg_attribute 的[attnum ]字段(将要移动的字段先更新成数据库里面没有的值,再按顺序更新)。
UPDATE pg_attribute SET attnum =7 WHERE attname='new_field' AND attrelid=12666;
UPDATE pg_attribute SET attnum =6 WHERE attname='name' AND attrelid=12666;
UPDATE pg_attribute SET attnum =5 WHERE attname='password' AND attrelid=12666;
UPDATE pg_attribute SET attnum =2 WHERE attname='new_field' AND attrelid=12666;
UPDATE pg_attribute SET attnum =3 WHERE attname='name' AND attrelid=12666;
UPDATE pg_attribute SET attnum =4 WHERE attname='password' AND attrelid=12666;
再检索表,字段就已经改好顺序了。(缺点 :一旦改错表就崩溃,事先一定要备份好。优点 :直达根处)
SELECT * FROM order_change_table;

相关推荐
星星也在雾里5 小时前
PgBouncer 解决 PostgreSQL 连接数超限 + 可视化监控
数据库·postgresql
zhojiew10 小时前
在本地PostgreSQL使用pgvector构建生成式 AI 应用的实践
数据库·人工智能·postgresql
snowfoootball11 小时前
解决低版本navicat连接PostgreSQl的不兼容报错问题
数据库·postgresql
csdn小瓯14 小时前
PostgreSQL迁移实战:从SQLite到生产级数据库的平滑演进
数据库·postgresql·sqlite
IvorySQL18 小时前
用生成列提升 JSONB 查询效率:PostgreSQL 三种索引方案实测对比
数据库·postgresql
丷丩18 小时前
Postgresql基础实践教程
数据库·postgresql
IvorySQL19 小时前
IvorySQL & PostgreSQL 国内镜像服务上线——更快拉取,更稳体验
数据库·postgresql
l1t19 小时前
DeepSeek总结的PostgreSQL 在 AI 基础设施中日益增长的作用
人工智能·postgresql
Languorous.2 天前
Linux 登录用户、主机名、提示符详解(新手不迷路)
linux·数据库·postgresql
张~颜2 天前
PostgreSQL复制槽
数据库·postgresql