Derby's handling of NULL values

前端 未结 4 494
深忆病人
深忆病人 2020-12-19 18:08

I am new to Derby and I noticed that I face similar problems as when using the DB2 RDBMS as far as null values are concerned. The Derby documentation states, th

4条回答
  •  北海茫月
    2020-12-19 18:25

    If you use the VALUES clause on your INSERT, you don't have to cast the NULL values:

    insert into T_AUTHOR (
      ID, FIRST_NAME, LAST_NAME, 
      DATE_OF_BIRTH, YEAR_OF_BIRTH, ADDRESS) 
    VALUES ( 
      1000, 'Lukas', 'Eder', 
      '1981-07-10', null, null 
    );
    

    This will work like you expect (i.e. the database can determine that the NULLs correspond to an integer and varchar(500). This works in both DB2 and Derby (and should work in pretty much any other database engine, as well).

    You can use VALUES with parameter markers as well, without having to CAST them.

    The reason that you have to cast when issuing an insert into ... select from statement is because the SELECT portion takes precedence -- the select statement returns certain data types, regardless of whether they are compatible with the table you're trying to insert them in to. If they aren't compatible, you will either get an error (with strongly typed database engines like DB2 <= 9.5) or the engine will do implicit type conversion (when possible).

提交回复
热议问题