MySQL 8.0.13: Default Value as uuid not working - mysql

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.

Related

Cannot convert MySQL Table from MyISAM to InnoDB

I am trying to convert a MySQL Table from MyISAM to InnoDB.
I use the following command:
ALTER TABLE `wp_wpr_rucss_used_css` ENGINE=InnoDB
But get the following error:
#1067 - Invalid default value for 'modified'
Why? How to solve it?
Update
I run the following command
SHOW CREATE TABLE `wp_wpr_rucss_used_css`
and
get the following result:
CREATE TABLE `wp_wpr_rucss_used_css` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(2000) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
`css` longtext COLLATE utf8mb4_unicode_520_ci,
`unprocessedcss` longtext COLLATE utf8mb4_unicode_520_ci NOT NULL,
`retries` tinyint(1) NOT NULL DEFAULT '1',
`is_mobile` tinyint(1) NOT NULL DEFAULT '0',
`modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_accessed` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `url` (`url`(150),`is_mobile`),
KEY `modified` (`modified`),
KEY `last_accessed` (`last_accessed`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci
Seems you have 0000-00-00 00:00:00 as the default value So
generally, The problem is because of sql_modes. Please check your current sql_modes by command:
show variables like 'sql_mode';
remove the sql_mode "NO_ZERO_IN_DATE,NO_ZERO_DATE" if you have. Then run your alter command.

Mysql - Cannot add foreign key constraint, there is no forign key in SQL query

This question is completely different from similar ones. There is no foreign key in the SQL query. This is a silly error I see when I import the SQL file on remote server. This is the SQL code
CREATE TABLE `locations` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
As you see there is no foreign key, But when I run the following code, it is ok
CREATE TABLE `locations` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ;
If I rename it to something else it is OK too.
CREATE TABLE `locationssss` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
what is wrong?
Just for future references:
Do you have more tables within your database? If so, is there a table that does contain a foreign key connected with the locations table?

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' ;

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.

Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

I always get the error:
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
DROP TABLE IF EXISTS `characters`;
CREATE TABLE `characters`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`accId` int(11) NOT NULL,
`charId` int(11) NOT NULL,
`charType` int(11) NOT NULL,
`level` int(11) NOT NULL,
`exp` int(11) NOT NULL,
`fame` int(11) NOT NULL,
`items` varchar(128) NOT NULL,
`hp` int(11) NOT NULL,
`mp` int(11) NOT NULL,
`stats` varchar(64) NOT NULL DEFAULT '"1,2,3,4,5,6,7,8"',
`dead` tinyint(1) NOT NULL DEFAULT '0',
`tex1` int(11) NOT NULL DEFAULT '0',
`tex2` int(11) NOT NULL DEFAULT '0',
`pet` int(11) NOT NULL DEFAULT '0',
`fameStats` varchar(128) NOT NULL DEFAULT 'eNoVytkRgCAMRdGH4IIbgmsPdmNVNmZf5n7kzM0kksLjJN2V4b30vcHK1YYam9hCxxqh5zpQI0wwQ4IFMhRYYeNjpw444YIfA3kDIA',
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`totalFame` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
This is my first time using MySQL stuff. The reason I am using it because I downloaded a source for a game with server and this is needed.
The problem is isolated to these two lines
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
To get rid of the error, you need to modify one of those lines to remove the DEFAULT CURRENT_TIMESTAMP. MySQL only allows one timestamp column to be defined that way. For example:
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL,
Because the deathTime column is defined as NOT NULL and doesn't have a default, you'll need to supply a value for deathTime whenever you insert a row. You could get the current date and time assigned to that column for an INSERT using a BEFORE INSERT trigger.
You are trying to, well, define more than "one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause". Specifically, createTime and deathTime.
You Can't Do That.™