Timestamp default value different than current_timestamp - mysql

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), .

Related

MySQL (or Mariadb) Expression with DATEDIFF and DATETIME() OR NOW()

What is wrong here?
CREATE TABLE `actionAngebot` (
`createdAt` DATETIME NOT NULL,
`expiryDat` SMALLINT DEFAULT UNSIGNED AS (DATEDIFF(`createdAt`, DATETIME())) STORED
)
ENGINE=MyISAM;
SQL 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 '() )) STORED ) ENGINE=MyISAM' at line 3 *
please do not propose me this :
CREATE TABLE `promos` (
`createdAt` DATETIME NOT NULL,
`createdTil` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`expiryDateOfReward` SMALLINT(10) AS ((to_days(`createdAt`) - to_days(`createdTil`))) VIRTUAL
)
ENGINE=MyISAM;
I want to make it with 2 columns.
The problem with your current query is that DATETIME is a data type, not a function, so you can't call it as one. DATETIME is not valid in that position either, you would need to use NOW() or CURDATE() dependent on whether you wanted a difference to the date and time or just date.
But
You can't use expressions for generated columns which include functions based on system date and time, so you cannot do what you want (see the manual). You could consider creating a view instead:
CREATE VIEW aabot AS
SELECT *, DATEDIFF(`createdAt`, NOW()) AS expiryDat
FROM actionAngebot

SQL Syntax error when using sysdate

so i'm having an issue using sysdate function as my default data value in MySQL. my code to create the table is as follows:
CREATE TABLE orders
(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
odr_date DATE DEFAULT sysdate() NOT NULL
);
i get the error
[42000][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 'sysdate()
)' at line 4
instead of sysdate() try CURRENT_TIMESTAMP for mysql
reference: Type date default sysdate in Mysql
CREATE TABLE orders
(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
odr_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
);

Error #1064 adding MySql datetime default value with fractional seconds

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)

MySQL syntax error in the CREATE TABLE statement

SQL query:
CREATE TABLE `comment_threads` (
`comment_id` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0',
`updated` TIMESTAMP NOT NULL ,
`timestamp` TIMESTAMP NOT NULL ,
) ENGINE = MYISAM ;
This is an old file that I'm trying to run on HostGator through phpMyAdmin. I get an error that says:
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 ') ENGINE=MyISAM' at line 5
UPDATE:
If I change the statement to this, I still get an error:
CREATE TABLE comment_threads (
comment_id INT( 11 ) UNSIGNED NOT NULL DEFAULT '0',
updated TIMESTAMP( 14 ) NOT NULL ,
timestamp TIMESTAMP NOT NULL
PRIMARY KEY ( comment_id ) )
ENGINE = MYISAM ;
I get the error:
ERROR 1064 (42000): 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 '( 14 ) NOT NULL ,
timestamp TIMESTAMP NOT NULL
PRIMARY KEY ( comment_id ) )
ENGI' at line 3
Your MySQL query is incorrect. Correcting it to this works.
CREATE TABLE `comment_threads` (
`comment_id` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`updated` TIMESTAMP NOT NULL ,
`timestamp` TIMESTAMP NOT NULL
) ENGINE=MyISAM;
To run this in phpMyAdmin, you can use the in-built table creator or enter the corrected SQL statement in the SQL query window.
Note that I removed the comma after the last TIMESTAMP NOT NULL line (immediately before the ending ).
UPDATE:
The second statement you posted corrects to this:
CREATE TABLE `comment_threads` (
`comment_id` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`updated` TIMESTAMP NOT NULL ,
`timestamp` TIMESTAMP NOT NULL,
PRIMARY KEY(`comment_id`)
) ENGINE=MyISAM;
Here are the problems you introduced in your second CREATE TABLE statement:
TIMESTAMP( 14 ) should just be TIMESTAMP (per the documentation)
You need a comma after the line TIMESTAMP NOT NULL line. The comma is necessary now because unlike in the first example, you're separated two parts of the statement: the TIMESTAMP NOT NULL line and the PRIMARY KEY declaration.
If you want more information on simple methods for debugging SQL statements, I strongly suggest you look at my answer to this question (see the section titled A bit more information on how to methodically fix errors like this).
Make sure that when you change a CREATE TABLE statement, or any piece of code, that you make changes in small enough increments that you're only "breaking at most one thing at a time." In the case of your second CREATE TABLE statement, there was no reason to change the first TIMESTAMP declaration, and doing so broke the code. That line was working; no need to change it.

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;