Informix

Informix

Connect with Db2, Informix, Netezza, open source, and other data experts to gain value from your data, share insights, and solve problems.

 View Only
  • 1.  DEFAULT value for NOT NULL column

    Posted 12 hours ago

    Informix 14.10.FC11W1

    Assume the following table definition:


    create table regional_users (
        employee_num SERIAL,
        region CHAR(3),
        lastname VARCHAR(30),
        firstname VARCHAR(30),
       email VARCHAR(80),
        user_type CHAR(1) DEFAULT 'X' NOT NULL,
    );

    If we try the following INSERT:

    INSERT INTO regional_users

    VALUES (0, "SW1", "Flintstone", "Fred", "fred.flintstone@bedrock.org", NULL); 

    we get error -391 "Cannot insert a null into column (regional_users.user_type)".  

    The manual states "Use the DEFAULT clause in the CREATE TABLE statement to specify the default value for the database server to insert into a column when no explicit value for the column is specified."  I read that as saying that if we do not provide a value, the user_type column would default to "X".  But it looks like since we provided NULL in the VALUES clause, that means that we have provided an explicit value of NULL, which violates the NOT NULL constraint.  Is that what is happening here?

    If so, then under normal circumstances, we could change the INSERT to be:

    INSERT INTO regional_users
        ( employee_number, region, last_name, first_name, email) 
    VALUES (0, "SW1", "Flintstone", "Fred", "fred.flintstone@bedrock.org"); 

    HOWEVER, this assumes that you have control over the format of the INSERT statement.  In this case, our application is using the Informix Perform screen program.  There is no INSERT statement in the Perform application.  Perform handles the INSERT/UPDATE/DELETE logic implicitly.  And the user_type is not a field on the Perform screen (it is handled elsewhere within the system), so we cannot simply put change the Perform logic to enforce the NOT NULL.

    Given that, how do we get the default value of "X".



    ------------------------------
    mark collins
    ------------------------------


  • 2.  RE: DEFAULT value for NOT NULL column

    Posted 11 hours ago
    Don't Insert user_type at all and it will default to X

    Cheers
    Paul

    Paul Watson
    Oninit LLC
    +1-913-387-7529
    www.oninit.com
    Oninit®️ is a registered trademark of Oninit LLC





  • 3.  RE: DEFAULT value for NOT NULL column

    Posted 11 hours ago

    Unfortunately, this is in a Perform screen program, so we do not get to change the INSERT statement to omit the reference to user_type.  Perform handles the INSERT/UPDATE/DELETE statements implicitly.



    ------------------------------
    mark collins
    ------------------------------



  • 4.  RE: DEFAULT value for NOT NULL column

    Posted 11 hours ago

    The answer is to create a VIEW over the table that does not contain that last not null column and have the PERFORM form use that view instead of the base table.

    Art



    ------------------------------
    Art S. Kagel, President and Principal Consultant
    ASK Database Management Corp.
    www.askdbmgt.com
    ------------------------------