错误代码:1406.列的数据太长 - MySQL
错误代码:1406.列的数据太长
Error Code: 1406. Data too long for column
CREATE TABLE `TEST`
(
`idTEST` INT NOT NULL ,
`TESTcol` VARCHAR(45) NULL ,
PRIMARY KEY (`idTEST`)
);
现在插入一些值
INSERT INTO TEST
VALUES
(
1,
'Vikas'
)
select
SELECT * FROM TEST;
插入超过长度
INSERT INTO TEST
VALUES
(
2,
'Vikas Kumar Gupta Kratika Shukla Kritika Shukla'
)
如果我们select length
SELECT LENGTH('Vikas Kumar Gupta Kratika Shukla Kritika Shukla')
'47'
它显示错误信息
错误代码:1406.列的数据太长
但我的期望是,我想在表格中至少插入前 45 个字符
But what is my expectation is, I want to insert at least first 45 characters in Table
如果问题不清楚,请告诉我.
please let me know if the question is not clear.
我知道这个错误的原因.我试图插入超过数据类型长度的值.
I know the cause of this error. I am trying to insert values more than the length of datatype.
我想要在 MySQL 中的解决方案,因为它可以在 MS SQL 中实现.所以我希望它也能在 MySQL 中.
I want solution in MySQL as It is possible in MS SQL. So I hope it would also be in MySQL.
推荐答案
MySQL 将截断任何超过指定列宽的插入值.
MySQL will truncate any insert value that exceeds the specified column width.
要做到这一点,请尝试将您的 SQL 模式 切换为不使用 STRICT.
to make this without error try switch your SQL mode to not use STRICT.
Mysql 参考手册
改变模式
这可以通过两种方式完成:
This can be done in two ways:
- 在 MySQL 安装目录中打开您的
my.ini(Windows) 或my.cnf(Unix) 文件,并查找文本sql-mode".
- Open your
my.ini(Windows) ormy.cnf(Unix) file within the MySQL installation directory, and look for the text "sql-mode".
查找:
代码:
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
替换为:
代码:
# Set the SQL mode to strict
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
或
- 您可以在数据库管理工具(例如 phpMyAdmin)中运行 SQL 查询:
代码:
SET @@global.sql_mode= '';
相关文章