3

I'm developing a new system from an old system. The new system is using MySQL and java. I want to start with a reduced number of tables. When I delete a table lets say X, how can I cause all references to X to be deleted as well, so if table Y has an FK to table X then on table Y the FK and the column used in the FK get deleted as well?

simplified example:

CREATE TABLE `Y` (
    `yID` int(11) NOT NULL AUTO_INCREMENT,
    `yName` varchar(50) NOT NULL,
    ...
   ) ENGINE=InnoDB;

CREATE TABLE `user` (
    `userID` int(11) NOT NULL AUTO_INCREMENT,
    `userName` varchar(50) NOT NULL,
    `givenName` varchar(50) DEFAULT NULL,
    `sourceYID` int(11) NOT NULL,
    CONSTRAINT `USER_FK_sourceYID` FOREIGN KEY (`sourceYID`) REFERENCES `Y` (`yID`)
    ) ENGINE=InnoDB;

I would like to preferably issue one command that will

DROP TABLE `Y`

and on the user table

  • remove the CONSTRAINT USER_FK_sourceYID
  • remove the column sourceYID
  • remove any KEY/INDEX definitions based on sourceYID as well if included (not included in this example)
djm.im
  • 3,295
  • 4
  • 30
  • 45
melutovich
  • 372
  • 1
  • 3
  • 15
  • I was not clear, at this stage I will be working with a reduced existing database structure, but I've exported the dump of the sql file without any data. I am hoping for an automatic way to delete on the referencing table the foreign keys and the column(s) which the foreign keys relate to. – melutovich Nov 19 '12 at 12:02

3 Answers3

2

There is no single command that can do this. The simplest way to handle this is to drop the constraint and then drop the parent table. Without the constraint, you can do this freely.

ALTER TABLE `user` DROP FOREIGN KEY `USER_FK_sourceYID`;
DROP TABLE `Y`;

Dropping the column automatically removes it from any indexes it belongs to. Even if it's a compound index, it leaves an index with the remaining columns. Here are some hypothetical example indexes, and we'll see what happens when we remove the column:

CREATE INDEX y1 ON `user` (sourceYID);
CREATE INDEX y2 ON `user` (userID, sourceYID);
CREATE INDEX y3 ON `user` (sourceYID, userID);

ALTER TABLE `user` DROP COLUMN `sourceYID`;

The result is that index y1 is gone, and both y2 and y3 are reduced to single-column indexes containing just the userID column:

SHOW CREATE TABLE `user`\G

CREATE TABLE `user` (
  `userID` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(50) NOT NULL,
  `givenName` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`userID`),
  KEY `y2` (`userID`),
  KEY `y3` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Therefore these two are now identical indexes, and you should run pt-duplicate-key-checker to analyze your schema for such cases.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Thanks that helps some. The old system is around 175 tables, and my first pass at the new system will be just 20 of those, so I'm looking for a way to automate this. I've sent an email to my new employer asking if there are any naming conventions that were used in the old system which might help. – melutovich Nov 19 '12 at 20:05
1
SET FOREIGN_KEY_CHECKS = 0;
drop table if exists <your_1st_table>;
drop table if exists <your_2nd_table>;
SET FOREIGN_KEY_CHECKS = 1;
samivic
  • 1,216
  • 14
  • 13
  • 6
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Aug 10 '18 at 10:15
  • 1
    @samivic sorry you misunderstood the question simply speaking I'm not looking to drop "your_2nd_table" but have the columns referring to "your_1st_table" to be removed. – melutovich Aug 10 '18 at 13:30
  • ok, i think in this case you have to disable the foreign key clause SET FOREIGN_KEY_CHECKS = 0; – samivic Aug 14 '18 at 09:39
0

If you have a foreign key, then you wan't be able to remove parent table, the foreign key relation won't allow it. To do this you should do these steps:

  • remove all children records, there are two ways: using ON DELETE CASCADE foreign key option, or using multi-table DELETE statement.
  • remove foreign key if it exists.
  • drop parent table.
Devart
  • 119,203
  • 23
  • 166
  • 186
  • I was not clear, at this stage I will be working with a reduced existing database structure, but I've exported the dump of the sql file without any data. I am hoping for an automatic way to delete on the referencing table the foreign keys and the column(s) which the foreign keys relate to. – melutovich Nov 19 '12 at 12:04
  • Can you add more information (what you have and what you want)? – Devart Nov 19 '12 at 12:16
  • I've added an example on the question. – melutovich Nov 19 '12 at 19:31