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.
Related
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
Im trying to add a new column to my SQL table i want the data type to be TIME and the default value to be CURRENT_TIME. This is my query.
ALTER TABLE tuesday_records ADD cur_time TIME DEFAULT CURRENT_TIME
And this is the error message i get.
Error
SQL query:
ALTER TABLE tuesday_records ADD cur_time TIME DEFAULT CURRENT_TIME
MySQL said: Documentation
#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 'CURRENT_TIME' at line 1
You can do what you want with generated columns:
create table t (
id int auto_increment primary key,
x int,
t timestamp default now(),
tt time generated always as (time(t))
);
That is, add a timestamp column and then extract the time.
Here is a db<>fiddle.
Although this answers your question, I'm not sure if it is the best approach to your overall problem.
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
CREATE TABLE 'test'.'sensor' (
'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
'time' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'value' VARCHAR( 10 ) NOT NULL
);
This is my code which I entered in phpMyAdmin. And when I pressed go I got the following error:
#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 ''test'.'sensor' (
'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
'time' TIMES' at line 1
I have tried changing some statements but couldn't get the error.
MySQL uses backticks to escape identifiers, single and double quotes for strings.
In this case you should do:
CREATE TABLE `test`.`sensor` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`value` VARCHAR( 10 ) NOT NULL
);
It's worth noting that backticks are only strictly required when your name conflicts with a reserved keyword and even then many are only relevant in a specific context. Terms like ORDER or SELECT always need to be escaped, so using them for columns or table names is best avoided. Likewise, TIME is also a column type, so you may want to pick a different name.
Additionally the TIMESTAMP column type is quite limited, values can only exist in the range of 1970 to 2038, so using it is not recommended. The DATETIME type by comparison has a range of years 1000 to 9999, more than adequate for most needs. There's a few other quirks of TIMESTAMP worth keeping in mind, too, like automatic UTC conversion.
Check that which version of php and MySQL u are using i have face this problem but solved after research . this query is working on MySQL 5.5 or newer
and MariaDB 5.5 or newer
CREATE TABLE Ghee(
country_code char(1) NOT NULL default '',
description varchar(10) NOT NULL default '',
PRIMARY KEY (country_code)
)
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.
I'm using MySQL (nobody's perfect), version 4.1 and I'm used to define some timestamp columns like that:
ALTER TABLE foo ADD creation TIMESTAMP DEFAULT NOW() ;
I'd like to do exactly the same thing, but for a DATE field. The reason being I don't need a TIMESTAMP precision and since no functional index exists in MySQL, I cannot access quickly to the rows with a given date (regardless of time of day). So I tried the following but it just does not work:
ALTER TABLE foo ADD creation_date DATE DEFAULT CURDATE() ;
ERROR 1067 (42000): Invalid default value for 'creation_date'
Or even
ALTER TABLE foo ADD creation_date DATE DEFAULT DATE(NOW()) ;
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 '(now())' at line 1
Which does not work either.
Any Ideas?
In MySQL default values have to be constant.
Functions or expressions are not allowed.
The exception ist the TIMESTAMP type, for which CURRENT_TIMESTAMP is a valid non constant default value.
See 4.1 manual: Data Type Default Values