How to alter column to set default CURRENT_TIMESTAMP in MySQL [duplicate] - mysql

This question already has answers here:
CREATE table of date type and use its default value as CURRENT_DATE on MySQL
(3 answers)
Closed 2 years ago.
I have created a table later I want to add a default value for the column as CURRENT_TIMESTAMP
CREATE TABLE `orders` (
`order_id` int(11) NOT NULL,
`order_user_id` int(11) DEFAULT NULL,
`order_date` date DEFAULT NULL, -- this column...
`order_status` varchar(25) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
my alter query is like
ALTER TABLE `orders` CHANGE `order_date` `order_date` DATE NOT NULL DEFAULT CURRENT_TIMESTAMP;
and I got error like
Query error:
#1067 - Invalid default value for 'order_date'

Please use this syntax:
ALTER TABLE `table`
MODIFY column_1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
In your case the statement would be as below,
ALTER TABLE `orders`
MODIFY `order_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

Your column needs to be of type DATETIME or TIMESTAMP. Docs. Aside from changing the column type, which you may not want to do, you could write an INSERT/UPDATE trigger.

Related

MySql table have two or more columns have default date vaule CURRENT_TIMESTAMP,any Solutions?

if i have a table with two columns create_time and update_time,the data type is timestamp,then have default value CURRENT_TIMESTAMP,the sql code of created table is:
CREATE TABLE `t_activity` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`STARTDATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENDDATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
);
but it prompt error:1293,there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or UPDATE clause.
I am not sure what you're trying to accomplish having both the STARTDATE and ENDDATE populated with the CURRENT_TIMESTAMP. However to fix your code try changing your data types to DATETIME like this:
**CREATE TABLE `t_activity` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`STARTDATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENDDATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
);**
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html
This limitation in Mysql:
Previously, at most one TIMESTAMP column per table could be
automatically initialized or updated to the current date and time.
This restriction has been lifted. Any TIMESTAMP column definition can
have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used
with DATETIME column definitions. For more information, see Automatic
Initialization and Updating for TIMESTAMP and DATETIME.
Check This for more detail
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html

how to write create table date query in phpmyadmin which store by default current date if date not given?

I am trying to create a table (phpMyAdmin) by using the following query:
CREATE TABLE login_detail(
Id int(11) primary key auto_increment,
userName varchar(100) not null,
userPassword varchar(100) not null,
created_at Date DEFAULT CURRENT_DATE
);
but it showing error at CURRENT_DATE. Can anyone solve this problem?
Its not supported.
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 column
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
you just use current time stamp .below code i tested in phpmyadmin.it working fine
CREATE TABLE login_detail(Id int(11) primary key auto_increment, userName varchar(100) not null,userPassword varchar(100) not null, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Try the following:
CREATE TABLE IF NOT EXISTS `login_detail` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(100) NOT NULL,
`userPassword` varchar(100) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
INSERT INTO `login_detail` (`Id`, `userName`, `userPassword`) VALUES
(1, 'aaa', 'bbb'),
(2, 'aaa', 'bbb');
To get the date and-or time in the format you want use DATE_FORMAT
SELECT `Id`, `userName`, `userPassword`, DATE_FORMAT(`created_at`, '%e %b %Y') AS `created_at_date`, DATE_FORMAT(`created_at`, '%H:%i:%s') AS `created_at_time` FROM `login_detail`;
http://sqlfiddle.com/#!9/ea673/1

Invalid default value for 'timestamp'

i am getting error in my database. i am encountering invalid default value for timestamp.
here's my database:
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`text` varchar(10000) NOT NULL,
`threadId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`color` varchar(10) DEFAULT '#00bcd4',
`icon` varchar(100) NOT NULL DEFAULT 'https://mymonas.com/forum/category_icon/ic_question.png'
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
I was having the same problem, I changed type from "datetime" to "timestamp" and It worked. I have mysql 5.5.52.
Mysql_error
I have the same issue in sql_mode.
Make query:
show variables like 'sql_mode' ;
You need to remove the "NO_ZERO_IN_DATE,NO_ZERO_DATE" from sql_mode.
SET sql_mode = '';
Use CURRENT_TIMESTAMP() instead CURRENT_TIMESTAMP
i.e.
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`text` varchar(10000) NOT NULL,
`threadId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
`isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;
Now() works as well
From the MySQL 5.5 manual:
"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."
The changes in MYSQL 5.6.x that allow the functionality are documented here:
"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."
So, this means you are using an older version of mysql, either you can use datetime data type of upgrade your mysql version
Answered by #max Sherbakov worked but I think its risky,
if you execute SET sql_mode = ''; query.
Because if you or other users SET any different variables in sql_mode
like NO_ENGINE_SUBSTITUTION check other SQL MODES
by changing sql_mode values in my.ini file
OR
using SET sql_mode = 'YOUR_VARIABLE_LIST'; query
it worked for you current situation
but create problem in other projects.
To view current sql mode use following query
show variables like 'sql_mode' ;

How can I add 24 hours in CURRENT_TIMESTAMP MySQL?

I want to add expire TIMESTAMP in DB table. I have one field timestamp with type TIMESTAMP and default value CURRENT_TIMESTAMP(). I adding new field "expire" with type DATETIME with default value ADDDATE(CURRENT_TIMESTAMP, INRERVAL 1 DAY), but not working.
CREATE TABLE IF NOT EXISTS `temp_mch` (
`name` varchar(60) NOT NULL,
`qty_new` int(5) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`name`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Where I'm wrong ?
Thanks in advance !
That is not supported in the column default definition.
You can use a trigger to do it instead.
See this question for details:
Expire date as default value for TIMESTAMP column

MySQL unknown column error when using ALTER, don't understand behaviour

I was wondering if someone could help me.
I have a odd behaviour while issueing a ALTER command. The command comes from MySQL Workbench sync and it is failing. I have a table with fields:
`id` int(11) NOT NULL AUTO_INCREMENT ,
`text` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL ,
`updated` datetime NULL DEFAULT NULL ,
`remote_addr` varchar(45) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
`http_user_agent` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
`user_id` int(11) NULL DEFAULT NULL ,
`category` varchar(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
`created` datetime NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
And I want to issue the ALTER command:
ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`
I get in response:
Unknown column 'created' in 'logs'
But
ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`
works by itself, and:
ALTER TABLE `logs`
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`
also works by itself.
I don't understand why when both are combined in one query it doesn't work and says that 'created' doesn't exist. I know that it definately exists.
Note that I'm not worried about the change column for 'created', it is generated by MWB when comparing and preparing to sync. But was just wondering why both actions can't be put on one query.
I am using MySQL 5.5.8
Update
I actually can do multiple clauses okay. I have been doing it on other tables just fine.
I forgot to mention this. But when I remove the AFTER part it works.
So this does not work:
ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`
But this does:
ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`
I had the same problem. I solved it by doing the CHANGE COLUMN (or MODIFY COLUMN) before ADD COLUMN.
In your example that would give the following SQL statement :
ALTER TABLE `logs`
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`,
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`;
This appears to be a bug:
http://bugs.mysql.com/bug.php?id=60650
I submitted this question as an example.
From the Documentation
You can issue multiple ADD, ALTER, DROP, and CHANGE clauses in a single ALTER TABLE statement, separated by commas. This is a MySQL extension to standard SQL, which permits only one of each clause per ALTER TABLE statement. For example, to drop multiple columns in a single statement, do this:
ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;
So you are only allowed to issue
ALTER TABLE t CHANGE ..., CHANGE ...
Not a combination of different modification statements.
Don't know, if the 5.5.8 has changed that behaviour, though.