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 > DB2 > How to drop the foreign keys in a table.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-03-11, 10:22
pratikp.vasani pratikp.vasani is offline
Registered User
 
Join Date: Jul 2011
Posts: 16
How to drop the foreign keys in a table.

I have a table with 7 foreign keys.
How can I drop then and again add them to a table in DB2?
Reply With Quote
  #2 (permalink)  
Old 08-03-11, 10:59
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
ALTER TABLE DROP FOREIGN KEY

ALTER TABLE ADD FOREIGN KEY

Look them up in the manual.

Andy
Reply With Quote
  #3 (permalink)  
Old 08-04-11, 01:53
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
db2look with drop option could help
db2look - DB2 Statistics and DDL extraction tool command
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #4 (permalink)  
Old 08-04-11, 02:17
pratikp.vasani pratikp.vasani is offline
Registered User
 
Join Date: Jul 2011
Posts: 16
Are there any disaadvantages of dropping the foreign keys from the table.
Then deleting all the data from parent table and child table and then again adding the foreign keys.
As by this way it reduces my time of deletion of records from Parent table.
Also if there are any disadvantages in doing so, is there any other way to do the same.
Reply With Quote
  #5 (permalink)  
Old 08-04-11, 02:43
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
what is the purpose of the test / operation and what are you trying todo
please explain clearly whit details.. we can not see what you are thinking..
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #6 (permalink)  
Old 08-04-11, 02:53
pratikp.vasani pratikp.vasani is offline
Registered User
 
Join Date: Jul 2011
Posts: 16
I have two tables Parent table and the Child table.
The Child table has foreign keys refering to the Parent table.
Both the tables contain around 1 lack rows, now I want to delete all the rows in both the tables.
It takes around 1 min for the Child table to be cleared but for Parent table it takes 30 minutes.
So I dropped the Foreign key relation and then the Parent table is also cleared in 1 min.
Then I again assign the foreign keys to the child table before a fresh insertion.
So is this thing has any disadvantages, and is there any other way to perform the above steps. I also tried CASCADE ON DELETE for Child table but then also it is taking 30 minutes to delete the data from both the tables.
Reply With Quote
  #7 (permalink)  
Old 08-04-11, 03:03
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
I would : take db2look - remove foreign keys - clear tables with truncate command or load from /dev/null - reload data - re-execute db2look.. depending on the size and nbr of indexes.. indexes can be dropped or kept during the load
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #8 (permalink)  
Old 08-04-11, 09:46
HostingBee HostingBee is offline
Registered User
 
Join Date: Aug 2011
Location: London, UK
Posts: 3
To drop a FOREIGN KEY constraint, use the following SQL:

MySQL:
ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrders

SQL Server / Oracle / MS Access:
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders
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