mysql,一列更新后它得到了空值,怎么能不允许在insert和update中出现空值呢

mysql,一列更新后它得到了空值,怎么能不允许在insert和update中出现空值呢,mysql,Mysql,在此表中,我希望标题列在插入或更新行时不允许null(不为空)。。。。用户应100%为其插入一些值,以便生成行 create table tab(id int not null auto_increment primary key, title varchar(255) not null ); +-------+--------------+------+-----+---------+----------------+

在此表中,我希望标题列在插入或更新行时不允许null(不为空)。。。。用户应100%为其插入一些值,以便生成行

    create table tab(id int not null auto_increment
                 primary key,
                 title varchar(255) not null );
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| title | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
创建的表

现在我插入:

插入选项卡(
id
title
)值(1,'title1')。。。。。真的

插入到选项卡(
id
)值(2)中;。。。。。。。。。。。。。。。。。。。。。。。。。true------此值不应为空

mysql> select * from tab;
+----+--------+
| id | title  |
+----+--------+
|  1 | title1 |
|  2 |        |
+----+--------+
2 rows in set (0.00 sec)

为列
title
设置
notnull
约束,并删除该
default'

原因
default'
insert
中没有为title列提供值时,会向title列插入默认的空字符串

因此,如果您的模式如下所示,并带有insert值

create table tab(id int not null auto_increment
                 primary key,
                 title varchar(255) not null default '');

insert into tab(title) values('aaaaa'),('mmmmmm'),('');
然后尝试下面的查询,它将返回0行。因此,本质上,
title
列中没有
NULL
值,而是一个
空字符串

select * from tab where title is null
您的模式应该如下所示

create table tab(id int not null auto_increment
                 primary key,
                 title varchar(255) not null );

如果要进一步检查空字符串或空字符串,请参阅演示小提琴?可能您应该删除默认值如何使表中的一列不为null,当用户插入数据时,使用100%的值为该列的值,如果不是,则只给出错误并回滚。创建表选项卡(id int not null auto_increment主键,title varchar(255)not null)--------我创建这样的表,然后插入值(插入到tab(
id
)值(1);现在在标题为null时插入值(无值)这应该运行query false,但它运行true@abas_rafiq,那是不可能的。你试过了吗?你作为评论发布的内容。如果你尝试,它将抛出一个错误,因为非空约束。使用那个fiddle链接,尝试你评论的内容。看看它是如何失败的。请再次检查我的问题,它将获得空值,我自己有点惊讶。。。在我做mistakya的地方,我不能保持ID为空,这是关于标题的column@abas_rafiq,尝试阅读SQL。你没有理解我的意思。在你的show表中,ID/title都有默认的NULL,这是不可能的。不管怎样,你尝试了(每篇文章)你会发现你的mysql配置有一些问题,而不是查询的问题。