It is possible to set default value on DATE (NOT DATETIME) column in MySQL 5.7 to current date?
I try this (generated by Workbench):
ALTER TABLE `db`.`table` CHANGE COLUMN `column` `column` DATE NOT NULL DEFAULT CURDATE() ;
but not works for me.
(no data in table)
No, you cannot. The documentation is pretty clear on this:
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 TIMESTAMP and DATETIME columns. See Section 12.3.5, “Automatic
Initialization and Updating for TIMESTAMP and DATETIME”.
You can do one of the following:
Set up a column with a default value for the DATETIME. Create view that extracts the date as a separate column.
Create an insert trigger to set the date column.
There is a way you can do this if you have another column that has a for example a datetime field with a default of NOW(). See this post:
Related
I want to make a column in my table that has the equivalent functionality as the oracle sysdate variable as the default value. So when a row is inserted (without the date, the current sysdate is used). what is the query to create this table?
Also should I allow nulls for this column, how would that work?
Also, I want to be able to insert a row into this table, such that the functionality occurs. Please provide sample insert query that uses this functionality.
Also, I want to be able to do the same for the DATETIME column type.
Thanks in advance.
This is pretty well explained in the documentation:
TIMESTAMP and DATETIME columns can be automatically initializated and
updated to the current date and time (that is, the current timestamp).
The syntax looks like:
create table t (
. . .,
createdAt datetime not null default current_timestamp
);
If you are giving the column a default value, then you might as well declare it as NOT NULL.
You can also do the same thing for updates:
updatedAt datetime not null default current_timestamp on update current_timestamp
I am trying to add an default value for my date column. The default value should be GETDATE(). I want to do this so I can see the date of the created row. When I try to save it I get the warning: Incorrect default value for date.
Does someone know why I am getting the warning and how I can fix it?
Run this query
ALTER TABLE `YourDb.yourtable`
CHANGE COLUMN `date` `date` DATETIME DEFAULT CURRENT_TIMESTAMP;
The Change column is oldval newval and the type
make sure if your column name isnt date to change it appropriately
additionally if you want that timestamp to update when the row is modified use
CHANGE COLUMN `date` `date` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
NOTE: The question is about DATE type, not Datetime nor Timestamp
How to alter column of date data type to use current date by default?
I saw a lot of examples for datetime (with time part), but not for date. I have tried:
ALTER TABLE `accounting` ALTER `accounting_date`
SET DEFAULT CURRENT_DATE;
ALTER TABLE `accounting` CHANGE `accounting_date`
`accounting_date` DATE NOT NULL DEFAULT CURRENT_DATE;
I also tried with CURDATE(), NOW(), CURRENT_DATE() ...
MySQL 8.0+:
CREATE TABLE foo (
`creation_time` DATE DEFAULT (DATE_FORMAT(NOW(), '%Y-%m-%d'))
)
I use version 8.0.26, this is working:
datecolumn date DEFAULT (CURDATE())
It does not work, if you don't use the brackets!
Probably you cannot set default value for 'date' data type in mysql. You need to change the type to timestamp or datetime.
You may have a look at this similar question.
Invalid default value for 'Date'
EDIT:
In version 5.6.5, it is possible to set a default value on a datetime column, and even make a column that will update when the row is updated. The type definition:
CREATE TABLE foo (
`creation_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
`modification_time` DATETIME ON UPDATE CURRENT_TIMESTAMP
)
Reference: http://optimize-this.blogspot.com/2012/04/datetime-default-now-finally-available.html
As noted in this question Invalid default value for 'create_date' timestamp field, this error may happen when MySQL is in strict mode (which is default behavior, I believe).
If you want to override it, just disable all these checks when creating your table:
SET SQL_MODE='ALLOW_INVALID_DATES';
The warning will be still generated, however it will allow to create the table.
It seems to work in sqlite:
"date" DATE NOT NULL DEFAULT (DATE(CURRENT_TIMESTAMP))
No, you cannot. The documentation is pretty clear on this:
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 TIMESTAMP and DATETIME columns.
I have two columns created_at and updated_at in same table. Both the columns have data type as datetime. Now when I am trying to modify the column data types as follows -
alter table nodestatics modify column updated_at datetime default current_timestamp;
alter table nodestatics modify column created_at datetime default current_timestamp;
It is showing the following error
Error Code : 1067 Invalid default value for 'updated_at' (0 ms taken)
Error Code : 1067 Invalid default value for 'created_at' (0 ms taken)
My mysql version is 5.5.41-0ubuntu0.14.04.1 (Ubuntu)
It is hard to reference documentation in a comment:
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table.
Either upgrade to 5.6.5. Or use TIMESTAMP instead of DATETIME.
Use TIMESTAMP instead of DATETIME
Should be something like this:
ALTER TABLE nodestatics MODIFY COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE nodestatics MODIFY COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Or you can use TRIGGERS.
CREATE TRIGGER myTable_OnInsert BEFORE INSERT ON `tblMyTable`
FOR EACH ROW SET NEW.dateAdded = NOW();
You can look at similar topic. Happy coding!
Try this
alter table nodestatics modify column created_at timestamp default current_timestamp;
Is it possible to define a timestamp column in a MySQL table that will automatically be updated every time a field in the same row is modified? Ideally this column should initially be set to the time a row was inserted.
Cheers,
Don
You can use the timestamp column as other posters mentioned. Here is the SQL you can use to add the column in:
ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
This adds a column called 'lastUpdated' with a default value of the current date/time. When that record is updated (lets say 5 minutes later) that timestamp will automatically update to the current time.
That is the default functionality of the timestamp column type. However, note that the format of this type is yyyymmddhhmmss (all digits, no colons or other separation).
EDIT: The above comment about the format is only true for versions of MySQL < 4.1... Later versions format it like a DateTime
This is what I have observed (MySql 5.7.11) -
The first TIMESTAMP column in the table gets current timestamp as the default value. So, if you do an INSERT or UPDATE without supplying a value, the column will get the current timestamp.
Any subsequent TIMESTAMP columns should have a default value explicitly defined. If you have two TIMESTAMP columns and if you don't specify a default value for the second column, you will get this error while trying to create the table -
ERROR 1067 (42000): Invalid default value for 'COLUMN_NAME'
A MySQL timestamp is set with creation or update time only if their default value is set as it. ALTER TABLE some_table ADD when TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
Otherwise it works just like a DateTime field, only that it's relative to 1970/01/01 UTC, so it's an absolute point in time not depending on a specific timezone as is DateTime.