Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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, 10: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, 11:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,539
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

pre-order my book Simply SQL from Amazon
Reply With Quote
  #3 (permalink)  
Old 06-04-08, 11: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, 12:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,539
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

pre-order my book Simply SQL from Amazon
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

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