logo

Oracle ALTER TABLE-erklæring

I Oracle angiver ALTER TABLE-sætningen, hvordan man tilføjer, ændrer, sletter eller sletter kolonner i en tabel. Det bruges også til at omdøbe en tabel.

Sådan tilføjes kolonne i en tabel

Syntaks:

 ALTER TABLE table_name ADD column_name column-definition; 

Eksempel:

hej verden java

Overvej, at allerede eksisterende bordkunder. Tilføj nu en ny kolonne customer_age i tabellen kunder.

 ALTER TABLE customers ADD customer_age varchar2(50); 

Nu vil en ny kolonne 'customer_age' blive tilføjet i kundetabellen.

Sådan tilføjer du flere kolonner i den eksisterende tabel

Syntaks:

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

Eksempel

 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. 

Sådan ændres kolonne i en tabel

Syntaks:

understreng streng java
 ALTER TABLE table_name MODIFY column_name column_type; 

Eksempel:

 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. 

Sådan ændres flere kolonner i en tabel

Syntaks:

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

Eksempel:

 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. 

Sådan slipper du kolonne i en tabel

Syntaks:

 ALTER TABLE table_name DROP COLUMN column_name; 

Eksempel:

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

Sådan omdøbes kolonne i en tabel

Syntaks:

 ALTER TABLE table_name RENAME COLUMN old_name to new_name; 

Eksempel:

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

Sådan omdøbes tabellen

Syntaks:

 ALTER TABLE table_name RENAME TO new_table_name; 

Eksempel:

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