Oracle ALTER TABLE语句

本文概述

  • 如何在表格中添加列
  • 如何在现有表中添加多个列
  • 如何修改表的列
  • 如何修改表格的多列
  • 如何删除表的列
  • 如何重命名表的列
  • 如何重命名表格
在Oracle中, ALTER TABLE语句指定如何添加, 修改, 删除或删除表中的列。它也用于重命名表。
如何在表格中添加列句法:
ALTER TABLE table_name ADD column_name column-definition;

【Oracle ALTER TABLE语句】例:
考虑已经存在的表客户。现在, 在表customers中添加新列customer_age。
ALTER TABLE customers ADD customer_age varchar2(50);

现在, 将在客户表中添加一个新列” 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.

    推荐阅读