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
Related
Trying to import table from backup to another server and getting error in SQL syntax
CREATE TABLE IF NOT EXISTS `book` (
`bid` bigint(20) NOT NULL AUTO_INCREMENT,
`bookdate` datetime NOT NULL,
`create_date` datetime NOT NULL,
`bookdate_date` date GENERATED ALWAYS AS (cast(`bookdate` as date)) VIRTUAL,
`create_date_date` date GENERATED ALWAYS AS (cast(`create_date` as date)) VIRTUAL,
PRIMARY KEY (`bid`),
KEY `IDX_book_bookdate_date` (`bookdate_date`),
KEY `IDX_book_create_date_date` (`create_date_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=316;
phpMyAminError
#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 'GENERATED ALWAYS AS (cast(`bookdate` as date)) VIRTUAL,
`create_date_date` da' at line 5
How do i fix import error? I dont know what this means its beyond my paygrade
`bookdate_date` date GENERATED ALWAYS AS (cast(`bookdate` as date)) VIRTUAL,
`create_date_date` date GENERATED ALWAYS AS (cast(`create_date` as date)) VIRTUAL,
Those are generated columns. They are set automatically, so you cannot assign values in an insert or update. These were introduced in some version of MySQL 5.7, so older versions do not support them. You can replace the idea using views, for similar functionality.
They are calculated -- using the expression -- when the table is queried. This is convenient, because the value is always correct. The VIRTUAL means that the column value is not stored in the table.
The alternative to VIRTUAL is STORED. That calculates the value once when the row is inserted or updated. It uses up storage space but can be convenient if the calculation is expensive.
EDIT:
Based on your comment, you have a problem. One method is to create a view but that can be tricky. Assuming that existing code is referencing the columns, your best best is to define the generated columns as regular columns and use triggers (insert and update) to set them.
This question already has an answer here:
Create table fail in mysql when using CURDATE() as default
(1 answer)
Closed 7 years ago.
Trying to insert new columns into my blog posts table as part of my sites update.
Here is the mysql code I am using:
ALTER TABLE blogposts
ADD (
postType varchar(20),
postDate datetime NOT NULL DEFAULT CURDATE(),
postTime datetime NOT NULL DEFAULT CURTIME()
);
It throws this error though when executed:
#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 'CURDATE(),
postTime datetime NOT NULL DEFAULT CURTIME()
)' at line 4
I assume that curdate() is the issue, but do not understand why since it is a valid mysql function.
This is not a duplicate of this as was proposed., the solution in that post did not fix my problem and instead threw this error:
#1067 - Invalid default value for 'postDate'
When using code:
ALTER TABLE blogposts
ADD (
postType varchar(20),
postDate datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
postTime datetime NOT NULL DEFAULT CURTIME()
);
So for reference, this is not a duplicate question it seems.
Default values for DATETIME columns are not available before MySQL Server 5.6.5.
In 5.6.5 and later, the value you specify as default must be CURRENT_TIMESTAMP or an equivalent expression returning a datetime value.
https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
Because I want them separated out by default for easier calling from the backend.
That seems like a little bit of a frivolous motivation, when you can SELECT TIME(c1) AS teh_time, DATE(c1) AS teh_date if needed, and almost any other operation you wanted to do, like datetime math, time zone conversions or DATE_FORMAT() would require them to be recombined.
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), .
I have 2 Windows servers running MySQL, one of them running version 5.1 and the other running version 5.7. I am trying trying to copy a database from the MySQL 5.7 over to the 5.1 on the other server and believe it's the difference in versions (new syntax in 5.7?) that is causing this error, but I could be wrong.
After Exporting through phpMyAdmin the database I in the 5.7 version and trying to Import in the 5.1 version I'm getting the error
MySQL said: Documentation #1067 - Invalid default value for
'postdate'
on the command
CREATE TABLE IF NOT EXISTS `jobs` (
`id` mediumint( 9 ) NOT NULL ,
`title` varchar( 200 ) DEFAULT NULL ,
`descr` varchar( 5000 ) DEFAULT NULL ,
`postdate` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB AUTO_INCREMENT =5 DEFAULT CHARSET = utf8mb4;
Any idea why?
In 5.1, the default value has to be a constant value (e.g. NULL is acceptable) except for the timestamp type where current_timestamp is allowed. I.e., for a date or datetime you cannot use current_date, now or current_timestamp.
So you either stick to the datetime type for your postdate column and you have to give up current_timestamp as a default value (you can maybe set up a trigger for the purpose, see examples here), or - depending on your requirements - consider using timestamp (which has a different range of values).
The corresponding section of the manual says:
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. The exception is that you can specify
CURRENT_TIMESTAMP as the default for a TIMESTAMP column.
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.