Oracle ALTER TABLE 文

Oracle では、ALTER TABLE ステートメントは、テーブル内の列を追加、変更、削除、または削除する方法を指定します。テーブルの名前を変更する場合にも使用されます。

テーブルに列を追加する方法

構文:

 ALTER TABLE table_name ADD column_name column-definition;  

例:

既存のテーブル顧客を考慮してください。次に、customers テーブルに新しい列 customer_age を追加します。

 ALTER TABLE customers ADD customer_age varchar2(50);  

これで、customers テーブルに新しい列「customer_age」が追加されます。

既存のテーブルに複数の列を追加する方法

構文:

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

 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.  

テーブルの列を変更する方法

構文:

 ALTER TABLE table_name MODIFY column_name column_type;  

例:

 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.  

テーブルの複数の列を変更する方法

構文:

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

例:

 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.  

テーブルの列を削除する方法

構文:

 ALTER TABLE table_name DROP COLUMN column_name;  

例:

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

テーブルの列の名前を変更する方法

構文:

 ALTER TABLE table_name RENAME COLUMN old_name to new_name;  

例:

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

テーブルの名前を変更する方法

構文:

 ALTER TABLE table_name RENAME TO new_table_name;  

例:

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