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命令

2013年8月7日星期三

mysql语法命令2

清空所有字段:      truncate table xf_tb
查看key:                show keys from xf_bt
查看索引:           show index from xf_bt
删除索引:           alter table xf_tb drop index idx_us
查看建表语句:      show create table xf_tb
修改表名:           rename table xf_tb to xf_bt
查看是否有其他进程连接:      show process list;
停止,启动mysql服务          service mysql start/stop
重命名数据库:
                         1.进入mysql的bin目录
                         2.进入mysql命令行:create database shop(新数据库名)
                         3.use shop
                         4.\. e:\shop.sql