Oracle alter table add column example
2014-01-06 11:26
861 查看
Oracle Tips by Burleson Consulting |
alter table
table_name
add
(
column1_name column1_datatype column1_constraint,
column2_name column2_datatype column2_constraint,
column3_name column3_datatype column3_constraint
);
Here are some examples of Oracle "alter table" syntax to add data columns.
alter table
cust_table
add
cust_sex varchar2(1) NOT NULL;
Her is an example of Oracle "alter table" syntax to add multiple data columns.
ALTER TABLE
cust_table
ADD
(
cust_sex char(1) NOT NULL,
cust_credit_rating number
);
Sometimes, we find that a piece of data that we did not maintain becomes important, and we need to add a new table column to the database. We can add a table to hold the new data or add it to our current schema by adding a column to a current table.
For example, we discover that we need to keep a record of the last date that each author published and what they published. We need to add two columns to the author table,author_last_published (a date) and author_item_published (a varchar2(40)). To do this,
we use the ALTER TABLE ADD command.
SQL> alter table author add (author_last_published date);
Table altered.
SQL> alter table author add (author_item_published varchar2(40));
Table altered.
SQL> desc author
Name Null? Type
------------------------------------- -------- ------------------AUTHOR_KEY VARCHAR2(11)
. . .
AUTHOR_ITEM_PUBLISHED VARCHAR2(40)
Notice that the new columns are at the end of the AUTHOR table. All current rows in the table now contain NULLs for the new columns.
I added each column separately, but you can add as many columns as needed in one command by separating them with commas.
alter table
author
add
(author_last_published date,
author_item_published varchar2(40));
If I define a default value for the new columns, all the current columns will have the default value. (I dropped and recreated the original author table.)
SQL> alter table author add (
2 author_last_published date default SYSDATE,
3 author_item_published varchar2(40)
4 default 'Magazine Article' not null
5 );
Table altered.
相关文章推荐
- Oracle alter table modify column Syntax example
- faster alter table add column
- 在线生产系统执行 alter table add column type default x ;的分析
- 由于一个或多个对象访问此列,ALTER TABLE DROP COLUMN *** 失败。
- Sybase add/delete column from table
- alter table image alter column id int IDENTITY (1, 1) NOT NULL 帮我看看语句是否正确!!
- 【Oracle问题集5】ORA-55622: DML, ALTER and CREATE UNIQUE INDEX operations are not allowed on table
- oracle alter table
- [Oracle] 11g中 ADD COLUMN 功能增强
- add column for a table if this column not exists using T-SQL in SQLServer
- sql server alter column 由于一个或多个对象访问此列,ALTER TABLE ALTER COLUMN 失败
- Oracle alter table
- ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE PARTITION
- GP BUG APPEND TABLE ADD COLUMN
- oracle:表、列、约束等操作,table,constraint,column
- alter table t_agent_adv alter column status set default 1;
- oracle 删除字段方法 alter table set unused
- create [table,column],alter column use SMO
- Add column to existing table with default value
- oracle alter table