简体   繁体   中英

Oracle: Insert into table(column_list) values(values_list) with some columns defined as default NULL

Suppose you have this Oracle 11g table:

table sample(
  mykey int, --primary key
  myval varchar2(10 byte) not null,
  col1 varchar2(10 byte),
  col2 varchar2(10 byte) default NULL)

If you execute this statement from sqldeveloper for example:

insert into sample(mykey, myval) values (1, 'test');

You get

mykey    myval   col1    col2
1        test    null    null

Note that it doesn't make a difference for the two nullable columns to specify the 'default null' or not, you'll still have 'null' entered as value for you.

Now, why does the exact same insert from JDBC driver fail with the 'Not enough values' error?

Furthermore, if the column col2 is defined without 'default null', but is still nullable, the JDBC insert works and sets 'null' for both col1 and col2.

Is it just superfluous or plain wrong to specify 'default null' for a column? If Oracle allows me to create the column with that default value and allows me to write SQL code that inserts as in the example correctly, I don't think it's wrong/illegal to do so.

Is this something related to the Oracle DB (or any DB for that matter) or could the application inserting via JDBC driver be issuing a wrong statement?

Thanks in advance

Is it just superfluous or plain wrong to specify 'default null' for a column?

Yes, it superfluous. DEFAULT NULL is implied if you don't specify a default value.

After all that's the only sensible behavior: if you don't provide a value, the only possible choice is to store "unknown" in that column. And that's precisely what NULL means: it is unknown whether there is a value or not.

Now, why does the exact same insert from JDBC driver fail with the 'Not enough values' error?

I don't believe that you are using the exact same statement in your Java program. The "not enough values" error only appears if you - well - don't supply enough values. Eg by providing less values in the values clause than you have columns in the insert part. The following statement would cause that error:

insert into sample(mykey, myval, col1) values (1, 'test');

I haven't tried, but it could be that something like the following would also cause this error:

PreparedStatement pstmt = connection.prepareStatement("insert into sample(mykey, myval, col1, col2) values (?,?,?,?)");
pstmt.setInt(1, 1),
pstmt.setString(2, 'test');

Note that setXXX() was not called for the third column.

I believe that the JDBC would create an SQL similar to the following:

insert into sample(mykey, myval, col1, col2 ) values (?, ?, ?, ?);

Just to say, i am not into JAVA so the parameters may be written differently.

Having said that, your client expects 4 values and not just 2. It wouldn't know that you may want to only pass a certain set of columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM