V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yxisenx
V2EX  ›  问与答

mysql 建表的时候主键类型为 varchar not null, 不设置长度, 为什么会报错呢?

  •  
  •   yxisenx · 2023-03-02 18:06:52 +08:00 · 604 次点击
    这是一个创建于 440 天前的主题,其中的信息可能已经有所发展或是发生改变。

    报错 sql

    create table `err_status_list`(
      `id` VARCHAR not null ,
      `err_msg` varchar(255) not null comment '错误信息',
      `err_status` int not null comment '状态码',
      `create_time` long not null comment '创建时间',
      primary key(`id`)
    ) engine=InnoDB default charset=utf8 comment='错误状态表';
    

    错误:

    > 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'not null ,
      `err_msg` varchar(255) not null comment '错误信息',
      `err_st' at line 2
    > 时间: 0s
    
    

    指定了长度的话就正常了.

    lalawu
        1
    lalawu  
       2023-03-02 18:09:29 +08:00
    varchar 字段在声明的时候是一定要指定长度的,不然 mysql 不知道你这变长字符串最长多长呀;
    还有建议如果设置了 not null 最好加上 default 值
    yxisenx
        2
    yxisenx  
    OP
       2023-03-02 18:12:11 +08:00
    @lalawu 十分感谢。 我之前在 navicat 建表的时候不设置长度, 它指定了 255 ,我还以为是 mysql 默认的。误会了好久。
    lalawu
        3
    lalawu  
       2023-03-02 19:57:56 +08:00
    @yxisenx 哈哈 谢谢 navicat
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1232 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:12 · PVG 02:12 · LAX 11:12 · JFK 14:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.