Connecting Tech Pros Worldwide Forums | Help | Site Map

Add foreign key to existing tables

AMT India's Avatar
Member
 
Join Date: Feb 2007
Posts: 64
#1: Aug 29 '07
I am using mysql 4.2. I have a big database with lots of data. Now there is no foreign key relationship between the tables. But every one has a primary key. Can I alter these tables to bring foreign key without affecting the data? Thanx in advance....

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Aug 29 '07

re: Add foreign key to existing tables


Heya, AMT.

Are these tables MyISAM or InnoDB?

If you're not sure, try issuing a
Expand|Select|Wrap|Line Numbers
  1. SHOW CREATE TABLE `tableName`\G
.

Look for ENGINE=MyISAM or ENGINE=InnoDB.
AMT India's Avatar
Member
 
Join Date: Feb 2007
Posts: 64
#3: Aug 30 '07

re: Add foreign key to existing tables


Quote:

Originally Posted by pbmods

Heya, AMT.

Are these tables MyISAM or InnoDB?

If you're not sure, try issuing a

Expand|Select|Wrap|Line Numbers
  1. SHOW CREATE TABLE `tableName`\G
.

Look for ENGINE=MyISAM or ENGINE=InnoDB.

Some of them are MyISAM and some are InnoDB...what I have to do?
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Aug 30 '07

re: Add foreign key to existing tables


Heya, AMT.

Only InnoDB tables properly support foreign key constraints right now. You can change the storage engine for any table by executing this command:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `tableName` ENGINE=InnoDB;
  2.  
MyISAM is faster, especially for large data sets, than InnoDB. The trade-off is that MyISAM does not support row locking, transactions nor foreign key constraints.

Check out this document for more information on setting up foreign key constraints on an InnoDB table.
AMT India's Avatar
Member
 
Join Date: Feb 2007
Posts: 64
#5: Aug 31 '07

re: Add foreign key to existing tables


Quote:

Originally Posted by pbmods

Heya, AMT.

Only InnoDB tables properly support foreign key constraints right now. You can change the storage engine for any table by executing this command:

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `tableName` ENGINE=InnoDB;
  2.  
MyISAM is faster, especially for large data sets, than InnoDB. The trade-off is that MyISAM does not support row locking, transactions nor foreign key constraints.

Check out this document for more information on setting up foreign key constraints on an InnoDB table.

Ok..i will change tables to InnoDB. But can I add foreign key for a table which contains data?
AMT India's Avatar
Member
 
Join Date: Feb 2007
Posts: 64
#6: Sep 1 '07

re: Add foreign key to existing tables


I have two tables with lots of data. How can I specify foreign keys for these tables in MySQL 4. (Both tables has primary key)
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#7: Sep 2 '07

re: Add foreign key to existing tables


Merged duplicate threads.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#8: Sep 2 '07

re: Add foreign key to existing tables


Heya, AMT.

Quote:

Originally Posted by AMT India

Ok..i will change tables to InnoDB. But can I add foreign key for a table which contains data?

You sure can. Simply use the ALTER TABLE command, and add foreign keys the same way you would any other.
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#9: Sep 2 '07

re: Add foreign key to existing tables


Quote:

Originally Posted by AMT India

Ok..i will change tables to InnoDB. But can I add foreign key for a table which contains data?

Make sure you do not have FULLTEXT index in any MyISAM table. FULLTEXT is only supported in MyISAM tables.
AMT India's Avatar
Member
 
Join Date: Feb 2007
Posts: 64
#10: Sep 3 '07

re: Add foreign key to existing tables


Thanx a lot for all the replies...
Reply