sql – 改变表来修改列的默认值

我有一个要求,我们需要在数据库表中修改列的认值.该表已经是数据库中的现有表,并且列的认值为NULL.
现在,如果添加一个新的认值给这个列,如果我是正确的,它会将列的所有现有NULL更新为新的DEFault值.有没有办法不这样做,但仍然在列上设置一个新的认值.
我的意思是我不希望现有的NULL被更新,并希望它们保持为NULL.

任何帮助在此赞赏.
谢谢

解决方法

你对于会发生什么的信念是不正确的.设置列的认值不会影响表中的现有数据.

我创建一个没有认值的列col2的表

sql> create table foo(
  2    col1 number primary key,3    col2 varchar2(10)
  4  );

Table created.

sql> insert into foo( col1 ) values (1);

1 row created.

sql> insert into foo( col1 ) values (2);

1 row created.

sql> insert into foo( col1 ) values (3);

1 row created.

sql> select * from foo;

      COL1 COL2
---------- ----------
         1
         2
         3

如果我改变表来设置一个认值,那么现有的行都不会改变

sql> alter table foo
  2    modify( col2 varchar2(10) default 'foo' );

Table altered.

sql> select * from foo;

      COL1 COL2
---------- ----------
         1
         2
         3

sql> insert into foo( col1 ) values (4);

1 row created.

sql> select * from foo;

      COL1 COL2
---------- ----------
         1
         2
         3
         4 foo

即使我以后再次更改认值,现有行仍然没有更改

sql> alter table foo
  2    modify( col2 varchar2(10) default 'bar' );

Table altered.

sql> select * from foo;

      COL1 COL2
---------- ----------
         1
         2
         3
         4 foo

sql> insert into foo( col1 ) values (5);

1 row created.

sql> select * from foo;

      COL1 COL2
---------- ----------
         1
         2
         3
         4 foo
         5 bar

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...