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

相关推荐
ldj202019 小时前
2025 Centos 安装PostgreSQL
linux·postgresql·centos
坤坤不爱吃鱼2 天前
【MySQL\Oracle\PostgreSQL】迁移到openGauss数据出现的问题解决方案
mysql·postgresql·oracle
行星0082 天前
PostgreSQL大表创建分区实战
数据库·postgresql
heart000_113 天前
拯救海量数据:PostgreSQL分区表性能优化实战手册(附压测对比)
数据库·postgresql·性能优化
忧愁的锅盖儿13 天前
PostgreSQL(二十七)索引内部结构
数据库·postgresql
静听山水15 天前
PostgreSQL/Hologres 外部数据包装器系统表 pg_foreign_data_wrapper 详解
数据库·postgresql
进击ing小白15 天前
项目中PostGreSql数据库的维护
数据库·postgresql·oracle
ZaaaaacK17 天前
Linux系统远程操作和程序编译
linux·运维·postgresql
haokan_Jia18 天前
java和postgresql替换多种空白字符(包括制表符、换行、空格等):
java·开发语言·postgresql
中国lanwp18 天前
DBeaver 中 Greenplum、PostgreSQL 和 PostgreSQL (old) 驱动的区别
数据库·postgresql