Creating a new table with fk's on and existing one that already contains data
I'm actually managing and updating a Webapp which DB is on MySQL Server 5.x and all tables are InnoDB, my problem comes when I need to create a new tables with foreign key that references to an existing one, and which one has live data. When I try to execute the create command it throws the famous errno 1005, The problem is solved if I delete all info of father table , create son table and reload da开发者_开发问答ta on father one (this is for constraints, I think). This will be a pain if father table has a grandfather table a it contains data too.
I was wondering if there is a way to do this task easily, maybe a command that includes ignoring constraints?
From what I can see from the MySQL documentation, you could try using
SET foreign_key_checks = 0
I've come across posts of cases where that doesn't always work.
Your next best option is to make the data conform to what the FK constraint will require, then apply the constraint. That is not necessarily uncommon when adding FK constraints to tables that already have data.
精彩评论