yxisenx
V2EX  ›  问与答

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

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