简体   繁体   中英

How to rearrange the column position in PostgreSQL

how to re-arrange the column names in PostgreSQL table with records

entityid formattedfilename 
-------- ----------------- 
1        file1             
2        file2  

Re-arrange below format with record

formattedfilename entityid 
----------------- -------- 
file1             1        
file2             2   

You can create a VIEW for this,
see following demo

create table foo (entityid int,formattedfilename text);

insert into foo values (1,'file1');
insert into foo values (2,'file2');

select * from foo

RESULT

entityid formattedfilename 
-------- ----------------- 
1        file1             
2        file2             

Now create a view like below

create or replace view vfoo as 
select formattedfilename,entityid from foo .

select * from vfoo

RESULT

formattedfilename entityid 
----------------- -------- 
file1             1        
file2             2  

Still you want to do it with the table itself then refer : https://wiki.postgresql.org/wiki/Alter_column_position

Just select it something like:

SELECT formattedfilename, entityid
FROM mytable

if you want SELECT * should return you that format then i would say you better have a view over your table and query on view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM