Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Created October 9, 2012 02:09
Show Gist options
  • Save hidayat365/3856167 to your computer and use it in GitHub Desktop.
Save hidayat365/3856167 to your computer and use it in GitHub Desktop.
Contoh ON DELETE CASCADE
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
D:\xampp\mysql\bin>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.16 MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Database changed
mysql> -- create tabel master
mysql> -- ---------------------------
mysql> create table tbl_master (
-> id int auto_increment,
-> name varchar(100) null,
-> primary key (id)
-> );
Query OK, 0 rows affected (0.17 sec)
mysql> -- create tabel details
mysql> -- ---------------------------
mysql> create table tbl_details (
-> id int auto_increment,
-> master_id int not null,
-> name varchar(100) null,
-> primary key (id),
-> foreign key (master_id)
-> references tbl_master (id) on delete cascade
-> );
Query OK, 0 rows affected (0.17 sec)
mysql> -- insert data ke tabel master
mysql> -- ---------------------------
mysql> insert into tbl_master (name)
-> select 'Amir' union all
-> select 'Budi' ;
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from tbl_master;
+----+------+
| id | name |
+----+------+
| 1 | Amir |
| 2 | Budi |
+----+------+
2 rows in set (0.00 sec)
mysql> -- insert data ke tabel details
mysql> -- ---------------------------
mysql> insert into tbl_details (master_id, name)
-> select 1, 'Anak pertama Amir' union all
-> select 1, 'Anak kedua Amir' union all
-> select 1, 'Anak ketiga Amir' union all
-> select 2, 'Anak pertama Budi' union all
-> select 2, 'Anak kedua Budi' ;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from tbl_details;
+----+-----------+-------------------+
| id | master_id | name |
+----+-----------+-------------------+
| 1 | 1 | Anak pertama Amir |
| 2 | 1 | Anak kedua Amir |
| 3 | 1 | Anak ketiga Amir |
| 4 | 2 | Anak pertama Budi |
| 5 | 2 | Anak kedua Budi |
+----+-----------+-------------------+
5 rows in set (0.00 sec)
mysql> -- sekarang coba kita hapus 'Amir' dari master
mysql> -- -------------------------------------------
mysql> delete from tbl_master
-> where name = 'Amir' ;
Query OK, 1 row affected (0.03 sec)
mysql> select * from tbl_master;
+----+------+
| id | name |
+----+------+
| 2 | Budi |
+----+------+
1 row in set (0.00 sec)
mysql> -- terlihat row 'Amir' sudah terhapus
mysql> -- sekarang kita lihat apakah anak2nya 'Amir' ikut terhapus
mysql> -- --------------------------------------------------------
mysql> select * from tbl_details;
+----+-----------+-------------------+
| id | master_id | name |
+----+-----------+-------------------+
| 4 | 2 | Anak pertama Budi |
| 5 | 2 | Anak kedua Budi |
+----+-----------+-------------------+
2 rows in set (0.00 sec)
mysql> -- YES !!!
mysql> -- -------
mysql>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment