If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Dropping foreign keys

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-04-08, 09:13
jleblanc24 jleblanc24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 2
Dropping foreign keys

I need to update our schema to change the definition of some of the primary keys. I am dropping the foreign key constraints but it is taking about 40 minutes to finish. I have enclosed the code I am using. Does anyone know a way to speed this up or a better way to do it?

DECLARE table_crsr cursor for
SELECT table_name, CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS where
table_schema = 'spa' and constraint_type = 'FOREIGN KEY';

SET FOREIGN_KEY_CHECKS = 0;

-- Open the cursor
open table_crsr;

-- Get first table
fetch next from table_crsr into tableName, cName;

-- Loop thru tables
WHILE (done = 0) DO

-- Build the alter table statement
set @query = CONCAT('alter table ' ,tableName, ' drop foreign key ', cName);
PREPARE cCommand FROM @query;

-- Execute the command
EXECUTE cCommand;

-- Get next table
fetch next from table_crsr into tableName, cName;

END WHILE;
Reply With Quote
  #2 (permalink)  
Old 06-04-08, 10:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
my question is, if you are changing the definition of only some of the primary keys, why do you need to obliterate all foreign keys in the database?

why not just drop only the foreign keys that reference the primary keys that are changing?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-04-08, 10:56
jleblanc24 jleblanc24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 2
The some should actually be most. We are cutting the size of the primary key field from 8 to 4 bytes.
Reply With Quote
  #4 (permalink)  
Old 06-04-08, 11:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
if you're only going to do this once, and you already know it takes 40 minutes, then haven't you already done it?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On