简体   繁体   中英

INNODB cascade on delete and update

I'm trying to create some tables in a mysql db to handle customers, assign them to groups and give customers within these groups unique promotion codes/coupons.

there are 3 parent(?) tables - customers, groups, promotions then I have table - customerGroups to assign each customer_id to many group_id's also I have - customerPromotions to assign each customer_id to many promotion_id's

I know I need to use cascade on delete and update so that when I delete a customer, promotion or group the data is also removed from the child tables. I put together some php to create the tables easily http://pastebin.com/gxhW1PGL

I've been trying to read up on cascade, foreign key references but I think I learn better by trying to do things then learning why they work. Can anyone please give me their input on what I should do to these tables to have them function correctly.

I would like to have the database and tables set up correctly before I start with queries or anything further so any advice would be great.

You seem to want just a little guidance. So I'll try to be brief.

$sql = "CREATE TABLE customerGroups (
   customer_id int(11) NOT NULL,
   group_id int(11) NOT NULL,
   PRIMARY KEY (customer_id, group_id),
   CONSTRAINT customers_customergroups_fk
     FOREIGN KEY (customer_id)
     REFERENCES customers (customer_id) 
     ON DELETE CASCADE,
   CONSTRAINT groups_customergroups_fk
     FOREIGN KEY (group_id)
     REFERENCES groups (group_id) 
     ON DELETE CASCADE
)ENGINE = INNODB;";

You only need id numbers when identity is hard to nail down. When you're dealing with people, identity is hard to nail down. There are lots of people named "John Smith".

But you're dealing with two things that have already been identified. (And identified with id numbers, of all things.)

Cascading deletes makes sense. It's relatively rare to cascade updates on id numbers; they're presumed to never change. (The main reason Oracle DBAs insist that primary keys must always be ID numbers, and that they must never change is because Oracle can't cascade updates.) If, later, some id numbers need to change for whatever reason, you can alter the table to include ON UPDATE CASCADE.

$sql = "CREATE TABLE groups
(
group_id int(11) NOT NULL AUTO_INCREMENT,
group_title varchar(50) NOT NULL UNIQUE,
group_desc varchar(140),
PRIMARY KEY (group_id)
)ENGINE = INNODB;";

Note the additional unique constraint on group_title. You don't want to allow anything like this (below) in your database.

group_id  group_title
--
1         First group
2         First group
3         First group
...
9384      First group

You'll want to carry those kinds of changes through all your tables. (Except, perhaps, your table of customers.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM