Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default value for column of new created table from select statement in 11g

Tags:

I create a table in Oracle 11g with the default value for one of the columns. Syntax is:

create table xyz(emp number,ename varchar2(100),salary number default 0); 

This created successfully. For some reasons I need to create another table with same old table structure and data. So I created a new table with name abc as

create table abc as select * from xyz.  

Here "abc" created successfully with same structure and data as old table xyz. But for the column "salary" in old table "xyz" default value was set to "0". But in the newly created table "abc" the default value is not set.

This is all in Oracle 11g. Please tell me the reason why the default value was not set and how we can set this using select statement.

like image 811
gitee.com Avatar asked May 09 '11 07:05

gitee.com


People also ask

How do you assign a value to a table column by default?

In Object Explorer, right-click the table with columns for which you want to change the scale and select Design. Select the column for which you want to specify a default value. In the Column Properties tab, enter the new default value in the Default Value or Binding property.

How do I add a column to an existing table with default value in Oracle?

If a new column is added to a table, the column is initially NULL unless you specify the DEFAULT clause. When you specify a default value, the database immediately updates each row with the default value. Note that this can take some time, and that during the update, there is an exclusive DML lock on the table.

What is the default value of column in Oracle?

Oracle Default Value A column can be given a default value using the DEFAULT keyword. The DEFAULT keyword provides a default value to a column when the Oracle INSERT INTO statement does not provide a specific value. The default value can be literal value, an expression, or a SQL Function, such as SYSDATE.


2 Answers

You can specify the constraints and defaults in a CREATE TABLE AS SELECT, but the syntax is as follows

create table t1 (id number default 1 not null); insert into t1 (id) values (2);  create table t2 (id default 1 not null) as select * from t1; 

That is, it won't inherit the constraints from the source table/select. Only the data type (length/precision/scale) is determined by the select.

like image 136
Gary Myers Avatar answered Oct 09 '22 12:10

Gary Myers


The reason is that CTAS (Create table as select) does not copy any metadata from the source to the target table, namely

  • no primary key
  • no foreign keys
  • no grants
  • no indexes
  • ...

To achieve what you want, I'd either

  • use dbms_metadata.get_ddl to get the complete table structure, replace the table name with the new name, execute this statement, and do an INSERT afterward to copy the data
  • or keep using CTAS, extract the not null constraints for the source table from user_constraints and add them to the target table afterwards
like image 44
Frank Schmitt Avatar answered Oct 09 '22 10:10

Frank Schmitt