将 null 插入 char(1)

发布于 2024-09-24 16:41:38 字数 145 浏览 2 评论 0原文

我正在创建一个 INSERT 脚本,但遇到了一个问题。有一个中间初始字段是 char(1)

有时记录在该字段中没有任何内容,所以我输入 NULL。这会导致数据列错误过长。我不想只输入“”,因为这样只会留下一个空格。

还有其他办法解决这个问题吗?

I am creating a INSERT script and I am coming across a problem. There is a middle initial field that is a char(1)

Sometimes the records don't have anything in that field so I put a NULL. This causes a Data too long for column error. I don't want to just put ' ' as that leaves just a blanks space.

Is there another way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-10-01 16:41:38

听起来您可能正在尝试将字符串 'NULL' 插入 char(1) 字段,而不是 SQL NULL,并且您启用了严格的 SQL 模式,这可以防止它被截断为N

如果可以,请

SHOW CREATE TABLE <your_table_name>

在 MySQL shell 中运行以确定目标字段是否接受 NULL。在没有上下文的情况下(您是将其作为纯 SQL 运行,还是从另一种语言的某个客户端程序连接到数据库?),很难提供准确的解决方案,但您可能会遇到这样的情况:

INSERT <your_table_name>
SELECT first_name, 'NULL', last_name

其中 'NULL' 只是一个字符串没有特殊意义,当你想要的是

INSERT <your_table_name>
SELECT first_name, NULL, last_name

下面是一个例子:

mysql> CREATE TABLE my_table ( middle_initial CHAR(1) NULL );
mysql> INSERT INTO my_table SELECT 'NULL';
mysql> SHOW WARNINGS;

Level       Code    Message
Warning     1265    Data truncated for column 'middle_initial' at row 1

mysql> SELECT * FROM my_table;

middle_initial
--------------
N

mysql> set sql_mode = 'STRICT_TRANS_TABLES';

mysql> INSERT INTO my_table SELECT 'NULL';

ERROR 1406 (22001) at line 16: Data too long for column 'middle_initial' at row 1

mysql> INSERT INTO my_table SELECT NULL;
mysql> SELECT * FROM my_table;

middle_initial
--------------
N
NULL

有点下注 - 抱歉,如果没有用......

It sounds like you may be attempting to insert the string 'NULL' into the char(1) field, rather than an SQL NULL, and you have strict SQL mode enabled, which prevents this being truncated to N.

If you are able, run

SHOW CREATE TABLE <your_table_name>

in the MySQL shell to determine whether your target field accepts NULLs. With no context (are you running this as pure SQL, or connecting to the database from some client program in another langauge?) it's difficult to provide the exact solution, but you may have something like this:

INSERT <your_table_name>
SELECT first_name, 'NULL', last_name

where 'NULL' is simply a string with no special meaning, when what you intend is

INSERT <your_table_name>
SELECT first_name, NULL, last_name

Here's an illustration:

mysql> CREATE TABLE my_table ( middle_initial CHAR(1) NULL );
mysql> INSERT INTO my_table SELECT 'NULL';
mysql> SHOW WARNINGS;

Level       Code    Message
Warning     1265    Data truncated for column 'middle_initial' at row 1

mysql> SELECT * FROM my_table;

middle_initial
--------------
N

mysql> set sql_mode = 'STRICT_TRANS_TABLES';

mysql> INSERT INTO my_table SELECT 'NULL';

ERROR 1406 (22001) at line 16: Data too long for column 'middle_initial' at row 1

mysql> INSERT INTO my_table SELECT NULL;
mysql> SELECT * FROM my_table;

middle_initial
--------------
N
NULL

Bit of a punt - apologies if no use ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文