In one of my tables, I have a column with some null values and some integer values. When I use MySQL Workbench to set a default value and non-null for that column, I get a truncation error.
When my colleague performs the same operation, his apply works and the null value are set to 0.
The only obvious difference is that he is running MySQL Server 5.6 and I am running 5.5.
An example table can be created with the following CREATE:
CREATE TABLE 'resource`.`test_table` (
`idtest_table` INT NOT NULL ,
`bad_column` BIGINT(20) NULL ,
PRIMARY KEY (`idtest_table`) );
Insert some values as follows:
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (1);
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (2);
INSERT INTO `resource`.`test_table` (`idtest_table`) VALUES (3);
The command that is failing is:
ALTER TABLE `resource`.`test_table` CHANGE COLUMN `bad_column` `bad_column` BIGINT(20) NOT NULL DEFAULT 0 ;
with the following error:
ERROR 1265: Data truncated for column 'bad_column' at row 1
SQL Statement:
ALTER TABLE `resource`.`test_table` CHANGE COLUMN `bad_column` `bad_column` BIGINT(20) NOT NULL DEFAULT 0
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'test_table' already exists
SQL Statement:
CREATE TABLE `test_table` (
`idtest_table` int(11) NOT NULL,
`bad_column` bigint(20) DEFAULT NULL,
PRIMARY KEY (`idtest_table`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Is there a setting in the MySQL config that allows this? Is it the version?
The error is occurring because bad_columns has a value of NULL after the insert.
You are changing the column type to "not NULL", so what can the database do? It can generate an error. The default value doesn't apply to existing rows.
If you want to do this, then update the column first:
update resource.test_table
set bad_column = 0
where bad_column is null;
Related
I'm using AWS Aurora MySQL 5.7. I have a column that is of type MEDIUMTEXT that I'd like to convert to JSON. After using an ALTER to accomplish this, I can neither update the newly JSON-typed field of an existing record nor insert new records. In both cases, a Cannot CAST value to JSON error is thrown. the default character set for this database is latin1, but the character set on this table is utf8.
The table is defined like:
CREATE TABLE `table_sample` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`config` mediumtext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT
I've tried changing the column type via:
ALTER TABLE table_sample
ADD COLUMN config2 JSON DEFAULT NULL;
UPDATE table_sample
SET config2 = IF(JSON_VALID(config), config, NULL);
ALTER TABLE table_sample
DROP COLUMN config;
ALTER TABLE table_sample
CHANGE config2 config JSON;
Attempting to duplicate a record like this, after the conversion to JSON, is one way to cause the aforementioned CAST error:
INSERT INTO table_sample (config)
SELECT config FROM table_sample WHERE id = 1;
I have curious about why some not null column already set default value, but during insert sql script, it will throw error.
Here is the sample table
drop table if exists `delivery`;
create table `delivery`(
`price` BIGINT not null default 0,
`created_time` TIMESTAMP(6) not null default CURRENT_TIMESTAMP (6)
) ENGINE=INNODB DEFAULT CHARSET=UTF8MB4
;
Let's say, execute the three statement below, only the second statement will not throw error
insert into `delivery` (`price`,`created_time`) values (null, null);
insert into `delivery` (`price`,`created_time`) values (1, null);
insert into `delivery` (`price`,`created_time`) values (null, now());
So does it have anyway to insert null for bigint datatype column and make it execute success? And any ideas for the logic behind.
You can't insert null values since you have NOT NULL constraints on the columns.
The first and third statement throw an error since you are trying to insert a null value into the column price and/or created_time, and that clearly doesn't satisfy the constraint(s).
If you really want to allow null values, then remove the NOT NULL constraint on the column(s).
Alternatively, you could sucessfully run your SQL statements as shown below:
insert into `delivery` () values ();
insert into `delivery` (`price`) values (1);
insert into `delivery` (`created_time`) values (now());
select * from `delivery`;
Result:
price created_time
----- --------------------------
0 2020-04-16 09:48:23.505147
1 2020-04-16 09:48:25.549202
0 2020-04-16 09:48:26.0
EDIT:
The second query actually succeeds in MySQL 5.7; it silently ignores the explicit null value and uses the default value.
It seems that the behavior was fixed in MySQL 8.x since it fails now (as it should).
my table structure is
CREATE TABLE IF NOT EXISTS `emp` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(11) DEFAULT NULL,
`age` varchar(31) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
My query is :
INSERT INTO `emp` (`id`, `name`) VALUES ('1', 'prashant');
This is working with all the MYSQL versions below 5.7, but not working with MYSQL version 5.7.12-0ubuntu1
Getting error :
#1364 - Field 'age' doesn't have a default value
What is new in this version ??
Try it on mysql version below 5.7 ,you will see the difference.
Thanks :-)
It would be a huge surprise if this worked in any version of mysql at all. Copy paste this into sqlfiddle.com (mysql 5.6 or 5.5) and confirm for yourself.
age is defined as varchar(31) and not null. Thus your insert statement should have a value for that column. Or you should give it a default value. While you are at it, change it to a more appropriate data type.
CREATE TABLE IF NOT EXISTS `emp` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(11) DEFAULT NULL,
`age` int(3) NOT NULL default 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
Updated:
Thinking about this some more I think you have switched off Strict Mode in your older version of mysql
Strict mode controls how MySQL handles invalid or missing values in
data-change statements such as INSERT or UPDATE. A value can be
invalid for several reasons. For example, it might have the wrong data
type for the column, or it might be out of range. A value is missing
when a new row to be inserted does not contain a value for a non-NULL
column that has no explicit DEFAULT clause in its definition. (For a
NULL column, NULL is inserted if the value is missing.) Strict mode
also affects DDL statements such as CREATE TABLE.
So my original statement is wrong! With string mode off, the default for varchar is probably '' (not sure though never used strict mode off)
In your table age described as not null.
`age` varchar(31) NOT NULL
So, it is required field for insert.
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field.You have to give value for age also in your insert query because it cannot be null.For eg:-
insert into emp(`id`,`name`,`age`) values('1','rahul','26')
hope this helps!!.Comment for further query
I have a table constructed by the followinng:
CREATE TABLE IF NOT EXISTS test_table (
ID int(11) NOT NULL AUTO_INCREMENT,
ProfileID int(11) NOT NULL,
ForeignID int(11) NOT NULL,
PRIMARY KEY (ProfileID,ForeignID) )
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I want to do something a little peculiar though, say there are 4 records in the database:
RecA, RecB, RecC, RecD
I would like to run the following query and have the insert behavior stop when a duplicate key was encountered:
INSERT IGNORE INTO test_table (ProfileID, ForeignID) VALUES(RecE, RecF, RecA, RecB, RecG);
So the query would only insert RecE and RecF, is there a way to do this in MySQL, perhaps using ON DUPLICATE KEY? Ideally the execution would just be terminated once a duplicate has been found, I am not too familiar with SQL syntax though.
Where RecG was explicitly not inserted.
If i execute my sql create table statement i get an errormessage that says:
Invalid default value for 'end'
CREATE TABLE IF NOT EXISTS `monkeybutler`.`Setuptimeslot` (
`id` INT NOT NULL,
`begin` TIMESTAMP NOT NULL,
`end` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
If I change the statement and comment out the "end"-column it works, but as soon as i try to create 2 columns of type TIMESTAMP it doesnt work anymore. How can I insert both columns without getting an error?
(This is the complete Create Table Statement and my mysql Version is 5.6.19)