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)
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).
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.
The DATE_ADD() function adds a time/date interval to a date and then returns the date.
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.
"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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With