Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-30440

Invalid default value on DEFAULT NULL TIMESTAMP

Details

    Description

      On mariadb 10.5.4 it seen not possible to create a TIMESTAMP column with null default value.

      For exemple :
      CREATE TABLE Test (
      test TIMESTAMP DEFAULT NULL
      );

      Return :
      Error Code: 1067
      Invalid default value for 'test'

      Attachments

        Activity

          alice Alice Sherepa added a comment -

          MariaDB [test]> CREATE TABLE Test1 ( test TIMESTAMP DEFAULT NULL );
          ERROR 1067 (42000): Invalid default value for 'test'
           
          MariaDB [test]> CREATE TABLE Test2 ( test TIMESTAMP  NULL DEFAULT NULL);
          Query OK, 0 rows affected (0,054 sec)
          

          It works as expected. KB says: "MariaDB also has special behavior if NULL is assigned to column that uses the TIMESTAMP data type. If the column is assigned the NULL value in an INSERT or UPDATE query, then MariaDB will automatically initialize the column's value with the current date and time.

          This automatic initialization for NULL values can also be explicitly disabled for a column that uses the TIMESTAMP data type by specifying the NULL attribute for the column. In this case, if the column's value is set to NULL, then the column's value will actually be set to NULL." ( https://mariadb.com/kb/en/timestamp/)

          So - first set NULL attribute to timestamp column - then set the default (NULL or something else if needed, CURRENT_TIMESTAMP e.g.)

          alice Alice Sherepa added a comment - MariaDB [test]> CREATE TABLE Test1 ( test TIMESTAMP DEFAULT NULL ); ERROR 1067 (42000): Invalid default value for 'test'   MariaDB [test]> CREATE TABLE Test2 ( test TIMESTAMP NULL DEFAULT NULL); Query OK, 0 rows affected (0,054 sec) It works as expected. KB says: "MariaDB also has special behavior if NULL is assigned to column that uses the TIMESTAMP data type. If the column is assigned the NULL value in an INSERT or UPDATE query, then MariaDB will automatically initialize the column's value with the current date and time. This automatic initialization for NULL values can also be explicitly disabled for a column that uses the TIMESTAMP data type by specifying the NULL attribute for the column. In this case, if the column's value is set to NULL, then the column's value will actually be set to NULL." ( https://mariadb.com/kb/en/timestamp/ ) So - first set NULL attribute to timestamp column - then set the default (NULL or something else if needed, CURRENT_TIMESTAMP e.g.)

          People

            Unassigned Unassigned
            fmartin35 Florent
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.