Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CASCADE confusion

Posted on 2006-07-10
9
Medium Priority
?
597 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 800 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 400 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 800 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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
 
LVL 143

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 143

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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 …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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

636 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