二、 通过修改系统表(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;