Olá, No PostgreSQL não tem como adicionar uma coluna especificando a posição em uma tabela já existente pelo comando alter table.
O Roberto deu uma solução usando o catálogo do PostgreSQL mas como ele mesmo mencionou não é nada recomendável fazer dessa maneira. Eu também não aconselho a fazer isso, você pode ter problemas. Outra possibilidade seria fazer backup, deletar a tabela, recriar e inserir os dados na forma correta. Porém, qual a necessidade de você especificar a ordem para a coluna desejada, isso é fundamental para você? []s 2008/2/7, Roberto Mello <[EMAIL PROTECTED]>: > > 2008/2/7 sergio <[EMAIL PROTECTED]>: > > > > > > Bom Dia. > > > > Quando acrescento um campo novo em uma tabela já existente, ele passa a > ser > > o último campo. Tem como eu colocá-lo em 3º por exemplo? > > Altere o dicionario de dados, a seu proprio risco. Aqui vai como, mas > nao faco garantia nenhuma sobre a integridade dos seus dados ao mexer > assim com o dicionario de dados. > > test=# \d foobar > Table "pg_temp_1.foobar" > Column | Type | Modifiers > ----------+-----------------------+----------- > id | integer | > segunda | character varying(50) | > terceira | character varying(50) | > > > teste=# SELECT c.oid, > teste-# n.nspname, > teste-# c.relname > teste-# FROM pg_catalog.pg_class c > teste-# LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace > teste-# WHERE c.relname ~ '^(foobar)$' -- Nome da minha tabela (foobar) > aqui > teste-# AND pg_catalog.pg_table_is_visible(c.oid) > teste-# ORDER BY 2, 3; > oid | nspname | relname > ---------+-----------+--------- > 7002157 | pg_temp_1 | foobar > (1 row) > > teste=# select attrelid, attname, attnum from pg_catalog.pg_attribute > where attrelid = '7002157' AND attnum > 0 AND NOT attisdropped; > > attrelid | attname | attnum > ----------+----------+-------- > 7002157 | id | 1 > 7002157 | segunda | 2 > 7002157 | terceira | 3 > (3 rows) > > teste=# BEGIN; > BEGIN > teste=# update pg_catalog.pg_attribute set attnum = '4' where attname > = 'terceira' and attrelid = '7002157'; -- coloque um numero maior que > o de outras colunas, pois ha' uma constraint > UPDATE 1 > teste=# update pg_catalog.pg_attribute set attnum = '3' where attname > = 'segunda' and attrelid = '7002157'; > UPDATE 1 > teste=# update pg_catalog.pg_attribute set attnum = '2' where attname > = 'terceira' and attrelid = '7002157'; > UPDATE 1 > teste=# commit; > COMMIT > > teste=# \d foobar > Table "pg_temp_1.foobar" > Column | Type | Modifiers > ----------+-----------------------+----------- > id | integer | > terceira | character varying(50) | > segunda | character varying(50) | > _______________________________________________ > pgbr-geral mailing list > [email protected] > https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral > -- João Paulo www.dextra.com.br/postgres PostgreSQL
_______________________________________________ pgbr-geral mailing list [email protected] https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
