Instrução ALTER TABLE do Oracle

No Oracle, a instrução ALTER TABLE especifica como adicionar, modificar, eliminar ou excluir colunas em uma tabela. Também é usado para renomear uma tabela.

Como adicionar coluna em uma tabela

Sintaxe:

 ALTER TABLE table_name ADD column_name column-definition;  

Exemplo:

Considere aquela mesa de clientes já existente. Agora, adicione uma nova coluna customer_age na tabela clientes.

 ALTER TABLE customers ADD customer_age varchar2(50);  

Agora, uma nova coluna 'customer_age' será adicionada na tabela de clientes.

Como adicionar várias colunas na tabela existente

Sintaxe:

 ALTER TABLE table_name ADD (column_1 column-definition, column_2 column-definition, ... column_n column_definition);  

Exemplo

 ALTER TABLE customers ADD (customer_type varchar2(50), customer_address varchar2(50));  
 Now, two columns customer_type and customer_address will be added in the table customers.  

Como modificar coluna de uma tabela

Sintaxe:

 ALTER TABLE table_name MODIFY column_name column_type;  

Exemplo:

 ALTER TABLE customers MODIFY customer_name varchar2(100) not null;  
 Now the column column_name in the customers table is modified to varchar2 (100) and forced the column to not allow null values.  

Como modificar múltiplas colunas de uma tabela

Sintaxe:

 ALTER TABLE table_name MODIFY (column_1 column_type, column_2 column_type, ... column_n column_type);  

Exemplo:

 ALTER TABLE customers MODIFY (customer_name varchar2(100) not null, city varchar2(100));  
 This will modify both the customer_name and city columns in the table.  

Como eliminar coluna de uma tabela

Sintaxe:

 ALTER TABLE table_name DROP COLUMN column_name;  

Exemplo:

 ALTER TABLE customers DROP COLUMN customer_name;  
 This will drop the customer_name column from the table.  

Como renomear coluna de uma tabela

Sintaxe:

 ALTER TABLE table_name RENAME COLUMN old_name to new_name;  

Exemplo:

 ALTER TABLE customers RENAME COLUMN customer_name to cname;  
 This will rename the column customer_name into cname.  

Como renomear tabela

Sintaxe:

 ALTER TABLE table_name RENAME TO new_table_name;  

Exemplo:

 ALTER TABLE customers RENAME TO retailers;  
 This will rename the customer table into 'retailers' table.