Changing MySQL primary key when foreign key contraints exist

前端 未结 2 483
暗喜
暗喜 2020-12-20 16:02

I have two already-existing tables which look (in part) roughly like this:

CREATE TABLE parent (
    old_pk CHAR(8) NOT NULL PRIMARY KEY
) ENGINE=InnoDB;

CR         


        
2条回答
  •  一个人的身影
    2020-12-20 17:00

    I'll weigh in on this with what may be an unpopular suggestion. Don't use foreign key constraints in your database - enforce unique key and other constraints via TSQL in stored procedures as needed. It's my experience that in scaled environments check constraints are rarely used.

    I say this with an open mind to opposing comments/discussion that may ensue. I'm not saying this suggestion is correct, just that it has been the prevailing opinion in the shops I've worked in.

    A Request: If you downvote me, please also leave a short comment as well. In the 10 or so years I've been working with relational databases, the only people I know who use check constraints are working on systems that aren't at scale. If those are the people downvoting me then I can live with that. But if you're working on a scaled system and check constraints are the norm for you I'd like to know who you are so I can do some reading to see what I've missed.

提交回复
热议问题