修改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;

相关推荐
thinking_talk1 小时前
WorkBuddy应用生成背后的数据层:腾讯云PG
postgresql·腾讯云·云数据库
知识的搬运工旺仔21 小时前
唯一索引与 NULL 值:PostgreSQL 主键约束与 NULLS NOT DISTINCT
数据库·后端·sql·postgresql
jnrjian1 天前
EDB postgresql TDE 加密的文件
postgresql
IvorySQL1 天前
PostgreSQL 日报|逻辑解码竞态条件修复(9 月 20 日)
数据库·人工智能·postgresql
XMYX-01 天前
Rocky_Linux_9.8_安装_PostgreSQL_16
postgresql
foolishlee1 天前
SCRAM-SHA-256
数据库·算法·postgresql
IvorySQL2 天前
IvorySQL 5.6 发布:PG 18.6 内核升级,沙盒即开即用
数据库·人工智能·postgresql
腾科IT教育2 天前
PGCM考试详解:认证价值、含金量及与PGCA、PGCE区别
postgresql·pg·pgcm考试·pgca考试·pgce考试
Zhu7582 天前
docker环境快速部署postgresql数据库18及以上版本
postgresql·容器
l1t2 天前
DeepSeek总结的 pgColumnar 1.0-alpha4 发布说明
数据库·postgresql