How To Add a New Column to an Existing Table with a Default Value?
Submitted by: AdministratorIf you want to add a new column to an existing table, and insert a default value in this column on all existing data rows, you can use the ALTER TABLE ... ADD statement with the DEFAULT clause. Here is an example script:
SQL> CREATE TABLE emp_dept_90
2 AS SELECT * FROM employees WHERE department_id=90;
Table created.
SQL> ALTER TABLE emp_dept_90
2 ADD (vacation NUMBER DEFAULT 10);
Table altered.
SQL> SELECT first_name, last_name, vacation
2 FROM emp_dept_90;
<pre>FIRST_NAME LAST_NAME VACATION
-------------------- ------------------------- ----------
Steven King 10
Neena Kochhar 10
Lex De Haan 10</pre>
As you can see, the "DEFAULT 10" clause did inserted 10 to all existing data rows.
Submitted by: Administrator
SQL> CREATE TABLE emp_dept_90
2 AS SELECT * FROM employees WHERE department_id=90;
Table created.
SQL> ALTER TABLE emp_dept_90
2 ADD (vacation NUMBER DEFAULT 10);
Table altered.
SQL> SELECT first_name, last_name, vacation
2 FROM emp_dept_90;
<pre>FIRST_NAME LAST_NAME VACATION
-------------------- ------------------------- ----------
Steven King 10
Neena Kochhar 10
Lex De Haan 10</pre>
As you can see, the "DEFAULT 10" clause did inserted 10 to all existing data rows.
Submitted by: Administrator
Read Online Oracle Database Job Interview Questions And Answers
Top Oracle Database Questions
☺ | What Happens to Indexes If You Drop a Table? |
☺ | How To Recover a Dropped Table in Oracle? |
☺ | How To Use Values from Other Tables in UPDATE Statements using Oracle? |
☺ | How To Invoke the Data Pump Import Utility? |
☺ | How To Create Tables for ODBC Connection Testing? |
Top DB Oracle Categories
☺ | Oracle PL-SQL Interview Questions. |
☺ | Oracle DBA Interview Questions. |
☺ | Oracle D2K Interview Questions. |
☺ | OCI Interview Questions. |
☺ | Oracle RMAN Interview Questions. |