以字元類型為例
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' | |
|
上面的結果僅僅是顯示會有問題,在插入資料時,預設值仍然為132或abc,不會出現空格,換行,注釋的情況。 其它類型的欄位也會出現類似的情況
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*/