How to set default date time as system date time in mysql - mysql

I am trying to set my columns default date time to system datetime.
It shows me an error
Invalid default value for
'InsertionDate'
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
datetime DEFAULT 'Now()' NULL after `PickPointLatLong`

The default value for a column in mysql cannot be the result of a function.
The one exception is the current_timestamp as astander points out.
Your statement should be
alter table `vts`.`tblpickpoint`
add column `InsertionDate` TIMESTAMP
DEFAULT CURRENT_TIMESTAMP

Have a look at CURRENT_TIMESTAMP

If you want to init and update the value on every change, use this:
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
after `PickPointLatLong`
If you only want the creation time, use this:
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
TIMESTAMP DEFAULT CURRENT_TIMESTAMP
after `PickPointLatLong`

Related

Add default value for date column

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

MySQL DATE field with default CURDATE(). NOT DATETIME

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:

Column name is not taking timestamp as default value

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;

Why won't MySQL let me remove attribute "on update CURRENT_TIMESTAMP"?

I have a table with two timestamp fields. I simply defined them with a name and the type TIMESTAMP, yet for some reason MySQL automatically set one of them with a default value and the attribute on update CURRENT_TIMESTAMP. I was planning on having NO default value in either of the fields, but one of the fields is called "date_updated" so I suppose I could set the mentioned attribute to that field.
Unfortunately, it's the field "date_created" that was set with the on update CURRENT_TIMESTAMP attribute, and no matter what I do, MySQL won't let me remove it.
I've tried editing the "date_created" field and removing the attribute. When clicking save, the attribute is back. I have also tried selecting both fields, removing the attribute from one of them and setting it on the other. It gives me the error #1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause and suddenly both attribute columns on the values are set to on update CURRENT_TIMESTAMP the result:
Error
SQL query:
ALTER TABLE `pages` CHANGE `date_created` `date_created` TIMESTAMP NOT NULL ,
CHANGE `date_updated` `date_updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
MySQL said:
#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Must I really recreate both those columns in the correct order to fix this?
I would like to know how I could solve this problem correctly, for future reference.
Thanks
Now I've also tried to run
ALTER TABLE pages
CHANGE date_created
date_created TIMESTAMP NOT NULL
You should specify DEFAULT CURRENT_TIMESTAMP (or DEFAULT 0)
ALTER TABLE pages CHANGE date_created date_created TIMESTAMP NOT NULL DEFAULT 0,
CHANGE `date_updated` `date_updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
As of MySQL version 5.6.6, you can use the explicit_defaults_for_timestamp option in the configuration file, therefore timestamp columns will not have 'DEFAULT CURRENT_TIMESTAMP' or 'ON UPDATE CURRENT_TIMESTAMP' attributes by default. It will also be possible so set these columns to NULL if they are not declared as NOT NULL.
See: http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp

How do I alter a mysql table column defaults?

I have a table with a column of type timestamp which defaults current_timestamp and updates to current_timestamp on every update.
I want to remove the "on update" feature on this column. How do I write the alter statement?
I tried the following:
ALTER TABLE mytable alter column time set DEFAULT now();
but this didn't work.
Pete was almost correct but used the wrong syntax for 'change':
ALTER TABLE mytable CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Notice that you must repeat the column name. Also, make sure you are using backticks instead of single quotes to escape the column name time, which prevents it from being interpreted as the mysql column type of time.
By specifying the DEFAULT of CURRENT_TIMESTAMP, MySQL will no longer automatically update the column. From the MySQL Manual:
With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
You can't AFAIK use functions such as NOW() as a default.
Try
ALTER TABLE `mytable` CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
(Edited to add escaping and second use of field name)