oracle 默认值需要注意的一个地方

来源:互联网 发布:什么软件可以哼歌识曲 编辑:程序博客网 时间:2024/04/20 02:07

以字符类型为例

java代码

public static void main(String[] args) throws Exception {      String sql = "select COLUMN_NAME, DATA_TYPE, Data_Default from user_tab_columns where TABLE_NAME = 'TEST'";      Connection con = DriverManager.getConnection(constr, username, password);      ResultSet rs = con.createStatement().executeQuery(sql);      while(rs.next()) {         System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t|" + rs.getString(3) + "|");      }      rs.close();      con.close();   }

结果

Sql语句

Java结果

备注

create table test(a varchar2(10) default 'abc');

A  VARCHAR2 |'abc'|

正常,默认值单引号后面有括号

alter table test modify a  default 'abc';

A  VARCHAR2 |'abc'

|

默认值后出现换行符

alter table test modify a  default 'abc'  not null;

A  VARCHAR2 |'abc'  |

'abc'not null之间有两个空格,java结果中也出现两个空格

alter table test modify a  default 132   null;

A  VARCHAR2 |132   |

三个空格

alter table test modify a  default 132 /*dd*/ not null;

A  VARCHAR2 |132 /*dd*/ |

加入注释的情况

alter table test modify a  default 'abc' /*dd*/;

A  VARCHAR2 |'abc' /*dd*/

|

有注释,换行

alter table test modify a  default 'abc' null /*dd*/;

A  VARCHAR2 |'abc' |

 

 

上面的结果仅仅是显示会有问题,在插入数据时,默认值仍然为132abc,不会出现空格,换行,注释的情况。

其它类型的字段也会出现类似的情况

alter table test add b number default 11 /*dd*/;alter table test add c date default sysdate /*dd*/;SQL> desc testName Type         Nullable Default        Comments ---- ------------ -------- -------------- -------- A    VARCHAR2(10) Y        123 /*dd*/              B    NUMBER       Y        11 /*dd*/               C    DATE         Y        sysdate /*dd*/ 



原创粉丝点击