📜  Oracle Alter Table

📅  最后修改于: 2020-11-13 06:06:17             🧑  作者: Mango

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);

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