Solved

CASCADE confusion

Posted on 2006-07-10
9
583 Views
Last Modified: 2008-02-01
OK - small background is - I am working in phpMyAdmin with tables.  I have set up the foreign key constraints but have thus far avoided messing with any of the cascade delete stuff because I didn't really understand it thoroughly.  I understand the premise, but wasn't comfortable with messing with things.

Just tried to reload the database though from a mysqldump by dropping the tables if they exist and inserting the new rows, and it won't let me do that because it is saying "Cannot delete or update a parent row: a foreign key constraint fails" - so I know it's that the cascades aren't set.  

SO - I am hoping you can give me a 101 on how to accomplish this based on a couple tables.  If I understand a couple, I think I can get the rest.

The tables are the following: basically an address table that references FK's to a state table and a country table - I guess my question is, is the cascade from the parent table down?  So if I deleted a state in my state table like 'CO' and addresses has some rows in it that reference 'CO', it is going to cascade down to those addresses?  So in my address table I would set ON DELETE CASCADE and then it would delete the entire row in  address when I delete a state?  Am I understanding that correctly?

DROP TABLE IF EXISTS `address`;
CREATE TABLE `address` (
  `address_id` smallint(6) NOT NULL auto_increment,
  `street` varchar(60) NOT NULL default '',
  `city` varchar(30) NOT NULL default '',
  `state_id` smallint(6) default NULL,
  `zip` varchar(20) default NULL,
  `iso` char(2) default NULL,
  PRIMARY KEY  (`address_id`),
  KEY `state_id` (`state_id`),
  KEY `iso` (`iso`)
) TYPE=InnoDB AUTO_INCREMENT=1 ;

DROP TABLE IF EXISTS `country`;
CREATE TABLE `country` (
  `iso` char(2) NOT NULL default '',
  `name` varchar(80) NOT NULL default '',
  `printable_name` varchar(80) NOT NULL default '',
  `iso3` char(3) default NULL,
  `numcode` smallint(6) default NULL,
  PRIMARY KEY  (`iso`)
) TYPE=InnoDB;

DROP TABLE IF EXISTS `states`;
CREATE TABLE `states` (
  `state_id` smallint(6) NOT NULL auto_increment,
  `name` varchar(40) NOT NULL default '',
  `abbrev` char(2) NOT NULL default '',
  PRIMARY KEY  (`state_id`)
) TYPE=InnoDB AUTO_INCREMENT=66 ;
0
Comment
Question by:PurpleSlade
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 17077359
> So in my address table I would set ON DELETE CASCADE and then it would delete the entire row in  address when I delete a state?  >Am I understanding that correctly?
Yes. ON DELETE CASCADE will delete all the "child" records when the "parent" or "master" record gets deleted.
in your case, state is the master of country, which in turn is master of the address table.
0
 
LVL 25

Assisted Solution

by:jrb1
jrb1 earned 100 total points
ID: 17077360
Yes, that's it.  With cascade, if you delete a for from a table which has references to it (like your ADDRESS table referencing a STATE), the system will allow you to delete the STATE, and it will delete any references to that STATE.
0
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 200 total points
ID: 17077397
Solutions to reloading your data:
- Reorder the file so that the address table is populated after the country and states tables.

- Add to the beginning of the import file the following:
  SET FOREIGN_KEY_CHECKS=0
  Add a line to set it back to 1 at the end.
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 17077448
OK then, so 1) the cascades are set in the child tables  and 2) the entire reference is deleted

So if I understand correctly, then in my Address table is where I would make the setting changes to and I wouldn't need to set up any cascades in the state or country table at all:

state_id  which references states->state_id
ON DELETE CASCADE ON UPDATE CASCADE

and

iso  which references country->iso
ON DELETE CASCADE ON UPDATE CASCADE

So is it generally a good idea to set everything to cascade in the child tables?  

Also, you guys are fast.  I have had a mysql/Perl question posted in the Perl section for like 3-4 days without a single response.  If any of you guys use mysql with perl maybe you can check that out too.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17077501
>So is it generally a good idea to set everything to cascade in the child tables?  
this is a choice to be made. with important data I would NOT allow automatic deletion of the child records, but prevent deletion with the foreign key constraints (that's what they are for).
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 17077534
Snoyes, that worked with the first table from the dump, but it looks like it didn't work for the next one -- it created address, but for address_type (the next in line in the dump file) I get the following error:

C:\>mysql -u root -p mydb < mydb.sql
Enter password: ******
ERROR 1005 (HY000) at line 46: Can't create table '.\mydb\address_type
.frm' (errno: 150):

-- phpMyAdmin SQL Dump
-- version 2.8.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 10, 2006 at 05:17 PM
-- Server version: 4.0.27
-- PHP Version: 4.3.11
--
-- Database: `mydb`
--

SET FOREIGN_KEY_CHECKS=0;

-- --------------------------------------------------------

--
-- Table structure for table `address`
--

DROP TABLE IF EXISTS `address`;
CREATE TABLE `address` (
  `address_id` smallint(6) NOT NULL auto_increment,
  `street` varchar(60) NOT NULL default '',
  `city` varchar(30) NOT NULL default '',
  `state_id` smallint(6) default NULL,
  `zip` varchar(20) default NULL,
  `iso` char(2) default NULL,
  PRIMARY KEY  (`address_id`),
  KEY `state_id` (`state_id`),
  KEY `iso` (`iso`)
) TYPE=InnoDB AUTO_INCREMENT=1 ;

--
-- Dumping data for table `address`
--


-- --------------------------------------------------------

--
-- Table structure for table `address_type`
--

DROP TABLE IF EXISTS `address_type`;
CREATE TABLE `address_type` (
  `address_type_id` char(3) NOT NULL default '',
  `description` varchar(20) default NULL,
  PRIMARY KEY  (`address_type_id`)
) TYPE=InnoDB;

--
-- Dumping data for table `address_type`
--

INSERT INTO `address_type` (`address_type_id`, `description`) VALUES ('HMP', 'Primary Home Address');

-- --------------------------------------------------------
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17077548
>this is a choice to be made.
to give you an example of where I would use it:
ORDERS <- ORDER_DETAILS (CASDADE DELETE)
simply deleting the order will delete it's child items along.

but NOT on this:
PRODUCTS <- ORDER DETAILS
you should not be able to delete a product if you have order details on it.
you might "close" the product (make it unavailable), but keep it for reference.

0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 17077572
>So is it generally a good idea to set everything to cascade in the child tables?  
this is a choice to be made. with important data I would NOT allow automatic deletion of the child records, but prevent deletion with the foreign key constraints (that's what they are for).

So then really, I should probably not be changing everything to cascade delete just because it is not dropping the table in a reload.  Ok - I think I understand now.
0
 
LVL 25

Expert Comment

by:jrb1
ID: 17077605
>I should probably not be changing everything to cascade delete just because it is not dropping the table in a reload.

Maybe a solution to this is to drop the table with the foreign key references (ADDRESS) before you drop STATE and COUNTRY, but then load the STATE and COUNTRY tables first, then ADDRESS.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

17 Experts available now in Live!

Get 1:1 Help Now