Declarația Oracle ALTER TABLE

În Oracle, instrucțiunea ALTER TABLE specifică cum să adăugați, să modificați, să aruncați sau să ștergeți coloanele dintr-un tabel. De asemenea, este folosit pentru a redenumi un tabel.

Cum se adaugă o coloană într-un tabel

Sintaxă:

 ALTER TABLE table_name ADD column_name column-definition;  

Exemplu:

Luați în considerare că clienții de masă deja existenți. Acum, adăugați o nouă coloană customer_age în tabelul customers.

 ALTER TABLE customers ADD customer_age varchar2(50);  

Acum, o nouă coloană „customer_age” va fi adăugată în tabelul clienți.

Cum să adăugați mai multe coloane în tabelul existent

Sintaxă:

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

Exemplu

 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.  

Cum se modifică coloana unui tabel

Sintaxă:

 ALTER TABLE table_name MODIFY column_name column_type;  

Exemplu:

 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.  

Cum se modifică mai multe coloane ale unui tabel

Sintaxă:

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

Exemplu:

 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.  

Cum să aruncați coloana dintr-un tabel

Sintaxă:

 ALTER TABLE table_name DROP COLUMN column_name;  

Exemplu:

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

Cum se redenumește coloana unui tabel

Sintaxă:

 ALTER TABLE table_name RENAME COLUMN old_name to new_name;  

Exemplu:

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

Cum se redenumește tabelul

Sintaxă:

 ALTER TABLE table_name RENAME TO new_table_name;  

Exemplu:

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