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

相关推荐
i***683210 小时前
PostgreSQL 中进行数据导入和导出
大数据·数据库·postgresql
z***751517 小时前
PostgreSQL 查看数据库及表中数据占用空间大小
数据库·postgresql·oracle
dblens 数据库管理和开发工具21 小时前
PostgreSQL物化视图详解:用空间换时间的性能优化利器
数据库·postgresql·性能优化
z***67771 天前
postgresql链接详解
数据库·postgresql
启明真纳1 天前
用 Logstash 把 PostgreSQL 数据实时导出到 Kafka
数据库·postgresql·kafka
Zongsoft2 天前
是时候从 MySQL 转到 PostgreSQL 18 了
postgresql
Navicat中国2 天前
Navicat 高频问题速解:PostgreSQL / MySQL / SQL Server / MongoDB / 达梦
数据库·mysql·mongodb·postgresql·navicat
q***99632 天前
如何查看PostgreSQL的版本
数据库·postgresql
chase。2 天前
关于 nvidia-smi: no devices were found 解决方案
服务器·数据库·postgresql
G***T6913 天前
PostgreSQL全文搜索教程,中文分词配置
数据库·postgresql·中文分词