NULL and zero-length string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zeljko.prince@gmail.com

    #1

    NULL and zero-length string

    This is a copy from
    http://forums.mysql.com/read.php?10,...3797#msg-73797.
    Perhaps someone on this group will know the answer.

    Given the following table:
    CREATE TABLE foo(field VARCHAR(20) NOT NULL);

    The following query *can not* be executed:
    INSERT INTO foo VALUES(NULL);

    That is the behaviour I expected. But why the following query *can* be
    executed:
    UPDATE foo SET field=NULL;

    By executing the UPDATE query, fields in all rows become zero-length
    strings. I would like the error to be thrown instead.

    Is there a way to instruct MySQL, at run-time, to make UPDATE queries
    strict, without implicit conversion?


    I appreciate your answers.
    Ċ½eljko

  • Bill Karwin

    #2
    Re: NULL and zero-length string

    <zeljko.prince@ gmail.com> wrote in message
    news:1141466973 .782118.89830@u 72g2000cwu.goog legroups.com...[color=blue]
    > Is there a way to instruct MySQL, at run-time, to make UPDATE queries
    > strict, without implicit conversion?[/color]

    In MySQL versions prior to 5.0.2, it seems that there is no choice. A
    string column defined as NOT NULL but without an explicit DEFAULT clause
    uses the empty string '' as an implicit default value.

    In MySQL 5.0.2 and later, you can set "strict mode" which has a number of
    effects, but one of which is to disable implicit defaults.

    SET GLOBAL SQL_MODE = 'STRICT_ALL_TAB LES';

    You can also set the sql_mode as a command-line option to mysqld, or in the
    my.cnf configuration file.

    For more information, see:
    http://dev.mysql.com/doc/refman/5.0/...-defaults.html
    http://dev.mysql.com/doc/refman/5.0/...-sql-mode.html

    Regards,
    Bill K.


    Comment

    • zeljko.prince@gmail.com

      #3
      Re: NULL and zero-length string

      > In MySQL versions prior to 5.0.2, it seems that there is no choice. A[color=blue]
      > string column defined as NOT NULL but without an explicit DEFAULT clause
      > uses the empty string '' as an implicit default value.
      >
      > In MySQL 5.0.2 and later, you can set "strict mode" which has a number of
      > effects, but one of which is to disable implicit defaults.
      >
      > SET GLOBAL SQL_MODE = 'STRICT_ALL_TAB LES';
      >
      > You can also set the sql_mode as a command-line option to mysqld, or in the
      > my.cnf configuration file.
      >
      > For more information, see:
      > http://dev.mysql.com/doc/refman/5.0/...-defaults.html
      > http://dev.mysql.com/doc/refman/5.0/...-sql-mode.html[/color]

      I am using MySQL 4.1 and therefore can not set SQL_MODE variable. I
      guess I should find some workaround solution until the server is
      upgraded.

      When I UPDATEd such column, MySQL implicitly converted NULL value to
      zero-length string. (I wish it has thrown an error instead) But when I
      wanted to INSERT that row from one table to another (INSERT INTO...
      SELECT .... FROM ....) error occured. I wish the same behaviour was on
      both, the INSERT and the UPDATE query, and not the way it is. :)

      Thank you for your reply.


      Zeljko

      Comment

      Working...