Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Adding foreign key constraints to mysql databases

Posted on 2006-06-13
8
2,122 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 143

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 16896530
is innodb functionality really enabled on the mysql server?
0
 
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 143

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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