?
Solved

Adding foreign key constraints to mysql databases

Posted on 2006-06-13
8
Medium Priority
?
2,131 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
[X]
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
  • 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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