mysql char null,将null插入char(1)

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?

解决方案

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

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

SELECT first_name, 'NULL', last_name

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

INSERT

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 ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值