BASISDATA
TABEL dan MANIPULASI DATA
LABORATORIUM 4 BASISDATA
MENU PART INI
CODE :
mysql> create table t_pegawai(id_pegawai varchar(12) primary key, nama_peg
varchar(50), alamat_peg varchar(50));
Query OK, 0 rows affected (0.07 sec)
LIHAT DESKRIPSINYA
mysql> desc t_pegawai;
+------------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id_peg
| varchar(12) | NO
| PRI | NULL
|
|
| nama_peg
| varchar(50) | YES |
| NULL
|
|
| alamat_peg | varchar(50) | YES |
| NULL
|
|
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
ISI TABELNYA
mysql> select*from t_pegawai;
+---------+----------+------------+
| id_peg | nama_peg | alamat_peg |
+---------+----------+------------+
| HRD-001 | Chandra | Jakarta
|
| HRD-002 | Cindy
| Bandung
|
+---------+----------+------------+
2 rows in set (0.00 sec)
LIHAT ERRORNYA..
mysql> insert into t_pegawai values('HRD-001','Budi','Jogja');
ERROR 1062 (23000): Duplicate entry 'HRD-001' for key 'PRIMARY'
memberikan
batasan
bahwa
nilai
dalam
sebuah
kolom
harus
sama)
CODE:
mysql>
create
table
t_parkir(id_parkir
varchar(5)
primary
key,
plat_no
LIHAT DESKRIPSINYA
mysql> desc t_parkir;
+-----------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id_parkir | varchar(5) | NO
| PRI | NULL
|
|
| plat_no
| varchar(12) | YES | UNI | NULL
|
|
| merk
| varchar(10) | YES |
| NULL
|
|
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
ISI TABEL
mysql> select*from t_parkir;
+-----------+------------+--------+
| id_parkir | plat_no
| merk
|
+-----------+------------+--------+
| 11101
| AB-1234-OP | TOYOTA |
| 11102
| B-3454-SDE | BMW
|
+-----------+------------+--------+
2 rows in set (0.00 sec)
LIHAT ERRORNYA..
mysql> insert into t_parkir values('11103','AB-1234-OP','JAGUAR');
ERROR 1062 (23000): Duplicate entry 'AB-1234-OP' for key 'plat_no'
dengan nama
yang
| t_pegawai
+-------------------+
2 rows in set (0.00 sec)
CODE..
mysql> create table IF NOT EXISTS t_pegawai(id_peg varchar(8) primary key,
nama_peg varchar(50), alamat_peg varchar(50));
Query OK, 0 rows affected, 1 warning (0.00 sec)
Duplicates: 0
Warnings: 0
+-------+--------+------+-----+---------+-------+
| id
| int(8) | YES
| NULL
+-------+--------+------+-----+---------+-------+
1 row in set (0.01 sec)
Jika kita lihat dengan perintah SHOW TABLES maka temporary tabel tidak akan
muncul
mysql> show tables;
+-------------------+
| Tables_in_minggu2 |
+-------------------+
| t_parkir
| t_parkir_copy
| t_pegawai
+-------------------+
3 rows in set (0.00 sec)
Tetapi kita bisa menginputkan nilai ke temporary table, contohnya :
mysql> insert into t_temporary values('11100010');
Query OK, 1 row affected (0.03 sec)
mysql> insert into t_temporary values('11100011');
Query OK, 1 row affected (0.02 sec)
mysql> insert into t_temporary values('11100012');
Query OK, 1 row affected (0.02 sec)
mysql> insert into t_temporary values('11100013');
Query OK, 1 row affected (0.01 sec)
mysql> select*from t_temporary;
+----------+
| id
|
+----------+
| 11100010 |
| 11100011 |
| 11100012 |
| 11100013 |
+----------+
4 rows in set (0.00 sec)
Tapi jika kita matikan atau tutup MySQL kita maka temporary tabel akan langsung
hilang..
mysql> \q
bye
Database changed
mysql> select*from t_temporary;
ERROR 1146 (42S02): Table 'minggu2.t_temporary' doesn't exist
satu atau beberapa kolom pada table yang merupakan primary key pada
table satu namun diletakan pada table dimana tablenya berelasi dengan
table dirinya
tabel induk
tabel anak
`t_stok_ibfk_1`
FOREIGN
KEY
(`id_mobil`)
Maksudnya adalah tidak dapat menghapus atau mengupdate kolom pada table
induk karena bereferensi pada table t_stok.
C. SET NULL
Nilai-nilai dalam kolom yang mengkait dari tabel anak diset ke NULL saat
baris-baris dengan data terkait dalam tabel induk dihapus dari tabel induk atau
ketika data terkait dalam tabel induk diupdate. Untuk menggunakan opsi ini, semua
kolom-kolom yang mengkait dalam tabel anak harus mengijinkan nilai NULL.
D. NO ACTION
Tidak ada aksi yang diambil dalam tabel anak ketika baris-baris dihapus dari
tabel induk atau nilai-nilai dalam kolom terkait dalam tabel induk diupdate.
E. SET DEFAULT
Nilai-nilai dalam kolom-kolom yang mengkait dari tabel anak diset ke nilai
default mereka ketika baris-baris dihapus dari tabel induk atau kolom terkait dari
tabel induk diupdate.
Untuk kali ini kita akan belajar tentang CASCADE, jadi data yang terdapat di
tabel induk dan tabel anak akan sama sama terhapus atau terupdate.
Langkah
pertama
kita
ganti
struktur
dari
tabel
anak
agar
memuat
perintah
untuk
on update cascade);
Query OK, 0 rows affected (0.06 sec)
mysql> desc t_stok;
+----------+------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+----------+------------+------+-----+---------+-------+
| id_stok | varchar(8) | NO
| PRI | NULL
|
|
| id_mobil | varchar(8) | YES | MUL | NULL
|
|
| stok
| int(5)
| YES |
| NULL
|
|
+----------+------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
Isi tabelnya lagi
mysql>
insert
into
t_stok
values('ST-001','TOY-01','1000'),('ST-002','TOY02','521'),('ST-003','HON-01','875');
Query OK, 3 rows affected (0.01 sec)
Records: 3
Duplicates: 0
Warnings: 0
perintah
Duplicates: 0
Warnings: 0
alter
table,
Kita juga bisa mengatur letak kolom yang akan kita buat
Duplicates: 0
Warnings: 0
| varchar(20) | YES |
| varchar(8) | YES |
| NULL
| NULL
|
|
|
|
| harga
| bigint(11) | YES |
| NULL
|
|
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.04 sec)
Duplicates: 0
Warnings: 0
e. Menghapus Kolom
Cara untuk menghapus kolom dengan perintah ALTER TABLE adalah sbb, misal kita akan
menghapus salah satu kolom di t_car, maka caranya :
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
Tambah primary key
mysql>
insert
into
t_parkir
values('11103','AB-5555-UX','HONDA'),
('11104','H-36-B','SUZUKI'),('11105','N-7876-OK','KIA');
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select*from t_parkir;
+-----------+------------+--------+
| id_parkir | plat_no
| merk
|
+-----------+------------+--------+
| 11101
| AB-1234-OP | TOYOTA |
| 11102
| B-3454-SDE | BMW
|
| 11103
| AB-5555-UX | HONDA |
| 11104
| BH-36-B
| SUZUKI |
| 11105
| N-7876-OK | KIA
|
+-----------+------------+--------+
5 rows in set (0.00 sec)
10.
Changed: 1
Warnings: 0
| AB-1234-OP | TOYOTA
|
| B-3454-SDE | NISSAN
|
| AB-5555-UX | HONDA
|
| BH-36-B
| SUZUKI
|
| N-7876-OK | KIA
|
+------------+-----------+
5 rows in set (0.00 sec)
id_parkir,
plat_no
12.
| AB-1234-OP | TOYOTA |
| B-3454-SDE | BMW
|
| 11103
| 11104
| AB-5555-UX | HONDA
| BH-36-B
| ISUZU
|
|
+-----------+------------+--------+
4 rows in set (0.00 sec)
merk)
values