Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Last active June 23, 2019 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidayat365/328174121866adaece2d3b6d9143091f to your computer and use it in GitHub Desktop.
Save hidayat365/328174121866adaece2d3b6d9143091f to your computer and use it in GitHub Desktop.
Dasar penggunaan Foreign Key (FK) di database relasional (RDBMS)
Nurs-MacBook-Pro:~ nurhidayat$ /Applications/XAMPP/bin/mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 307
Server version: 10.1.38-MariaDB Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use test
Database changed
MariaDB [test]>
MariaDB [test]> -- header table
MariaDB [test]> -- primary key id
MariaDB [test]> create table orders (
-> id int auto_increment primary key,
-> code varchar(50) not null,
-> order_date int not null default 0,
-> required_date int null,
-> remarks text
-> );
Query OK, 0 rows affected (0.04 sec)
MariaDB [test]>
MariaDB [test]> -- detail table
MariaDB [test]> -- primary key id
MariaDB [test]> -- foreign key refer ke header table
MariaDB [test]> create table order_details (
-> id int auto_increment primary key,
-> order_id int not null,
-> item_id int not null,
-> quantity int not null default 0,
-> unit_price int not null default 0,
-> remarks text,
-> foreign key (order_id) references orders (id)
-> on delete restrict on update cascade
-> );
Query OK, 0 rows affected (0.04 sec)
MariaDB [test]> -- coba insert ke table orders
MariaDB [test]> insert into orders (code, order_date)
-> values ('PO.0001', unix_timestamp(now()))
-> , ('PO.0002', unix_timestamp(now()));
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [test]>
MariaDB [test]> -- lihat isi table orders
MariaDB [test]> select * from orders;
+----+---------+------------+---------------+---------+
| id | code | order_date | required_date | remarks |
+----+---------+------------+---------------+---------+
| 4 | PO.0001 | 1561262020 | NULL | NULL |
| 5 | PO.0002 | 1561262020 | NULL | NULL |
+----+---------+------------+---------------+---------+
2 rows in set (0.01 sec)
MariaDB [test]>
MariaDB [test]> -- coba insert ke table order_details
MariaDB [test]> -- menggunakan nilai order_id dari id di table orders
MariaDB [test]> -- dalam contoh ini, kita pakai order_id = 4
MariaDB [test]> insert into order_details (order_id, item_id, quantity, unit_price)
-> values (4, 1, 10, 10000), (4, 2, 10, 20000), (4, 3, 10, 15000);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [test]>
MariaDB [test]> -- lihat isi table order_details
MariaDB [test]> select * from order_details;
+----+----------+---------+----------+------------+---------+
| id | order_id | item_id | quantity | unit_price | remarks |
+----+----------+---------+----------+------------+---------+
| 1 | 4 | 1 | 10 | 10000 | NULL |
| 2 | 4 | 2 | 10 | 20000 | NULL |
| 3 | 4 | 3 | 10 | 15000 | NULL |
+----+----------+---------+----------+------------+---------+
3 rows in set (0.00 sec)
MariaDB [test]>
MariaDB [test]> -- coba insert ke table order_details
MariaDB [test]> -- menggunakan nilai order_id fiktif
MariaDB [test]> -- kita coba pakai order_id = 9 yg tidak ada di table orders
MariaDB [test]> insert into order_details (order_id, item_id, quantity, unit_price)
-> values (9, 1, 10, 10000);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`test`.`order_details`, CONSTRAINT `order_details_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON UPDATE CASCADE)
MariaDB [test]>
MariaDB [test]> -- perhatikan error message yang muncul karena kita mencoba
MariaDB [test]> -- untuk insert data menggunakan nilai FK order_id, yaitu 2
MariaDB [test]> -- yang tidak ada pasangannya di table orders
MariaDB [test]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment