Solved

Adding foreign key constraints to mysql databases

Posted on 2006-06-13
8
2,115 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

11 Experts available now in Live!

Get 1:1 Help Now