Altering Mysql column default value to CURRENT_DATE gives error? - mysql

ALTER TABLE `mysystem`.`projects`
MODIFY COLUMN `project_capture_date` DATE NOT NULL DEFAULT CURRENT_DATE();
gives:
Error Code: 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_DATE()'
at line 2
Current row definition:
project_capture_date, date, NO, , 0000-00-00
Just recently changed the engine to InnoDB from MyISAM.

As per the mysql document
http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
The DEFAULT value clause in a data type specification indicates a
default value for a column. 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
To solve this you may need to define the datatype as TIMESTAMP column with DEFAULT CURRENT_TIMESTAMP
DEMO
If you do not want TIMESTAMP then you have do while inserting by setting the column value as Now()

Related

Using date function for SQL

I am having issues using the date functions in sql workbench.
CREATE TABLE exampletable (
exampledate DATE DEFAULT CURDATE() not null,
);
I've tried CURDATE(), NOW(), CURRENT_DATE, but it keeps saying you have an error in your sql syntax and underlines whatever date function I use. Any ideas on what my issue is?
According to MySQL doc at http://dev.mysql.com/doc/refman/5.6/en/create-table.html:
The DEFAULT clause specifies a default value for a column. 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 or (as of MySQL
5.6.5) DATETIME column. See Section 11.3.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.

Cannot use functions and variables in DEFAULT constraint when creating a new database table

I'm trying to create a new database using the CREATE command. Here's the SQL:
CREATE TABLE test(
rok_utworzenia timestamp DEFAULT CURRENT_DATE
);
But it throws an error saying
'#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_DATE )' at line 2'
It works if I change the DEFAULT constraint to a constant, but not if I try to use any function or variable. Why is that so?
Use this:
CREATE TABLE test( rok_utworzenia timestamp DEFAULT CURRENT_TIMESTAMP );
From MySQL site:
The DEFAULT value clause in a data type specification indicates a
default value for a column. 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.
Try this CURRENT_TIMESTAMP:
CREATE TABLE test( rok_utworzenia timestamp DEFAULT CURRENT_TIMESTAMP );

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:").

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

Define default date value in MySQL, similar to timestamp

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