Pergunta

On the percona-5.6 and MariaDB-10.0 servers, the NOT NULL declared on table field are being ignored. I am able to leave fields declared as NOT NULL empty when inserting data and the servers are not throwing an error or stopping the insertion from going on. When i try it on MySQL-5.6 and MariaDB-5.5, it works fine as expected and does not allow yo to insert empty values on fields declared as NOT NULL.

Please, advise why this is the case on mariaDB-10.0. Thanks

Foi útil?

Solução

This is taken from the MySQL documentation:

Changes in MySQL 5.7.1 (2013-04-23, Milestone 11)

... If a column is declared as NOT NULL, it is not permitted to insert NULL into the column or update it to NULL. However, this constraint was enforced even if there was a BEFORE INSERT (or BEFORE UPDATE trigger) that set the column to a non-NULL value. Now the constraint is checked at the end of the statement, per the SQL standard. (Bug #6295, Bug#11744964). ...

Basically to change this behavior can do the following:

MariaDB> SET SESSION sql_mode := 'NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
MariaDB> SELECT @@GLOBAL.sql_mode 'sql_mode::GLOBAL',
          @@SESSION.sql_mode 'sql_mode::SESSION';
+------------------------+-----------------------------------------------------------------------------------------------------------------+
| sql_mode::GLOBAL       | sql_mode::SESSION                                                                                               |
+------------------------+-----------------------------------------------------------------------------------------------------------------+
| NO_ENGINE_SUBSTITUTION | NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+------------------------+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB> SHOW CREATE TABLE `table_name`;
+------------+----------------------------------------------------------------------------+
| Table      | Create Table                                                               |
+------------+----------------------------------------------------------------------------+
| table_name | CREATE TABLE `table_name` (                                                |
|            |        `id` INT(11) UNSIGNED NOT NULL,                                     |
|            |        `field` VARCHAR(20) DEFAULT NULL,                                   |
|            |        `field_with_default_value` VARCHAR(20) NOT NULL DEFAULT 'myDefault' |
|            | ) ENGINE=InnoDB DEFAULT CHARSET=latin1                                     |
+------------+----------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB> INSERT INTO `table_name`(`id`, `field`, `field_with_default_value`)
   VALUES
   (1, 'Value', NULL);
ERROR 1048 (23000): Column 'field_with_default_value' cannot be null

I hope this a light on the road.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a dba.stackexchange
scroll top