mysql增加constraint,_查看schema总结

来源:互联网 发布:80端口被系统占用 编辑:程序博客网 时间:2024/06/06 21:08

Mysql增加constraint, 查看schema总结

可参考资料

MYSQL外键(Foreign Key)的使用

指令范例

// 增加constraintalter table [table_name] add constraint FK_tablename1_tablename2 foreign key [columnintable1] references table_name2 (columnintable2);alter table message add constraint FK_message_place foreign key (place_id) references place (place_id);// 删除外键alter table message drop foreign key FK_message_place;alter table message drop key FK_message_place;// 查看schema, 两种方法, 第二种格式更好show create table message;describe message;

遇到的问题

  • Cannot add or update a child row: a foreign key constraint fails
  • 问题原因在于数据库中的表中已有数据不满足新增加限制的要求,所以失败
  • 我的解决方案是把两个相关表的内容都删除掉,再添加限制,理论上可以将相关限制先满足,再添加新限制。
0 0
原创粉丝点击