Declaració ALTER TABLE d'Oracle

A Oracle, la instrucció ALTER TABLE especifica com afegir, modificar, eliminar o eliminar columnes d'una taula. També s'utilitza per canviar el nom d'una taula.

Com afegir columna a una taula

Sintaxi:

 ALTER TABLE table_name ADD column_name column-definition;  

Exemple:

Tingueu en compte que els clients de taula ja existents. Ara, afegiu una nova columna customer_age a la taula clients.

 ALTER TABLE customers ADD customer_age varchar2(50);  

Ara, s'afegirà una nova columna 'customer_age' a la taula de clients.

Com afegir diverses columnes a la taula existent

Sintaxi:

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

Exemple

 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.  

Com modificar la columna d'una taula

Sintaxi:

 ALTER TABLE table_name MODIFY column_name column_type;  

Exemple:

 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.  

Com modificar diverses columnes d'una taula

Sintaxi:

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

Exemple:

 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.  

Com deixar anar la columna d'una taula

Sintaxi:

 ALTER TABLE table_name DROP COLUMN column_name;  

Exemple:

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

Com canviar el nom de la columna d'una taula

Sintaxi:

 ALTER TABLE table_name RENAME COLUMN old_name to new_name;  

Exemple:

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

Com canviar el nom de la taula

Sintaxi:

 ALTER TABLE table_name RENAME TO new_table_name;  

Exemple:

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