Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: NOW() giving me zeros

I have a table which I want to record the timestamp of every order at every insertion time. However, I'm getting zero values for the timestamps.

Here's my schema:

CREATE TABLE IF NOT EXISTS orders(
            order_no VARCHAR(16) NOT NULL,
            volunteer_id VARCHAR(16) NOT NULL,
            date TIMESTAMP DEFAULT NOW(),
            PRIMARY KEY (order_no),
            FOREIGN KEY (volunteer_id) REFERENCES volunteer(id)
            ON UPDATE CASCADE ON DELETE CASCADE)
like image 373
Fortisimo Avatar asked Mar 29 '10 15:03

Fortisimo


People also ask

What does NOW () return in MySQL?

The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).

What is the data type for now () in MySQL?

NOW() function in MYSQL This function in MySQL is used to check the current date and time value. The return type for NOW() function is either in 'YYYY-MM-DD HH:MM:SS' format or YYYYMMDDHHMMSS. uuuuuu format, depending on whether the function is used in a string or numeric context.

What is Date_add in MySQL?

The DATE_ADD() function adds a time/date interval to a date and then returns the date.

What is Unix_timestamp in MySQL?

UNIX_TIMESTAMP() : This function in MySQL helps to return a Unix timestamp. We can define a Unix timestamp as the number of seconds that have passed since '1970-01-01 00:00:00'UTC. Even if you pass the current date/time or another specified date/time, the function will return a Unix timestamp based on that.


1 Answers

"The DEFAULT value clause in a data type specification indicates 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"

Source: http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html

You are going to have to name the column in your insert query and pass Now() as value.

like image 80
Fabian Avatar answered Oct 20 '22 21:10

Fabian