我使用 MySQL 5.7。 如果我只查看“information_schema”数据库,有没有办法区分默认为 NULL 的列和没有默认的列?
这是我的 table :
mysql> CREATE TABLE defaults (default_null varchar(100) DEFAULT NULL, no_default varchar(100)) ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
mysql> ALTER TABLE defaults ALTER COLUMN no_default DROP DEFAULT;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE defaults;
+----------+------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+------------------------------------------------------------------------------------------------------------------------------------------+
| defaults | CREATE TABLE `defaults` (
`default_null` varchar(100) DEFAULT NULL,
`no_default` varchar(100)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO defaults SET no_default = 'foo';
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO defaults SET default_null = 'bar';
ERROR 1364 (HY000): Field 'no_default' doesn't have a default value
mysql>
no_default 和 default_null 列不同,但在 information_schema.columns 表中它们是相同的:
mysql> SELECT column_name, is_nullable, IFNULL(column_default, 'real NULL') FROM information_schema.columns WHERE table_name = 'defaults';
+--------------+-------------+-------------------------------------+
| column_name | is_nullable | IFNULL(column_default, 'real NULL') |
+--------------+-------------+-------------------------------------+
| default_null | YES | real NULL |
| no_default | YES | real NULL |
+--------------+-------------+-------------------------------------+
2 rows in set (0.01 sec)
mysql>
最佳答案
如果启用了严格的 sql 模式,则此行为适用于每个版本。
If strict SQL mode is enabled, an INSERT statement generates an error if it does not specify an explicit value for every column that has no default value. See Section 5.1.10, “Server SQL Modes”.
您可以在 https://dev.mysql.com/doc/refman/5.7/en/insert.html 下找到它
关于mysql - 在没有 DEFAULT 的 information_schema DB : column with default NULL vs. 列中区分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57333198/