Uploaded image for project: 'Derby'
  1. Derby
  2. DERBY-118

Allow any build-in function as default values in table create for columns.

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • None
    • None
    • SQL
    • None

    Description

      It is ok in ij to do a values char(current_date) but is is not allowed to use char(current_date) as default value for clolumns; like for example

      CREATE TABLE DOSENOTWORK (num int, created_by varchar(40) default user, create_date_string varchar(40) default char(current_date))

      Request: It should be allowed to use any build-in function which return a valid type as part of the default value spec.

      There was a e-mail thread for this and the core content/answer was:

      Bernd Ruehlicke wrote:
      >
      > CREATE TABLE DOSENOTWORK (num int, created_by varchar(40) default
      > user, create_date_string varchar(40) default char(current_date))
      >
      > give an error as below - any idea why ?!??!
      >

      The rules for what is acceptable as a column default in Derby say that the only valid functions are datetime functions.
      The logic that enforces this can be seen in the "defaultTypeIsValid" method of the file:

      ./java/engine/org/apache/derby/impl/sql/compile/ColumnDefinitionNode.java

      The Derby Reference Manual also states this same restriction (albeit rather briefly):


      Column Default

      For the definition of a default value, a ConstantExpression is an expression that does not refer to any table. It can include constants, date-time special registers, current schemas, users, and null.


      A "date-time special register" here means a date-time function such as "date(current_date)" in your first example.
      Since the function "char" is NOT a date-time function, it will throw an error.

      I believe this restriction was put in place as part of the "DB2 compatibility" work was that done in Cloudscape a while back.

      Hope that answers your question,
      Army

      [Edit: dagw 2012-06-23
      DERBY-118 started out as a request to allow date-time functions values to be assigned to character columns. This is not standard SQL, but it turns out the Derby allows such conversions at DML time, e.g. in an INSERT statement. Investigating this we found that there a re several instances in which Derby is more restrictive in type conversion at DDL time that at DML time. These instances can be classified as

      a) legacy restrictions that are DB2 specific, but would be legal SQL if lifted (DERBY-5829)
      b) restrictions that are not legal SQL per the standard (this issue)

      We decided not to fix issues in b) those since this would allow users to write non-standard SQL, notwithstanding that fact the Derby also extends on the standard at DML time. Ideally, we would restrict those cases to follow the standard as well, but that would cause compatibility concerns.

      Furthermore, a related issue is that type checks at DDL time, e.g. that a character literal is not too long for its column, are performed at DML time. This is not as per the standard, and we track an improvement in this area as

      c) Catch type mismatch of DEFAULT values at DDL time (DERBY-5825)
      ]

      Attachments

        1. derby-118.diff
          7 kB
          Dag H. Wanvik
        2. derby-118.stat
          0.1 kB
          Dag H. Wanvik
        3. derby-118-all-defaults.diff
          19 kB
          Dag H. Wanvik
        4. derby-118b.diff
          8 kB
          Dag H. Wanvik
        5. derby-118b.stat
          0.1 kB
          Dag H. Wanvik
        6. derby-118c.diff
          11 kB
          Dag H. Wanvik
        7. derby-118c.stat
          0.2 kB
          Dag H. Wanvik
        8. derby-118-limited.diff
          11 kB
          Dag H. Wanvik
        9. derby-118-limited.status
          0.7 kB
          Dag H. Wanvik
        10. derby-118-limited-even-more.diff
          8 kB
          Dag H. Wanvik
        11. derby-118-limited-even-more.status
          0.7 kB
          Dag H. Wanvik
        12. derby-118-limited-even-more-b.diff
          8 kB
          Dag H. Wanvik
        13. derby-118-limited-even-more-b.status
          0.7 kB
          Dag H. Wanvik
        14. derby-118-longvarchar.diff
          4 kB
          Dag H. Wanvik

        Issue Links

          Activity

            People

              dagw Dag H. Wanvik
              bruehlicke Bernd Ruehlicke
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: