Solved

Adding foreign key constraints to mysql databases

Posted on 2006-06-13
8
2,112 Views
Last Modified: 2008-01-09
Hi, I started off with all of my tables in MyISAM, not realizing that it would not take the constraints I was entering.  Now I am trying to add the constraints either through sql command or through phpMyAdmin interface.  I am kind of confused on a couple of things.  Here is an example of 2 of the tables -

CREATE TABLE `address` (
 `address_id` int(11) NOT NULL auto_increment,
 `street` varchar(60) NOT NULL default '',
 `city` varchar(30) NOT NULL default '',
 `state_id` int(11) default NULL,                       <--- should have been a FK
 `zip` varchar(20) NOT NULL default '',
 `iso` char(2) NOT NULL default '',                    <---- should have been a FK
 PRIMARY KEY  (`address_id`),
 KEY `state_id` (`state_id`)
) TYPE=InnoDB


CREATE TABLE `states` (
 `state_id` int(11) NOT NULL auto_increment,
 `name` char(40) NOT NULL default '',
 `abbrev` char(2) NOT NULL default '',
 PRIMARY KEY  (`state_id`)
) TYPE=InnoDB

So how now do I get 'state_id' in address table to relate to the states table - it also looks liek somehow it has added KEY 'state_id' ('state_id') which I'm not sure how I did that.

The other part of the question is, it says to make sure you add your indexes - I'm not sure how to do that - I tried making an index in myPHPadmin but it will not let me relate it to the states table after that.  When I try and relate it, it says 'no index defined' for state id - but when I go to the states table and try and add the index there, it says that I shouldhn't have an index AND a primary key.  Obviously I am missing something.  Please help.
0
Comment
Question by:PurpleSlade
  • 5
  • 3
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16896391
in the phpMyAdmin it should be pretty easy. once you hav the states table with the state_id field as primary key, and the state_id field in the address table indexed, simply add a foreign key constraints to the table address, on field state_id, pointing to table states (field state_id )
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 16896420
That's what I would have thought, but it's complaining that I have an index and a primary key when I try to make the association.  Also, it says in the documentation that I have read that if the column to which I am referring is a primary key that I shouldn't need an index.  I am confused.
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 16896469
I also tried doing it this way through sql
ALTER TABLE address ADD CONSTRAINT address_state_id_fk FOREIGN KEY ( state_id ) REFERENCES states( state_id ) ;

but now get an error saying:
#1005 - Can't create table './my tabel/#sql-3025_8e114.frm' (errno: 150)
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 16896530
is innodb functionality really enabled on the mysql server?
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 2

Author Comment

by:PurpleSlade
ID: 16896980
Good question - I have a question into my ISP and should find out shortly.  Originally the tables were created in MyISAM and then I changed the type to InnoDB.  Do you think that they need to be created originally in InnoDB?  
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 16897701
Hi, apparently they were running an outdated version of mySql which had some bugs producing the errors.

CREATE TABLE `address` (
 `address_id` int(11) NOT NULL auto_increment,
 `street` varchar(60) NOT NULL default '',
 `city` varchar(30) NOT NULL default '',
 `state_id` int(11) default NULL,
 `zip` varchar(20) NOT NULL default '',
 `iso` char(2) NOT NULL default '',
 PRIMARY KEY  (`address_id`),
 KEY `state_id` (`state_id`),
 KEY `iso` (`iso`),
 CONSTRAINT `address_state_id_fk` FOREIGN KEY (`state_id`) REFERENCES `states` (`state_id`)
 CONSTRAINT `address_iso_fk` FOREIGN KEY (`iso`) REFERENCES `country` (`iso`)
) TYPE=InnoDB

I am going to drop the old table and recreate it as the above.  Does that look ok?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16897926
yes, looks ok
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 16898099
beautiful, thanks.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now