Skip to content

Instantly share code, notes, and snippets.

@adrianlzt
Created October 17, 2013 10:33
Show Gist options
  • Save adrianlzt/7022669 to your computer and use it in GitHub Desktop.
Save adrianlzt/7022669 to your computer and use it in GitHub Desktop.
Percona XtraDB Cluster error with ON CASCADE. Following this guide: http://www.mysqltutorial.org/mysql-on-delete-cascade/
mysql> use adritest;
Database changed
mysql> CREATE TABLE buildings (
-> building_no int(11) NOT NULL AUTO_INCREMENT,
-> building_name varchar(255) NOT NULL,
-> address varchar(355) NOT NULL,
-> PRIMARY KEY (building_no)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (3.39 sec)
mysql> CREATE TABLE rooms (
-> room_no int(11) NOT NULL AUTO_INCREMENT,
-> room_name varchar(255) NOT NULL,
-> building_no int(11) NOT NULL,
-> PRIMARY KEY (room_no),
-> KEY building_no (building_no),
-> CONSTRAINT rooms_ibfk_1
-> FOREIGN KEY (building_no)
-> REFERENCES buildings (building_no)
-> ON DELETE CASCADE
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.16 sec)
mysql> INSERT INTO buildings(building_name,address)
-> VALUES('ACME Headquaters','3950 North 1st Street CA 95134'),
-> ('ACME Sales','5000 North 1st Street CA 95134');
Query OK, 2 rows affected (0.11 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM buildings;
+-------------+------------------+--------------------------------+
| building_no | building_name | address |
+-------------+------------------+--------------------------------+
| 1 | ACME Headquaters | 3950 North 1st Street CA 95134 |
| 4 | ACME Sales | 5000 North 1st Street CA 95134 |
+-------------+------------------+--------------------------------+
2 rows in set (0.00 sec)
mysql> INSERT INTO rooms(room_name,building_no)
-> VALUES('Amazon',1),
-> ('War Room',1),
-> ('Office of CEO',1),
-> ('Marketing',2),
-> ('Showroom',2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`adritest`.`rooms`, CONSTRAINT `rooms_ibfk_1` FOREIGN KEY (`building_no`) REFERENCES `buildings` (`building_no`) ON DELETE CASCADE)
mysql>
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (3.39 sec)
mysql> CREATE TABLE rooms (
-> room_no int(11) NOT NULL AUTO_INCREMENT,
-> room_name varchar(255) NOT NULL,
-> building_no int(11) NOT NULL,
-> PRIMARY KEY (room_no),
-> KEY building_no (building_no),
-> CONSTRAINT rooms_ibfk_1
-> FOREIGN KEY (building_no)
-> REFERENCES buildings (building_no)
-> ON DELETE CASCADE
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.16 sec)
mysql> INSERT INTO buildings(building_name,address)
-> VALUES('ACME Headquaters','3950 North 1st Street CA 95134'),
-> ('ACME Sales','5000 North 1st Street CA 95134');
Query OK, 2 rows affected (0.11 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM buildings;
+-------------+------------------+--------------------------------+
| building_no | building_name | address |
+-------------+------------------+--------------------------------+
| 1 | ACME Headquaters | 3950 North 1st Street CA 95134 |
| 4 | ACME Sales | 5000 North 1st Street CA 95134 |
+-------------+------------------+--------------------------------+
2 rows in set (0.00 sec)
mysql> INSERT INTO rooms(room_name,building_no)
-> VALUES('Amazon',1),
-> ('War Room',1),
-> ('Office of CEO',1),
-> ('Marketing',2),
-> ('Showroom',2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`adritest`.`rooms`, CONSTRAINT `rooms_ibfk_1` FOREIGN KEY (`building_no`) REFERENCES `buildings` (`building_no`) ON DELETE CASCADE)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.31 |
+-----------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment