外键约束求助

DarrenFu1019 2010-06-09 11:23:56
我在《深入浅出MYSQL数据库开发、优化与管理维护》一书中照着抄了一段代码,可是提示出错,怎么找也没找到错误原因,还请各位高手不吝赐教!

书本上抄的代码是:create table city(city_id smallint unsigned not null auto_increment,city varchar(50) not null,country_id smallint unsigned not null,last_update timestamp not null default current_timestamp on update current_timestamp,primary key(city_id),key idx_fk_country_id(country_id),constraint 'fk_city_country' foreign key(country_id) references country(country_id) on delete restrict on update cascade)engine=innodb default charset=utf8;

错误提示:error 1064(42000):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 ''fk_city_country' foreign key(country_id) references country(country_id) on dele' at line 1

请各位高手帮我看看错误出在哪儿,谢谢了!
...全文
74 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2010-06-10
  • 打赏
  • 举报
回复
'fk_city_country'-> `fk_city_country`
DarrenFu1019 2010-06-10
  • 打赏
  • 举报
回复
好了,非常感谢!
iihero_ 2010-06-09
  • 打赏
  • 举报
回复
这个应该是印刷错误,或者写书的人把``括起符弄成''括起符了。,就是把`fk_city_country`弄成'fk_city_country'了。
你copy下边的文本就OK了:
create table city(city_id smallint unsigned not null
auto_increment,city varchar(50) not null,country_id smallint
unsigned not null,last_update timestamp not null default
current_timestamp on update current_timestamp,
primary key(city_id),constraint `fk_city_country` foreign key(country_id)
references country(country_id) on delete restrict on update cascade) engine=innodb
default charset=utf8;
或者不用指定constraint name,使用:
create table city(city_id smallint unsigned not null
auto_increment,city varchar(50) not null,country_id smallint
unsigned not null,last_update timestamp not null default
current_timestamp on update current_timestamp,
primary key(city_id),foreign key(country_id)
references country(country_id) on delete restrict on update cascade) engine=innodb
default charset=utf8;

如:
mysql> create table city(city_id smallint unsigned not null
-> auto_increment,city varchar(50) not null,country_id smallint
-> unsigned not null,last_update timestamp not null default
-> current_timestamp on update current_timestamp,
-> primary key(city_id),foreign key(country_id)
-> references country(country_id) on delete restrict on update cascade) engine=innodb
-> default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧