If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to get MSSQL style relationships going?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-12-07, 19:27
MDesigner MDesigner is offline
Registered User
 
Join Date: Jan 2004
Posts: 65
How to get MSSQL style relationships going?

I figured out how to configure phpMyAdmin so I can use the "relation view" to relate tables together via PK/FK. What I can't figure out is how to set up cascade deletes, so if, for example, there's a table full of dog owners, and a table full of dogs, and I delete an owner, I want all the dogs that person owned to also get deleted.

How do I do this using phpMyAdmin? (or if I have to get down & dirty with straight SQL, I can handle that...)
Reply With Quote
  #2 (permalink)  
Old 04-12-07, 23:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
do a SHOW CREATE TABLE on each of your two tables, paste that here, and we can supply the necessary SQL to add your relationship
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-13-07, 00:45
MDesigner MDesigner is offline
Registered User
 
Join Date: Jan 2004
Posts: 65
phpMyAdmin's SQL is cutting off the text result.. so I did an export instead

CREATE TABLE `words` (
`word_id` int(10) unsigned NOT NULL auto_increment,
`word` varchar(30) NOT NULL,
PRIMARY KEY (`word_id`),
KEY `word` (`word`)
) ENGINE=MyISAM

CREATE TABLE `answers` (
`word_id` int(10) unsigned NOT NULL,
`matters` tinyint(3) unsigned NOT NULL,
`feeling` tinyint(3) unsigned NOT NULL
) ENGINE=MyISAM

Is that enough info? So words contains a list of words.. and answers (which is lacking a primary key, I know) has a FK word_id. I'd like it so when I delete a word, all the answers that refer to it are deleted too.
Reply With Quote
  #4 (permalink)  
Old 04-13-07, 05:56
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Yes that's plenty.
What you need to realise is that MySQL only supports Foreign Keys using the InnoDB engine type. This is where your problem is occuring. Specify those tables as follows :

CREATE TABLE `words` (
`word_id` int(10) unsigned NOT NULL auto_increment,
`word` varchar(30) NOT NULL,
PRIMARY KEY (`word_id`),
KEY `word` (`word`)
) ENGINE=InnoDB

CREATE TABLE `answers` (
`word_id` int(10) unsigned NOT NULL,
`matters` tinyint(3) unsigned NOT NULL,
`feeling` tinyint(3) unsigned NOT NULL
FOREIGN KEY (`word_id`) REFERENCES words(`word_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
Reply With Quote
  #5 (permalink)  
Old 04-13-07, 06:07
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
unless you have to use phpmyadmin have you though of using MySQL Administrator form MySQL.......
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 04-13-07, 20:49
MDesigner MDesigner is offline
Registered User
 
Join Date: Jan 2004
Posts: 65
Awesome, thanks guys
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On