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

相关推荐
数据知道11 小时前
PostgreSQL 故障排查:如何找出数据库中最耗时的 SQL 语句
数据库·sql·postgresql
光蛋12 小时前
Docker Compose 助力阿里云 Linux 3 PostgreSQL 高可用部署
postgresql
IvorySQL16 小时前
PostgreSQL 分区表的 ALTER TABLE 语句执行机制解析
数据库·postgresql·开源
Shi_haoliu16 小时前
python安装操作流程-FastAPI + PostgreSQL简单流程
python·postgresql·fastapi
符哥200817 小时前
Ubuntu 常用指令集大全(附实操实例)
数据库·ubuntu·postgresql
l1t19 小时前
DeepSeek总结的PostgreSQL解码GIF文件SQL移植到DuckDB的性能优化方法
sql·postgresql·性能优化
数据知道20 小时前
PostgreSQL 性能优化:分区表实战
数据库·postgresql·性能优化
数据知道20 小时前
PostgreSQL 性能优化:如何提高数据库的并发能力?
数据库·postgresql·性能优化
数据知道20 小时前
PostgreSQL性能优化:内存配置优化(shared_buffers与work_mem的黄金比例)
数据库·postgresql·性能优化
数据知道21 小时前
PostgreSQL 性能优化:连接数过多的原因分析与连接池方案
数据库·postgresql·性能优化