How can I add 24 hours in CURRENT_TIMESTAMP MySQL? - 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

Related

unable to create a foreign key constraint in mysql

I am unable to create a foreign key constraint in mysql for the column "vsa", below is my table structure. i am getting error as "Error creating foreign key on vsa(check data types)". Here I don't see any issues with data type. What am i Missing.
CREATE TABLE `child` (
`id` int(11) NOT NULL,
`day` tinyint(3) NOT NULL,
`vsa` varchar(50) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `parent` (
`price_id` int(10) NOT NULL,
`device_id` text NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`vsa` varchar(50) NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
As an alternative approach I run the alter query to add a constraint. I am getting below error.
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
for correct foreign key definition. ```

MySQL 8.0.13: Default Value as uuid not working

I am trying to set the Default value as UUID() in MySQL version 8.0.13. But upon successful execution, the default value resets to NOT NULL.
MySQL version:
Here is my CREATE TABLE script
CREATE TABLE `session` (
`id` binary(16) NOT NULL DEFAULT (UUID_TO_BIN(UUID(), TRUE)),
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
the log output on table generation,
SQL script was successfully applied to the database.
The TABLE definition post execution:
CREATE TABLE `session` (
`id` binary(16) NOT NULL,
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
I am not able to figure out why this could happen when documentation clearly mentions that parenthesis enclosed functions are allowed.
This is unfortunately a bug with default expressions for primary key columns, Expression Default is made NULL during CREATE TABLE query, if field is made PK.
It is fixed in MySQL 8.0.19:
For a column defined as a PRIMARY KEY in a CREATE TABLE statement, a default value given as an expression was ignored. (Bug #29596969, Bug #94668)
As a workaround (if you cannot upgrade), you can add the primary key afterwards with an ALTER TABLE-statement:
CREATE TABLE `session` (
`id` binary(16) NOT NULL DEFAULT (UUID_TO_BIN(UUID(), TRUE)),
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ALTER TABLE `session` ADD PRIMARY KEY(`id`);
I needed the column to not be a binary one. So, in my case, I declared it like this:
`id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT (UUID()),
For anyone who needs it to store UUIDs with default values in a char column.

MySQL Foreign Key format during table creation

I've run into a strange situation. I have two tables that initially looked like this:
CREATE TABLE vendors (
vendor_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
[...]
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (vendor_id)
) ENGINE=InnoDB CHARACTER SET=utf8mb4;
CREATE TABLE vendor_orders (
vendor_order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
vendor_id INT UNSIGNED NOT NULL REFERENCES vendors(vendor_id),
[...]
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (vendor_order_id)
) ENGINE=InnoDB CHARACTER SET=utf8mb4;
However, after inspecting the database, I discovered the foreign key in the table definition for vendor_orders wasn't created. I then tried to create the second table like this:
CREATE TABLE vendor_orders (
vendor_order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
vendor_id INT UNSIGNED NOT NULL,
[...]
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (vendor_order_id),
FOREIGN KEY (vendor_id) REFERENCES vendors(vendor_id)
) ENGINE=InnoDB CHARACTER SET=utf8mb4;
That worked, and my foreign key was created. Can anyone tell me what the difference between the two formats is? I thought both were supposed to be equivalent. I'm using MySQL 5.6.33.
Your expectation was correct, the references keyword as you used in the first example should create the foreign key, but it does not work that way. You are not the first who ran into this MySQL bug. The solution is to avoid using the inline foreign key definition and explicitly write out the foreign key as you did in the second example.

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

Create table with default values giving error

Using MySql Workbench 6.3 Build version 6.3.6.
I am trying to create a table with Default constraint but its giving me error.
Here is the script
Create Table `Migration_Log2` (
`Id` Int NOT NULL AUTO_INCREMENT,
`FilePath` varchar(1000) NOT NULL,
`FileName` varchar(100) NOT NULL,
`IsSent` bool NOT NULL DEFAULT '0',
`CreatedDate` DateTime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ModifiedDate` DateTime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`SendAttemptMade` int NOT NULL DEFAULT '0',
`Message` Text DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `migration_log_Id_UNIQUE` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Error Message
Error Code: 1067. Invalid default value for 'CreatedDate' 0.000 sec
This may be due to some strict constraint on the data type check on database server.
I would suggest to change type of field CreatedDate from datetime to timestamp.
I had faced similar issue in a VPS for my website.
Your CURRENT_TIMESTAMP might have been appending the microseconds in the output.
Try to use: CURRENT_TIMESTAMP(0) as the default value.
SELECT CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(0), CURRENT_TIMESTAMP(1), CURRENT_TIMESTAMP(2);
The microseconds mattered, possibly. See the differences.