Using MySQL 5.6.40
This is the table definition:
CREATE TABLE IF NOT EXISTS `updated_tables` (
`table_name` VARCHAR(50) NOT NULL,
`updated_at` TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (`table_name`),
UNIQUE INDEX `table_name_UNIQUE` (`table_name` ASC))
ENGINE = InnoDB;
This is the query (Generated by Sequalize):
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL) ON DUPLICATE KEY UPDATE `table_name`=VALUES(`table_name`), `updated_at`=VALUES(`updated_at`);
Simplified form of the same query:
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL)
And it produces the following entry in table:
table_name: workdamnit
updated_at: 2018-07-05 14:27:17.142494
Now to the question.
Using MySQL 5.6.39-log on AWS RDS
Gives this error:
Error Code: 1048. Column 'updated_at' cannot be null
Is it since the MySQL versions are a bit different, or it has to do something with RDS?
During the creation of your table you have mentioned the following on your column: updated_at:
Do not allow NULL
If nothing is provided, use CURRENT_TIMESTAMP(6) as default.
Hence, It is giving the error when you are inserting NULL in your query.
So, If I understand what you are trying to do currectly, this should be your query:
INSERT INTO `updated_tables` (`table_name`) VALUES ('workdamnit')
instead of
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL)
Hope it helps
You are trying to INSERT NULL on a field that does not allow it. Furthermore this field has a default value. so you can simply try:
INSERT INTO `updated_tables` (`table_name`) VALUES ('workdamnit') ...
After some research I found the answer. The RDS instance has a strict mode turned on by default, so it was responding properly. While my local instance, and also an instance of MySQL on EC2, were not using the strict mode.
Related
I have the following create table statement.
CREATE TABLE `test_table` (
`id` INT(11) NOT NULL,
`field1` varchar(10),
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
when I do the following insert, I'll get the erro "Column 'updated_at' cannot be null"
insert into test_table (id, field1, updated_at) values (1234, 'foo', null);
I expected updated_at would just take on the default value in this case.
mysql version 5.7.12
However when I do this in mysql version 5.6, the insert commands works.
Is there a change in the versoin from 5.6 to 5.7? The only difference I thought was 5.7 has NO_ZERO_DATE default to true. But I thought that was only for datetime. Is there a configuration change that I need to make?
It's possible to achieve the result by not passing in updated_at but I don't have over insert statement in this case.
I suspect that this is related to SQL Strict mode - but I can't find the exact quote in the documentation that matches your use case.
Bottom line, you should not be expecting the server to use the default when you provide it with an explicit null value.
If you can't remove the column from the insert list for some reason, a possible workaround is to use the default keyword, which makes your intent unambiguous:
insert into test_table (id, field1, updated_at)
values (1234, 'foo', default);
This error is thrown because explicit_defaults_for_timestamp is enabled. Disabling this will solve the issue
Refer: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
I am trying to migrate data from Oracle table over to MariaDB table. I have csv files from the Oracle table unload and a load script to load these files into corresponding mariadb tables which I have created using the same table/schema definition as it is in Oracle side. I am getting the 1062 (230000) Duplicate entry error however I don't understand why MySQL/Mariadb treats the data as unique? The value in the 1st record which gets inserted is in lower case and in the 2nd insert which fails is in uppercase. It seems that both mysql and mariadb are treating the value the same and hence I am getting the Duplicate entry error for primary key. In order to show my error, I have created a test table and manually attempted to insert 2 rows. I have put all the relevant info below and would appreciate if there is some explanation. Thanks in advance for your help.
MariaDB [mytestdb]> create table test1 (id char(15) not null, recnum integer, primary key(id));
MariaDB [mytestdb]> show create table test1\G
*************************** 1. row ***************************
Table: test1
Create Table: CREATE TABLE `test1` (
`id` char(15) NOT NULL,
`recnum` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
MariaDB [mytestdb]> insert into test1 values ('abc123',1);
Query OK, 1 row affected (0.01 sec)
MariaDB [mytestdb]> insert into test1 values ('ABC123',2);
ERROR 1062 (23000): Duplicate entry 'ABC123' for key 'PRIMARY'
Thanks,
Sanjay
MySQL isn't case sensitive by default. You should define the column with one of the COLLATION options (*_cs) suitable for the datatype, or if none exists, the _bin collation. Or, if all else fails (eg, the _cs or _bin collations for your datatype don't exist), you may need to set the column to BINARY charset instead.
As a starting point, https://dev.mysql.com/doc/refman/5.7/en/charset-mysql.html might be useful.
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 know this has been discussed before but when I read the other threads, they don't seem to address my problem.
When I try to run the SQL query in PhpMyAdmin, I get the error :
#1062 - Duplicate entry 'button_buynow' for key 'PRIMARY'
I am sure the table was empty prior to me running the query so I don't know what's going on. Can somebody shed a light?
CREATE TABLE IF NOT EXISTS `buttons` (
`name` varchar(255) NOT NULL default '',
`value` text NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `buttons`
--
INSERT INTO `buttons` (`name`, `value`) VALUES
('button_buynow', 'buynowCC_LG.gif'),
('button_addtocart', 'x-click-but41.gif'),
('button_viewcart', 'viewcart_LG.gif'),
('button_freedownload', 'downloadnow.jpg');
I am sure the table was empty prior to me running the query so I don't know what's going on.
If you're sure that the table was empty you might have a trigger defined on this table that is the cause of this error.
You can check it this way
SELECT *
FROM information_schema.triggers
WHERE trigger_schema = schema()
AND event_object_table = 'buttons'
If you do in fact have a trigger then you either fix it or just drop it.
I currently trying to use an Object Relational Mapper for CodeIgniter and I'm experiencing something I did not expect.
I have a table with a couple of fields, some of which are NOT NULL. An insert query is which is missing of the NOT NULL fields is generated -- a new row is added but with blanks for those fields.
I did not know MySQL would disregard the NOT NULL fields that aren't present in the query and insert the row anyways. Is there a way to restrict this?
-Edit-
Let me add a few more details and try to explain it a bit more
Here is a sample table:
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`color` varchar(40) COLLATE utf8_bin DEFAULT '',
`shape` varchar(40) COLLATE utf8_bin NOT NULL,
`size` varchar(40) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
Here is a sample query:
INSERT INTO `test` (`shape`) VALUES ('foo')
I don't have size in my query yet it still adds the row - is this expected?
(The sample query was run in phpMyAdmin)
I believe the accepted answer is incorrect, given the question's test INSERT statement. It looks to me like MySQL's "strict mode" is turned off for this table or database. From the docs:
Strict mode controls how MySQL handles input values that are invalid or missing... 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...
If you are not using strict mode (that is, neither STRICT_TRANS_TABLES nor STRICT_ALL_TABLES is enabled), MySQL inserts adjusted values for invalid or missing values and produces warnings.
You can find out how your database is running with these queries:
SELECT ##global.sql_mode;
SELECT ##session.sql_mode;
Changing these values is discussed here: https://stackoverflow.com/a/5273824/27846
Empty string is not the same thing as NULL. Perhaps ORM inserts just '' for those fields.
Not a codeigniter dev, but I would hazard a guess that the issue is your ORM is passing blank values on to the database, I would check your logs to verify this and if its the case, check your ORM if it has some validation options.