Default date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    #1

    Default date

    Hello all

    I have a field (called active) of type DATE and I want to add a default
    value. The default value must be the current date. Is this possible to do
    this in the table create statement?

    CREATE TABLE mytable
    (
    id INTEGER NOT NULL AUTO_INCREMENT,
    active DATE DEFAULT ?????,
    PRIMARY KEY (id),
    )

    Thanks in advance
    Roger


  • Jonathan

    #2
    Re: Default date

    Rob wrote:[color=blue]
    > Hello all
    >
    > I have a field (called active) of type DATE and I want to add a default
    > value. The default value must be the current date. Is this possible to do
    > this in the table create statement?
    >
    > CREATE TABLE mytable
    > (
    > id INTEGER NOT NULL AUTO_INCREMENT,
    > active DATE DEFAULT ?????,
    > PRIMARY KEY (id),
    > )
    >
    > Thanks in advance
    > Roger
    >
    >[/color]
    Why not vonvert the column to a timestamp? This way it will always hold
    the current date (and time) if you modify the record.

    Jonathan

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Bill Karwin

      #3
      Re: Default date

      Rob wrote:[color=blue]
      > I have a field (called active) of type DATE and I want to add a default
      > value. The default value must be the current date. Is this possible to do
      > this in the table create statement?[/color]

      See http://dev.mysql.com/doc/mysql/en/create-table.html:

      "The DEFAULT clause specifies a default value for a column. With one
      exception, the default value must be a constant; it cannot be a function
      or an expression. This means, for example, that you cannot set the
      default for a date column to be the value of a function such as NOW() or
      CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTA MP as
      the default for a TIMESTAMP column as of MySQL 4.1.2. See Section
      11.3.1.2, TIMESTAMP Properties as of MySQL 4.1."

      So this means that you can't do what you describe on a field with a DATE
      datatype. You must use TIMESTAMP as the datatype.

      Regards,
      Bill K.

      Comment

      Working...