我在Oracle11g中用其中一列的默认值创建了一个表。语法为:
create table xyz(emp number,ename varchar2(100),salary number default 0);
这已成功创建。由于某些原因,我需要使用相同的旧表结构和数据创建另一个表。因此我创建了一个名为abc
的新表,如下所示
create table abc as select * from xyz.
这里成功创建了"abc“,结构和数据与旧表xyz
相同。但是对于旧表"xyz“中的"salary”列,缺省值设置为"0“。但在新创建的表"abc“中,未设置默认值。
这一切都在Oracle 11g中实现。请告诉我没有设置默认值的原因,以及如何使用select语句设置默认值。
发布于 2011-05-10 09:39:21
您可以将CREATE TABLE中的约束和缺省值指定为SELECT,但语法如下
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;
也就是说,它不会继承来自源表/select的约束。只有数据类型(长度/精度/小数位数)由select确定。
发布于 2011-05-09 19:27:01
您将需要alter table abc modify (salary default 0);
发布于 2014-01-11 01:29:49
新表只继承"not null“约束,不继承其他约束。因此,您可以在创建表之后使用"create table as“命令更改表,也可以通过遵循
create table t1 (id number default 1 not null);
insert into t1 (id) values (2);
create table t2 as select * from t1;
这将创建具有非空约束的表t2。但是对于除"not null“之外的一些其他约束,您应该使用以下语法
create table t1 (id number default 1 unique);
insert into t1 (id) values (2);
create table t2 (id default 1 unique)
as select * from t1;
https://stackoverflow.com/questions/5934083
复制相似问题