mysql - 在没有 DEFAULT 的 information_schema DB : column with default NULL vs. 列中区分

标签 mysql

我使用 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_defaultdefault_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/

相关文章:

mysql select查询烛台市场数据

mysql - 我可以创建一个 MySQL View ,将一个范围内的随机数添加到表中的任何小数字段吗?

mysql - SabreDav 身份验证失败

mysql - 返回 2 个日期之间的行数始终返回 0

php - $_GET 未定义索引单选按钮

php - 如何在 php 中显示 blob 类型的图像?

php - 使用 MySQL 表字段填充 HTML 表的准备语句结果

php - 在 mysql 数据库中存储密码的最佳方式?

php - 让密码更安全?

java - 在 Spring-Boot 中,我们如何在同一个项目中连接两个数据库(Mysql 数据库和 MongoDB)?