Derby
  1. Derby
  2. DERBY-158

PRIMARY KEY does not imply NOT NULL

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 10.1.1.0
    • Component/s: SQL
    • Labels:
      None

      Description

      PRIMARY KEY does not imply NOT NULL. Derby issues error message:

      ij> create table tab (i integer primary key);
      ERROR 42831: 'I' cannot be a column of a primary key or unique key because it can contain null values.

      This is neither compliant with SQL-92 nor with SQL-99 (Feature E141-08, I think).

      1. derby-158-1.patch
        8 kB
        Jeremy Boynes
      2. derby-158-2.patch
        16 kB
        Jeremy Boynes

        Activity

        Bernt M. Johnsen created issue -
        Jeremy Boynes made changes -
        Field Original Value New Value
        Assignee Jeremy Boynes [ jboynes ]
        Hide
        Jeremy Boynes added a comment -

        Patch to TableElementList that implicitly adds NOT NULL to columns included in a PRIMARY KEY constraint contained in a CREATE TABLE statement. However, it does not allow UNIQUE to be added in CREATE TABLE if any of the columns are NULL; similarly UNIQUE or PRIMARY KEY constraints cannot be added with ALTER TABLE if any of the columns are nullable.

        I am posting this first for review as a couple of tests still need to be updated.

        Show
        Jeremy Boynes added a comment - Patch to TableElementList that implicitly adds NOT NULL to columns included in a PRIMARY KEY constraint contained in a CREATE TABLE statement. However, it does not allow UNIQUE to be added in CREATE TABLE if any of the columns are NULL; similarly UNIQUE or PRIMARY KEY constraints cannot be added with ALTER TABLE if any of the columns are nullable. I am posting this first for review as a couple of tests still need to be updated.
        Jeremy Boynes made changes -
        Attachment derby-158-1.patch [ 19114 ]
        Hide
        Jeremy Boynes added a comment -

        Patch to TableElementList plus test changes.
        If I don't hear anything I'll commit this in a couple of days.

        Show
        Jeremy Boynes added a comment - Patch to TableElementList plus test changes. If I don't hear anything I'll commit this in a couple of days.
        Jeremy Boynes made changes -
        Attachment derby-158-2.patch [ 19120 ]
        Hide
        Jeremy Boynes added a comment -

        URL: http://svn.apache.org/viewcvs?view=rev&rev=156570
        Log:
        Fix for DERBY-158 PRIMARY KEY does not imply NOT NULL
        Columns specified in a PRIMARY KEY constraint in CREATE TABLE
        are implicitly set to NOT NULL per the SQL spec.

        If a PRIMARY KEY constraint is added using ALTER TABLE then all
        columns must also be NOT NULL. Similarly, columns
        specified in a UNIQUE constraint must still be explictly NOT NULL.

        Modified:
        incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/TableElementList.java
        incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/db2Compatibility.out
        incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/nulls.out
        incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/primarykey.out
        incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/db2Compatibility.sql
        incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nulls.sql
        incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/primarykey.sql

        Show
        Jeremy Boynes added a comment - URL: http://svn.apache.org/viewcvs?view=rev&rev=156570 Log: Fix for DERBY-158 PRIMARY KEY does not imply NOT NULL Columns specified in a PRIMARY KEY constraint in CREATE TABLE are implicitly set to NOT NULL per the SQL spec. If a PRIMARY KEY constraint is added using ALTER TABLE then all columns must also be NOT NULL. Similarly, columns specified in a UNIQUE constraint must still be explictly NOT NULL. Modified: incubator/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/TableElementList.java incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/db2Compatibility.out incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/nulls.out incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/primarykey.out incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/db2Compatibility.sql incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/nulls.sql incubator/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/primarykey.sql
        Jeremy Boynes made changes -
        Fix Version/s 10.1.0.0 [ 10993 ]
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Bernt M. Johnsen made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Gavin made changes -
        Workflow jira [ 40576 ] Default workflow, editable Closed status [ 12801275 ]

          People

          • Assignee:
            Jeremy Boynes
            Reporter:
            Bernt M. Johnsen
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development