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

2023-03-02 18:06:52 +08:00
 yxisenx

报错 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

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

610 次点击
所在节点    问与答
3 条回复
lalawu
2023-03-02 18:09:29 +08:00
varchar 字段在声明的时候是一定要指定长度的,不然 mysql 不知道你这变长字符串最长多长呀;
还有建议如果设置了 not null 最好加上 default 值
yxisenx
2023-03-02 18:12:11 +08:00
@lalawu 十分感谢。 我之前在 navicat 建表的时候不设置长度, 它指定了 255 ,我还以为是 mysql 默认的。误会了好久。
lalawu
2023-03-02 19:57:56 +08:00
@yxisenx 哈哈 谢谢 navicat

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/920590

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX