V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
yxisenx
V2EX  ›  问与答

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

  •  
  •   yxisenx · Mar 2, 2023 · 1120 views
    This topic created in 1151 days ago, the information mentioned may be changed or developed.

    报错 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  
       Mar 2, 2023
    varchar 字段在声明的时候是一定要指定长度的,不然 mysql 不知道你这变长字符串最长多长呀;
    还有建议如果设置了 not null 最好加上 default 值
    yxisenx
        2
    yxisenx  
    OP
       Mar 2, 2023
    @lalawu 十分感谢。 我之前在 navicat 建表的时候不设置长度, 它指定了 255 ,我还以为是 mysql 默认的。误会了好久。
    lalawu
        3
    lalawu  
       Mar 2, 2023
    @yxisenx 哈哈 谢谢 navicat
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5544 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 05:57 · PVG 13:57 · LAX 22:57 · JFK 01:57
    ♥ Do have faith in what you're doing.