MySQL DEFAULT与MariaDB DEFAULT

时间:2017-06-14 06:22:17

标签: mysql mariadb

我有table_a个,auto_increment列名为id,字符串列名为name

运行声明: INSERT INTO table_a(id, name)VALUES(DEFAULT, 'test');

结果(MySQL):

+----+------+
| id | name |
+----+------|
| 1  | test |
+----+------+

MariaDB中运行类似语句会导致:

+----+------+
| id | name |
+----+------|
| 0  | test |
+----+------+

其他情况:

我尝试将表的AUTO_INCREMENT值编辑为30.当MariaDB插入0时,MySQL插入30。

MySQL和MariaDB的DEFAULT语句中INSERT值的区别是什么?这是MariaDB中的错误还是按预期工作?

1 个答案:

答案 0 :(得分:2)

此行为由MySQL和MariaDB中的SQL_MODE='NO_AUTO_VALUE_ON_ZERO'控制。如果你发现差异,很可能是因为你在实例上有不同的sql_mode

MariaDB [test]> CREATE TABLE t (id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.20 sec)

MariaDB [test]> SET SQL_MODE='';
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> INSERT INTO t (id) VALUES (DEFAULT);
Query OK, 1 row affected (0.05 sec)

MariaDB [test]> SELECT * FROM t;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)

MariaDB [test]> DROP TABLE t;
Query OK, 0 rows affected (0.14 sec)

MariaDB [test]> CREATE TABLE t (id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.30 sec)

MariaDB [test]> SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> INSERT INTO t (id) VALUES (DEFAULT);
Query OK, 1 row affected (0.03 sec)

MariaDB [test]> SELECT * FROM t;
+----+
| id |
+----+
|  0 |
+----+
1 row in set (0.00 sec)