Oracle ALTER TABLE
在Oracle中,ALTER TABLE语句指定如何添加,修改,删除或删除表中的列。
如何在表中添加列
语法:
ALTER TABLE table_name
ADD column_name column-definition;
示例:
请考虑已经存在的表客户。现在,在表客户中添加新列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.