Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL CURRENT_TIMESTAMP as DEFAULT

Tags:

mysql

While creating a table I am getting the following error:

ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause 

The problem is that I don't actually have two columns TIMESTAMP with CURRENT_TIMESTAMP as default, neither I am using ON UPDATE clause.

The DDL query I'm trying to execute is

CREATE TABLE user(     /* Basic Information */     id INT NOT NULL AUTO_INCREMENT,     firstname VARCHAR(255) NOT NULL,     surname VARCHAR(255) NOT NULL,     email VARCHAR(255) NOT NULL UNIQUE,     username VARCHAR(255) NOT NULL UNIQUE,     password CHAR(40) NOT NULL,      /* System status information */     active BOOL NOT NULL DEFAULT FALSE,     validated BOOL NOT NULL DEFAULT FALSE,     date_validated TIMESTAMP,     date_registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,      /* Index */     PRIMARY KEY (id) ) Engine=InnoDB; 

What's causing the issue?

like image 604
Bart Platak Avatar asked Jul 09 '12 17:07

Bart Platak


People also ask

How do I add a default value to a TIMESTAMP column?

Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value; for example, DEFAULT 0 or DEFAULT '2000-01-01 00:00:00'.

What is the default value for date in MySQL?

MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'. even if they also say: Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').

What is the difference between now () and Current_timestamp?

NOW() returns a constant time that indicates the time at which the statement began to execute. NOW() returns the time at which the function or triggering statement began to execute, but SYSDATE() returns the exact time at which it executes. And CURRENT_TIMESTAMP , CURRENT_TIMESTAMP() are synonyms for NOW() .

Is TIMESTAMP or datetime better?

Timestamps in MySQL are generally used to track changes to records, and are often updated every time the record is changed. If you want to store a specific value you should use a datetime field.


1 Answers

You can use two timestamp in one table. For default, use DEFAULT field first and then the rest timestamp fields.

Below query should work.

CREATE TABLE myTable  (  id INT,  date_registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,   date_validated TIMESTAMP ); 

Demo at sqlfiddle

like image 172
Fahim Parkar Avatar answered Sep 25 '22 17:09

Fahim Parkar