Error #1064 adding MySql datetime default value with fractional seconds - mysql

I have table:
CREATE TABLE t1 (
id INT(3)
, datetime DATETIME(6)
);
I want to add default value to datetime column with fractional seconds:
ALTER TABLE 't1'
CHANGE 'datetime' 'datetime'
DATETIME DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
I get error message #1064:
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 ''t1' CHANGE 'dt' 'dt' DATETIME DEFAULT CURRENT_TIMESTAMP(6) ON
UPDATE CURRENT_TI' at line 1

You need to use.
Without the qoutes around the table name and column name.
I've used backticks around datetime because datetime is a keyword in MySQL.
And you have forgotten that DATETIME needs to have that fractional seconds defined like DATETIME(6)
ALTER TABLE t1
CHANGE `datetime` `datetime`
DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)

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;

How to change field name to default NULL

I want to change my field name to default Null but i am unable to solve this issue Here i am trying to change my deleteAt field to defaut Null but its giving me the error
Syntax error or access violation: 1064 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
'deletedAt timestamp default NULL' at
line 1
Here is my code
DB::statement('ALTER TABLE insurance CHANGE COLUMN deleted_at deletedAt timestamp default NULL');
I want to set default NULL but i am unable to solve this and i am confused about this issue. your help need here
Try this
ALTER TABLE `insurance` CHANGE COLUMN `deleted_at`
`deleted_at` TIMESTAMP NULL DEFAULT NULL;
ALTER TABLE insurance CHANGE COLUMN deleted_at deletedAt timestamp default NULL
Is incorrect.
Use this please.
ALTER TABLE insurance MODIFY COLUMN deleted_at timestamp NULL DEFAULT NULL
The word deletedAt isn't a valid syntax. There is an error in your query.

Timestamp default value different than current_timestamp

I' m trying to add a timestamp column to this table:
CREATE TABLE `task` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`timecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`session` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Then I try to execute this code to add another timestamp column with a different default value:
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP)
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 'TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP) AFTER `timecreated`,'
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL 15 MINUTE) AFTER `timecreated`'
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 '(CURRENT_TIMESTAMP + INTERVAL 15 MINUTE) AFTER `timecreated`,
but non of theese works.
You cannot have two timestamp columns with default values that use CURRENT_TIMESTAMP, however you can use a trigger before insert:
ALTER TABLE task
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00';
DELIMITER //
CREATE TRIGGER task_timeexpiration_default BEFORE INSERT ON task FOR EACH ROW
BEGIN
SET NEW.`timeexpiration` = TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP);
END;//
MySQL versions before 5.6.1 would not let two TIMESTAMP columns in the same table, unless as you rightly noted with out defaults and allowing null.
MySQL 5.6.+ allows two or more TIMESTAMP columns in a table.
How Aman said, it is not possible to use two Timestamp in same table. You could use DATETIME instead. Try something like this,
ALTER TABLE task ADD COLUMN `timeexpiration` DATETIME DEFAULT NULL AFTER timecreated;
Maybe this help.
Hint: you have to remove , end of last field after PRIMARY KEY (id), .

Mysql Alter glitch

Hey guys I was trying to alter my tables column to take
the current time stamp on creation, the error I'm getting
is #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 '( 'inspection_number' NOT NULL default CURRENT_TIMESTAMP )' at line 1
I was trying to use
ALTER TABLE `reports` (
`inspection_number` DATE NOT NULL default CURRENT_DATE
);
But I"m not seeing the error?
Seems syntax is not correct. Use below :
if adding new column inspection_number:
ALTER TABLE `reports` ADD COLUMN `inspection_number` timestamp NOT NULL default CURRENT_TIMESTAMP
if modifying existing inspection_number column:
ALTER TABLE `reports` MODIFY COLUMN `inspection_number` timestamp NOT NULL default CURRENT_TIMESTAMP
Please specify datatype of column
You're missing a MODIFY
ALTER TABLE `reports` (
MODIFY `inspection_number` NOT NULL default CURRENT_TIMESTAMP
);
ALTER TABLE `reports`
MODIFY `inspection_number` TIMESTAMP NOT NULL default CURRENT_TIMESTAMP ;
The syntax you have there is for when you create a table. When you modify a table and want to set a default value use:
ALTER TABLE table_name MODIFY col_name col_type NOT NULL DEFAULT CURRENT_TIMESTAMP;