Say I have a table Business in mySQL.
The ID in each "business" is a function of title and location. It makes programming much more easy.
I also often merge 2 identical businesses, etc.
Say one day the title of the business change and I want to change the ID. An operation that I will rarely done.
Many other tables point to that business.
So what should I do?
Should I create the new ID and then change all relationship that points to the original business to the new ID?
Is there a mysql command that'll do this faster?
Any sample vb.net code?
Depending on the DB engine in use for each table and their design, you may not have to do anything if you have configured foreign keys and constraints on the tables.
The first thing to do would be to check the structure of the linked tables. To do this, run show create table xyz
within a MySQL session and check:
(Foreign keys are only of any real use when using the InnoDB engine. You can request them in other engines but they won't do anything.)
If you're lucky to be using the InnoDB engine and have constraints in place, you're looking for something like:
CONTRAINT `FK_businessID` FOREIGN KEY(`businessID`) REFERENCES `Business` (`ID`)
ON DELETE CASCADE ON UPDATE CASCADE
in the table definition. If you do see something like this, you should be able to safely change the ID in the business table and all the other linked tables should follow suit.
If you don't have foreign keys but are using the InnoDB engine, change the information in all the tables within a single transaction so that, if anything does go wrong doing the changes outside of your control (connection lost, query error etc) you can roll back.
Finally, if you're not using the InnoDB engine, good luck. You're going to have to risk changing records essentially 'on the fly'. Your main choices (in order of personal preference) are:
Obviously, with all of the above, do lots (and I'm talking lots ) of testing and backups before making your mind up. And do it at a time when the minimum number of customers are using the DB; in other words, overnight.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.