MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 27th, 2004, 11:55 PM
Ieshna Ieshna is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Ieshna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Foreign Key Issue. Driving me nuts...

I have this one associative table which MySQL Does not like (getting an errno 150). I have no idea why there is an issue with the foreign key. I have been trying to find the problems for some time, but with no luck. Can anyone spot any problems with the foreign key statements in the MovieActor table? (BTW, I am using MySQLCC to run the SQL statements, so might there be an issue there ? )

CREATE TABLE Gaurav.Movie (
MovieID int(10) unsigned NOT NULL auto_increment,
CategoryID int(10) unsigned NOT NULL,
MovieName char(100) NOT NULL,
MovieType ENUM('VHS', 'DVD') NOT NULL default 'VHS',
RentalDuration int(10) unsigned NOT NULL,
DisplayWeb ENUM('TRUE', 'FALSE') NOT NULL default 'FALSE',
Producer char(100),
Director char(100),
Language ENUM('Hindi', 'Punjabi', 'Gujrati', 'Urdu', 'Other') NOT NULL,
ReleaseDate DATE,
Rated ENUM('G','R') NOT NULL default 'G',
ArrivalDate DATE,

PRIMARY KEY (MovieID),
FOREIGN KEY (CategoryID) REFERENCES Gaurav.MovieCategory(CategoryID)
ON UPDATE CASCADE ON DELETE RESTRICT,
UNIQUE (CategoryID),
UNIQUE (MovieID)
) TYPE=InnoDB;

CREATE TABLE Gaurav.Actor (
ActorID int(10) unsigned NOT NULL auto_increment,
FirstName char(100) NOT NULL default '',
LastName char(100) NOT NULL default '',
DateOfBirth DATE,

PRIMARY KEY (ActorID),
UNIQUE (ACtorID)
) TYPE=InnoDB;

CREATE TABLE Gaurav.MovieActor (
ActorID int(10) unsigned NOT NULL,
MovieID int(10) unsigned NOT NULL,

PRIMARY KEY (ActorID, MovieID),
#UNIQUE (ActorID, MovieID),
FOREIGN KEY (ActorID) REFERENCES Gaurav.Actor(ActorID)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (MovieID) REFERENCES Gaurav.Movie(MovieID)
ON UPDATE CASCADE ON DELETE RESTRICT
) TYPE=InnoDB;

** PROBLEM LIES WITH THE 2nd foreign key statement in MovieActor table. **

Any help will be appreciated.

Reply With Quote
  #2  
Old July 28th, 2004, 08:14 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Maybe it has something to do with the fact that both the MovieActor and the Movie tables have a key named "MovieID." To test it out, try renaming one of the fields and adjusting the foreign key as needed.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #3  
Old July 28th, 2004, 12:35 PM
Ieshna Ieshna is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Ieshna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by dhouston
Maybe it has something to do with the fact that both the MovieActor and the Movie tables have a key named "MovieID." To test it out, try renaming one of the fields and adjusting the foreign key as needed.

Okay, ill try that out, but im curious as to why the foreign key reference to ActorID works fine. If I comment out the second reference there are no errors.

Reply With Quote
  #4  
Old July 28th, 2004, 12:41 PM
Ieshna Ieshna is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Ieshna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
No, changing the names of the fields in the MovieActor table so that they are different from the name of the keys they reference doesnt help. Still getting the same referential integrity problem.

Reply With Quote
  #5  
Old July 28th, 2004, 12:46 PM
Ieshna Ieshna is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Ieshna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I noticed something else. The foreign key reference to Actor(ActorID) is fine, while the one to Movie(MovieID) is not. That happens then the PK and uniques are defined in the table as follows:

...
PRIMARY KEY (ActorID, MovieID),
UNIQUE (ActorID, MovieID),
...

But if you switch around the order of the two fields in these statements so that they read:

...
PRIMARY KEY (MovieID, ActorID),
UNIQUE (MovieID, ActorID),
...

Then all of a sudden the MovieID foreign reference is fine, and the ActorID one now is causing the issue...

Reply With Quote
  #6  
Old July 28th, 2004, 02:55 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Duh, shoulda noticed that you were defining two primary keys. By definition, there can be only one, right? That explains why it's always the second one that gives you the error.

Reply With Quote
  #7  
Old July 28th, 2004, 05:49 PM
Ieshna Ieshna is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Ieshna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by dhouston
Duh, shoulda noticed that you were defining two primary keys. By definition, there can be only one, right? That explains why it's always the second one that gives you the error.

Nope, theres only 1 primary key. Its a concatenation of the two foreign keys since it needs to be unique. So thats not the problem. I did find a method that made it work though. The following creates the table with no problems:

CREATE TABLE Gaurav.MovieActor (
ActorID int(10) unsigned NOT NULL references Actor(ActorID) on update cascade on delete restrict,
MovieID int(10) unsigned NOT NULL references Movie(MovieID) on update cascade on delete cascade,
PRIMARY KEY (ActorID, MovieID),
UNIQUE (ActorID, MovieID)
) TYPE=InnoDB;

It would appear that as long as you put the references and the integrities in the same declaration as the fields themselves, there is no problem. Thx for the help guys.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Foreign Key Issue. Driving me nuts...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT