473,511 Members | 15,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add foreign key to existing tables

AMT India
64 New Member
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....
Aug 29 '07 #1
9 4594
pbmods
5,821 Recognized Expert Expert
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.
Aug 29 '07 #2
AMT India
64 New Member
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?
Aug 30 '07 #3
pbmods
5,821 Recognized Expert Expert
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.
Aug 30 '07 #4
AMT India
64 New Member
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?
Aug 31 '07 #5
AMT India
64 New Member
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)
Sep 1 '07 #6
pbmods
5,821 Recognized Expert Expert
Merged duplicate threads.
Sep 1 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, AMT.

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.
Sep 1 '07 #8
mwasif
802 Recognized Expert Contributor
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.
Sep 2 '07 #9
AMT India
64 New Member
Thanx a lot for all the replies...
Sep 3 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
4494
by: Ron | last post by:
Mandatories: Ver 7.3.4, Redhat Linux 8.0, P4, 2GB RAM I want to add a 'nullable' foreign key to a column in a table. I have tables "company" and "project" which may be related by...
10
42382
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
26
14078
by: pb648174 | last post by:
I have a table called BidItem which has another table called BidAddendum related to it by foreign key. I have another table called BidFolder which is related to both BidItem and BidAddendum, based...
3
39967
by: . . | last post by:
Hi I am creating new SQL Server Tables using SQL Server 2005. I have set primary key to the tables .But I do not know how to assign Foreign key to the tables .I need to do some joins later and...
5
3307
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
7
2404
by: PC Datasheet | last post by:
Looking for suggestions ---- A database was designed for a national automobile inspection program. In it's simplest form, the database has two tables: TblOwner OwnerID <Year/Make/Model owned...
3
5327
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
9
3892
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
5
4562
by: kutty | last post by:
Hi All, I am loading data to a child table from a text file. the text files also contains data not referenced by parent key. while loading the data if one row fails to satisfies the constraint...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.