[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding foreign key constraints to mysql databases

Posted on 2006-06-13
8
Medium Priority
?
2,134 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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 …
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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
Course of the Month18 days, 16 hours left to enroll

834 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