gpt4 book ai didi

mysql,更新后的列得到空值,如何在插入和更新中不允许空值

转载 作者:行者123 更新时间:2023-11-30 00:14:07 26 4
gpt4 key购买 nike

在此表中,我希望标题列在插入或更新行时不允许为空(不是空白)......用户应该 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) value (1, 'title1'); .....正确

插入选项卡 (id) 值 (2);........................true --- -- 这不应该为空。

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

最佳答案

为列title添加一个NOT NULL约束并删除该默认''

因为只要您没有在 insert 中为标题列提供值,default '' 就会向标题列插入默认的空字符串。

所以,如果您的架构如下并插入值

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 值,而是 EMPTY STRING

select * from tab where title is null

您的架构应如下所示,

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

如果您想进一步检查,请参阅演示 fiddle http://sqlfiddle.com/#!2/b5acf/3

关于mysql,更新后的列得到空值,如何在插入和更新中不允许空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23798404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com