LANGKAH MEMBUAT RELASI TABEL MENGGUNAKAN VIEW MYSQL
mysql> use sekolah;
Database changed
+-------------------+
| Tables_in_sekolah |
+-------------------+
| mapel |
| nilai |
| siswa |
| v_nilai |
+-------------------+
4 rows in set (0.06 sec)
mysql> desc siswa;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| nis | varchar(4) | NO | PRI | NULL | |
| nm_siswa | varchar(25) | NO | | NULL | |
| alm_siswa | varchar(25) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> desc mapel;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| kd_mapel | varchar(8) | NO | PRI | NULL | |
| nm_mapel | varchar(25) | NO | | NULL | |
| jam_mapel | int(5) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> desc nilai
-> ;
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| nis | varchar(4) | NO | MUL | NULL | |
| kd_mapel | varchar(8) | NO | MUL | NULL | |
| nilai | int(3) | YES | | NULL | |
+----------+------------+------+-----+---------+----------------+
4 rows in set (0.08 sec)
-> as select
-> nilai.id as "ID",
-> nilai.nis as "NIS",
-> siswa.nm_siswa as "NAMA_SISWA",
-> nilai.kd_mapel as "KODE_MAPEL",
-> mapel.nm_mapel as "NAMA_MAPEL",
-> nilai.nilai as "NILAI"
-> FROM
-> siswa,mapel,nilai
-> where nilai.nis = siswa.nis and nilai.kd_mapel=mapel.kd_mapel
-> ;
Query OK, 0 rows affected (0.09 sec)
mysql> desc v_nilai;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| ID | int(11) | NO | | 0 | |
| NIS | varchar(4) | NO | | NULL | |
| NAMA_SISWA | varchar(25) | NO | | NULL | |
| KODE_MAPEL | varchar(8) | NO | | NULL | |
| NAMA_MAPEL | varchar(25) | NO | | NULL | |
| NILAI | int(3) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
mysql> select * from V_nilai;
+----+------+------------+------------+-------------+-------+
| ID | NIS | NAMA_SISWA | KODE_MAPEL | NAMA_MAPEL | NILAI |
+----+------+------------+------------+-------------+-------+
| 12 | 1001 | ikhwan | mat-01 | Matematika1 | 75 |
| 13 | 5555 | khanza | mat-01 | Matematika1 | 78 |
+----+------+------------+------------+-------------+-------+
2 rows in set (0.00 sec)
mysql> select * from siswa;
+------+----------+------------+
| nis | nm_siswa | alm_siswa |
+------+----------+------------+
| 1001 | ikhwan | pekalongan |
| 5555 | khanza | batang |
+------+----------+------------+
2 rows in set (0.00 sec)
mysql> select * from mapel;
+----------+-------------+-----------+
| kd_mapel | nm_mapel | jam_mapel |
+----------+-------------+-----------+
| mat-01 | Matematika1 | 4 |
+----------+-------------+-----------+
1 row in set (0.00 sec)
mysql> select * from nilai;
+----+------+----------+-------+
| id | nis | kd_mapel | nilai |
+----+------+----------+-------+
| 12 | 1001 | mat-01 | 75 |
| 13 | 5555 | mat-01 | 78 |
+----+------+----------+-------+
2 rows in set (0.00 sec)
mysql> select * from v_nilai;
+----+------+------------+------------+-------------+-------+
| ID | NIS | NAMA_SISWA | KODE_MAPEL | NAMA_MAPEL | NILAI |
+----+------+------------+------------+-------------+-------+
| 12 | 1001 | ikhwan | mat-01 | Matematika1 | 75 |
| 13 | 5555 | khanza | mat-01 | Matematika1 | 78 |
+----+------+------------+------------+-------------+-------+
2 rows in set (0.00 sec)
mysql>
1. Buka Database
mysql> use sekolah;
Database changed
2. Lihat Tabel
mysql> show tables;+-------------------+
| Tables_in_sekolah |
+-------------------+
| mapel |
| nilai |
| siswa |
| v_nilai |
+-------------------+
4 rows in set (0.06 sec)
3. Lihat Struktur Tabel
mysql> desc siswa;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| nis | varchar(4) | NO | PRI | NULL | |
| nm_siswa | varchar(25) | NO | | NULL | |
| alm_siswa | varchar(25) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> desc mapel;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| kd_mapel | varchar(8) | NO | PRI | NULL | |
| nm_mapel | varchar(25) | NO | | NULL | |
| jam_mapel | int(5) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> desc nilai
-> ;
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| nis | varchar(4) | NO | MUL | NULL | |
| kd_mapel | varchar(8) | NO | MUL | NULL | |
| nilai | int(3) | YES | | NULL | |
+----------+------------+------+-----+---------+----------------+
4 rows in set (0.08 sec)
4. Buatlah Tabel VIEW (Table baru penggabungan 2 tabel yang terelasi)
mysql> create view V_Nilai-> as select
-> nilai.id as "ID",
-> nilai.nis as "NIS",
-> siswa.nm_siswa as "NAMA_SISWA",
-> nilai.kd_mapel as "KODE_MAPEL",
-> mapel.nm_mapel as "NAMA_MAPEL",
-> nilai.nilai as "NILAI"
-> FROM
-> siswa,mapel,nilai
-> where nilai.nis = siswa.nis and nilai.kd_mapel=mapel.kd_mapel
-> ;
Query OK, 0 rows affected (0.09 sec)
5. Cek Hasilnya :
mysql> desc v_nilai;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| ID | int(11) | NO | | 0 | |
| NIS | varchar(4) | NO | | NULL | |
| NAMA_SISWA | varchar(25) | NO | | NULL | |
| KODE_MAPEL | varchar(8) | NO | | NULL | |
| NAMA_MAPEL | varchar(25) | NO | | NULL | |
| NILAI | int(3) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
mysql> select * from V_nilai;
+----+------+------------+------------+-------------+-------+
| ID | NIS | NAMA_SISWA | KODE_MAPEL | NAMA_MAPEL | NILAI |
+----+------+------------+------------+-------------+-------+
| 12 | 1001 | ikhwan | mat-01 | Matematika1 | 75 |
| 13 | 5555 | khanza | mat-01 | Matematika1 | 78 |
+----+------+------------+------------+-------------+-------+
2 rows in set (0.00 sec)
6. Bandingkan dengan tabel sebelumnya
mysql> select * from siswa;
+------+----------+------------+
| nis | nm_siswa | alm_siswa |
+------+----------+------------+
| 1001 | ikhwan | pekalongan |
| 5555 | khanza | batang |
+------+----------+------------+
2 rows in set (0.00 sec)
mysql> select * from mapel;
+----------+-------------+-----------+
| kd_mapel | nm_mapel | jam_mapel |
+----------+-------------+-----------+
| mat-01 | Matematika1 | 4 |
+----------+-------------+-----------+
1 row in set (0.00 sec)
mysql> select * from nilai;
+----+------+----------+-------+
| id | nis | kd_mapel | nilai |
+----+------+----------+-------+
| 12 | 1001 | mat-01 | 75 |
| 13 | 5555 | mat-01 | 78 |
+----+------+----------+-------+
2 rows in set (0.00 sec)
mysql> select * from v_nilai;
+----+------+------------+------------+-------------+-------+
| ID | NIS | NAMA_SISWA | KODE_MAPEL | NAMA_MAPEL | NILAI |
+----+------+------------+------------+-------------+-------+
| 12 | 1001 | ikhwan | mat-01 | Matematika1 | 75 |
| 13 | 5555 | khanza | mat-01 | Matematika1 | 78 |
+----+------+------------+------------+-------------+-------+
2 rows in set (0.00 sec)
mysql>
Komentar
Posting Komentar