How to default a MySQL date (timestamp) field to now - mysql

I have tried
alter table t_granja ALTER COLUMN purchase_date TIMESTAMP DEFAULT now();
but I got an error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TIMESTAMP DEFAULT now()' at line 1

Use CURRENT_TIMESTAMP
ALTER TABLE t_granja
ALTER COLUMN purchase_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

Related

Add timestamp column in mysql

SQL Statement:
ALTER TABLE `Trade`.`details`
ADD COLUMN `datetime` TIMESTAMP(3) NOT NULL DEFAULT TIMESTAMP(3) AFTER `Result`
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(3) AFTER Result' at line 2
i am new to mysql any help.Please
timestamp doesn't need length, also you are defining a default value but provided a datetype instead !!
here is what I mean
ALTER TABLE `Trade`.`details`
ADD COLUMN `datetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `Result`

How to alter table to add new date time column that insert date time on each row creation?

I created a table now I want to add date time column but I want to current date time of each row insertion.
I have written syntax like this:
ALTER TABLE particle_photon ADD COLUMN dt_created NOT NULL default CURRENT_TIMESTAMP AFTER humidity;
but I get error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL default CURRENT_TIMESTAMP AFTER humidity' at line 1
You are missing the declaration of the datatype (I assume TIMESTAMP):
ALTER TABLE particle_photon
ADD COLUMN dt_created TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER humidity;
Demo on DB Fiddle:
CREATE TABLE particle_photon(id INT PRIMARY KEY, humidity INT, lastcol INT);
ALTER TABLE particle_photon
ADD COLUMN dt_created TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER humidity;

mySQL alter table on update, current timestamp

alter table `quote`
modify column `timestamp`
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP
NOT NULL
What's wrong with the above mysql query?
I am trying to change my timestamp column to default and update with the current timestamp.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL' at line 1
After MODIFY COLUMN col_name the syntax requires a column_definition which in turn requires a type. Add the current type of this column (e.g. DATETIME) before DEFAULT to resolve the syntax error.
This is not so much an answer as it is a tip. In MySQL workbench 6.3 community build, if you want to modify the column using the table edit screen, make certain the "Data Type:" for your timestamp column is for sure set to TIMESTAMP and then make ON UPDATE CURRENT_TIMESTAMP the default value (on my screen, it is just below the "Data Type:").

How to alter an existing column to default to the current time on insert, in MySQL?

I'm trying to change the existing column "time_sent" to default to the time during insertion. My SQL is:
ALTER TABLE `email_history` alter `time_sent` set DEFAULT CURRENT_TIMESTAMP
I'm getting this error though:
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_TIMESTAMP' at line 1
I've read the documentation and other (similar, but not identical) examples, and no luck.
My version of MySQL is 5.0.67 I believe.
It should be ..
ALTER TABLE 'table' MODIFY collumn_1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

Not able to add a timestamp column to end of a sql table

I tried to add a column at end of the table assestbl
ALTER TABLE `assestbl` ADD `timestamp` VARCHAR NOT NULL DEFAULT CURRENT_TIMESTAMP
but its showing an error
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to
use near 'NOT NULL DEFAULT CURRENT_TIMESTAMP' at line 1
You have two errors:
1) syntax error in datatype, varchar needs defined length: VARCHAR(LEN)
once you fix that you get something like invalid default value for 'timestamp':
2) DEFAULT CURRENT_TIMESTAMP can only be applied to temporal datatypes (DATE,TIME,DATETIME,TIMESTAMP and YEAR).