Skip to content

值也 长 for 列

错误信息

                值也 长 for 类型
              

快速摘要

你 tried 到 insert 字符串那个是 longer than 列's 最大值长度 (e.g., VARCHAR(50) 但值 有 60 characters).

为什么发生

  • 字符串值 exceeds 列's 定义长度 (VARCHAR(n), CHAR(n))

  • 用户输入是 不验证 or truncated 之前 insertion

  • 列长度是 设置也 小 for 数据

最小示例

✗ 错误代码
INSERT INTO users (name) VALUES ('This is a very long name that exceeds the 50 character limit of the column');
-- Column is VARCHAR(50) but value is 80+ characters
✓ 修复代码
-- Option 1: increase the column length
ALTER TABLE users ALTER COLUMN name TYPE VARCHAR(255);
INSERT INTO users (name) VALUES ('This is a very long name that exceeds the 50 character limit of the column');

值是 也长 for VARCHAR(50). Increasing 列到 VARCHAR(255) accommodates longer 值.

如何诊断

  • 检查列's 类型 and 长度: \d table_name

  • Measure 长度的 值你 是 trying 到 insert

  • 检查如果 application 验证输入长度

如何修复

  • Truncate 值到 fit 列长度

  • Increase 列长度: ALTER TABLE ... ALTER COLUMN ... TYPE VARCHAR(255)

  • Use TEXT (无限长度) 相反的 VARCHAR(n) 如果 appropriate

  • 验证 and 拒绝长 输入 in application

如何预防

  • 验证输入长度 in application 之前 inserting

  • Use TEXT or appropriately sized VARCHAR 列

  • 设置表单输入 maxlength attributes 到 match 列长度

相关资源

相关术语

相关课程

  • 表

    学习关于列 类型 and 长度.

← 返回语言