MySQL在Enote库创建带外键的post表时报SQL语法错误如何解决
错误原因
date字段的default关键字后未指定默认值,且该行末尾多了多余的逗号。timestamp类型如果需要默认写入当前时间,需要明确指定default current_timestamp。uid int not null字段定义行末尾缺少逗号,无法和后续的约束定义做语法分隔。- 主键约束定义与外键约束定义之间缺少逗号分隔。
- 补充说明:
date是MySQL内置关键字,直接作为字段名使用容易触发语法问题,建议用反引号包裹或者更换字段名;另外主键约束名和uid字段重名也容易引发命名冲突,建议修改主键约束名。
解决方案
使用修正后的建表语句执行即可:
create table post (id int not null auto_increment, title varchar(45) not null, content varchar(45) not null, `date` timestamp null default current_timestamp, uid int not null, constraint pk_post primary key (id), foreign key (uid) references user (id) on delete cascade on update cascade);
内容的提问来源于stack exchange,提问作者Shiv Kumar Shah
相关产品推荐
相关产品推荐

