How to rename the SQL Table column

9

I want to rename the column via SQL. I'm trying this way:

ALTER TABLE nomes_clientes 
RENAME COLUMN primeiro_nome TO nome, 
RENAME COLUMN segundo_nome TO sobrenome;

But that way it is not working.

    
asked by anonymous 08.09.2017 / 18:56

3 answers

6
___ erkimt ___ How to rename the SQL Table column ______ qstntxt ___

I want to rename the column via SQL. I'm trying this way:

ALTER TABLE nomes_clientes 
    CHANGE primeiro_nome nome VARCHAR(255) NOT NULL;
    CHANGE segundo_nome sobrenome VARCHAR(255) NOT NULL;

But that way it is not working.

    
______ ___ azszpr235761

You need to reset every column as follows:

ALTER TABLE nomes_clientes 
    CHANGE primeiro_nome nome VARCHAR(255) NOT NULL;
    CHANGE segundo_nome sobrenome VARCHAR(255) NOT NULL;
    
______ azszpr235722 ___

Just as @bigown commented will depend on the database you are using, but for these three (Oracle, PostgreSQL and MySQL) it is as follows:

Oracle

%pre%

PostgreSQL

%pre%

MySQL

%pre%

References: MySQL | PostgreSQL Oracle

    
______ azszpr295933 ___

Complementing the answer:

SQLServer

%pre%

In this bank, a ready sp is used. Remember that you must use %code% to execute the procedure. The third parameter 'COLUMN' is required for the case in question, which is to rename a column. Example:

%pre%

For more details on using SP:

References: SQLServer

    
___
08.09.2017 / 20:15
5

Just as @bigown commented will depend on the database you are using, but for these three (Oracle, PostgreSQL and MySQL) it is as follows:

Oracle

ALTER TABLE T_CLASSE_SOCIAL
RENAME COLUMN TESTE TO  TESTE_NOVO;

PostgreSQL

ALTER TABLE distribuidores RENAME COLUMN endereco TO cidade;

MySQL

ALTER TABLE tabela_exemplo CHANGE id_exemplo novo_id_exemplo integer(5) unsigned;

References: MySQL | PostgreSQL Oracle

    
08.09.2017 / 19:07
0

Complementing the answer:

SQLServer

sp_RENAME '[Tabela.NomeColuna]', '[NovoNomeColuna]' , 'COLUMN'

In this bank, a ready sp is used. Remember that you must use exec to execute the procedure. The third parameter 'COLUMN' is required for the case in question, which is to rename a column. Example:

exec sp_RENAME 
          'Pessoa.codPessoa',
          'codPessoaAlterada',
          'COLUMN'

For more details on using SP:

References: SQLServer

    
03.05.2018 / 18:08