MySQL insert query with missing not null fields - mysql

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.

Related

Unique constraint that allows zero values (not null) in MySQL

I am facing the following problem.
I have a table with this column:
`responsible_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
From some reasons that have to do with the project, I cannot change this definition (NULL as default is not an option in this project).
I need to have this column UNIQUE, unless the value is 0.
Of course, I can do a check before I do INSERT/UPDATE from my php code, but constraints exist exactly to avoid this bad practice...
Any idea how to do it?
Existing:
CREATE TABLE tablename (
...
`responsible_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
... );
Addition:
ALTER TABLE tablename
ADD COLUMN responsible_id_null VARCHAR(255) COLLATE utf8mb4_unicode_ci
GENERATED ALWAYS AS (NULLIF(responsible_id, '0')) /* STORED / VIRTUAL */,
ADD UNIQUE INDEX idx_responsible_id_null (responsible_id_null);
PS. Remember, that this may cause problems - for example, if somewhere SELECT * is used... or INSERT without columns list...
Could you edit and explain what's going on here, thanks :-) – Martin
I simply add generated column where '0' value is replaced with NULL value, and create unique index by this column (which will forbid duplicates but ignore/allow NULLs in generated column, i.e. forbid duplicates in source column but ignore/allow '0' in it).

Mysql General Log Retrieve BLOB Content

Mysql :5.7
General Log: 1
log_output : Table
I have written some code using java to store images in a blob column of a table (tbl_attachment_mst).
My General Log settings are turned on and is configured to write to 'table'.
Whenever i add an image to tbl_attachment_mst , mysql does log it in the mysql.general_log table with _binary('some unreadable characters not sure what this is').
I have accidently lost contents of the table tbl_attachment_mst . Is it possible to recover my data from the mysql.generaL_log table??
I think i am having some issue with the character set while trying to execute the query that is stored in mysql.general_log.
(From comment)
CREATE TABLE tbl_attachment_mst (
attachment_id int(11) NOT NULL AUTO_INCREMENT,
file_name varchar(200) DEFAULT NULL,
created_date datetime DEFAULT NULL,
activate_flag tinyint(4) DEFAULT NULL,
file_id int(11) DEFAULT NULL,
type varchar(300) DEFAULT NULL,
attachment_asblob longblob,
PRIMARY KEY (attachment_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
insert into tbl_attachment_mst values
(default,'test.jpg',sysdate(),1,1,'Type',
_binary('some characters here'))
Grab it fast. I suspect the general_log, as a table, gets flushed in some fashion pretty fast.
It works just like a table, so SELECTs work. However, your SELECTs go into the general log unless you have turned it off.
Recommend doing SELECT ... HEX(col) ... to avoid the unprintable characters.
Please provide more details so I can try to simulate it and see what would work best.
SHOW CREATE TABLE
An approximation of the INSERT statement (or whatever was involved)
And if the grabbed hex is useful, you can use something like this to reverse the steps:
INSERT ... VALUES (... UNHEX(hex_string) ... )
using the below set of queries, i was able to get the exact data that was inserted into the db at that particular time.....
SELECT argument INTO #sql FROM mysql.general_log limit 1;
PREPARE sql_query FROM #sql;
EXECUTE sql_query;
These queries would insert the data back into the tbl_attachment_mst...

Query in Eloquent works when it shouldn't - in MySQL, it fails as expected

If you have an integer field setup in MySQL, no nulls allowed, default null, and you write a query that omits this field, in Laravel the query still executes, and the default value of null isn't used, but instead written as '0'.
If I take the exact, generated query by Eloquent/query builder, and run it in MySQL, it fails, rightly stating that the field in question must have a default value.
Why is there this discrepancy? What the hell is eloquent/query builder/pdo doing?!
create table syntax below:
CREATE TABLE `chapters` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(10) unsigned NOT NULL,
`season_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `chapters_account_id_index` (`account_id`),
KEY `chapters_season_id_index` (`season_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6060 DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
Doing an insert and omitting account_id in MySQL, fails - as it should. Doing the same in Eloquent/Laravel, does not, despite the generated query being correct.
So I figured out what was going on. Laravel by default sets strict rules to false in config/database.php. This turns off all sorts of options regarding strict mode, such as full group bys, strict trans table.etc.
What we did is set STRICT_ALL_TABLES on our my.cnf and then enforced laravel to strict = true. This solves it.

phpMyAdmin - "Please enter a valid length"

I'm trying to create a table in phpMyAdmin, and I keep getting the same error no matter how I manipulate the SQL code. This is the preview SQL that phpMyAdmin generates
CREATE TABLE `puppies`.`animals` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`puppy_name` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`breed_id` INT(11) NOT NULL ,
`description` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`price` DECIMAL(10,2) NOT NULL ,
`picture_url` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`sold` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
I've tried it with multiple variations of brackets and commas.
I have also faced the same issue and what I did was clicked on Preview SQL and copy the sql query and paste it in the SQL Run
To those still experiencing this, and don't want to wait for it to randomly work again:
I just encountered this, and cannot find any explanation other than some bug.
I tried:
CREATE TABLE `database`.`measurement_types` (
`TypeID` INT(2) NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(32) NOT NULL ,
`Description` VARCHAR(256) NOT NULL ,
PRIMARY KEY (`TypeID`)) ENGINE = InnoDB;
Which produced the same "Please enter valid length" error
Tried a few times with different length values, but kept getting the same error.
--SOLUTION--
So I just created the table with a single column first, then altered it with the two other columns like so:
CREATE TABLE `database`.`measurement_types` (
`TypeID` INT(2) NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`TypeID`)) ENGINE = InnoDB;
And then:
ALTER TABLE `measurement_types`
ADD `Name` VARCHAR(32) NOT NULL AFTER `TypeID`,
ADD `Description` VARCHAR(256) NOT NULL AFTER `Name`;
And that worked.
I also tried to delete the table and create it with the first SQL again, and this time it worked. Seems pretty random
I've had the same issue, seems to be a bug with VARCHAR fields. My solution was to make those fields INT, create the table, and then change them back to VARCHAR
You can also solve it by restarting your mysql... It worked for me.
It is a bug with varchar. If you change collation to utf8mb4_general_ci it should fix the problem
There seems to be some issue with PhpMyAdmin.
It needs 'Collation' value if column type is of varchar. You will encounter the error “Please enter a valid length” if the Collation field is empty for varchar. So basically both fields 'Size' and 'Collation' are mandatory and cannot be empty.
Please set Collation field with some value like 'utf8mb4_general_ci' to resolve the issue.
I solved the issue of "Please enter a valid length" by adding one by one column separately and by giving length to all the columns as the database needs to know how much memory it will consume.
It is definitely a problem with varchar fields but does not always happen.
You can still create your table by copying out the SQL query and executing the raw query.
As stated in the official docs, I think it is mandatory to give a specific length in later versions of phpMyAdmin.
I solved my “Please enter a valid length” by typing the length values for the data types that weren't of a dynamic memory allocation type. So therefore they obviously needed to know how much memory they could use for storage. Or by all means a valid length
If you use Varchar you have to give a length. Otherwise, it will not save.
My solution was to set a value for every varchar type.

insert not working on mysql 5.7.12-0?

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