2013年8月8日星期四

mysql命令语法3


  • 添加外键语法:


alter table t_user add constraint u_paid_id2p_id
foreign key (u_paid_id)
references t_paid (p_id) 
on delete cascade on update cascade;

注意key后面不要写表名,如果写成这样:

alter table t_user add constraint u_paid_id2p_id
foreign key t_user(u_paid_id)
references t_paid (p_id)
on delete cascade on update cascade;

会报错:ERROR 1061 (42000): Duplicate key name 't_user'
原因是mysql会把表名't_user'当成外键名称,然后再次新建外键时候会报重复外键的错,具体信息请参看
mysql>show innodb status;
找到LATEST FOREIGN KEY ERROR一行

  • 删除外键出错
ERROR:Error on rename of "xxxx" to "xxx"(error:152)
解决方法:正确删除索引后在删除key
出错原因:使用了错误的外键名称,详见show innodb status命令

没有评论:

发表评论