Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pemrograman Internet Mobile Antonius R.C, S.Kom, M.Cs MySQL.

Similar presentations


Presentation on theme: "Pemrograman Internet Mobile Antonius R.C, S.Kom, M.Cs MySQL."— Presentation transcript:

1 Pemrograman Internet Mobile Antonius R.C, S.Kom, M.Cs MySQL

2 SQL Pada MySQL DDL – Data Definition Language –CREATE –ALTER –DROP DML – Data Manipulation Language –INSERT –UPDATE –DELETE Data Retrieving / Query –SELECT DCL – Data Control Language –Administrasi User

3 Melihat Versi MySQL SELECT VERSION(); mysql> SELECT VERSION(); | VERSION() | | Debian_1ubuntu3.1-log |

4 Melihat Database MySQL SHOW DATABASES; mysql> SHOW DATABASES; | Database | | mysql | | test |

5 Create Database CREATE DATABASE ; mysql> CREATE DATABASE coba; mysql> SHOW DATABASES; | Database | | coba | | mysql | | test |

6 Menghapus Database DROP DATABASE ; mysql> DROP DATABASE coba; mysql> SHOW DATABASES; | Database | | mysql | | test |

7 Menggunakan Database USE ; mysql> CREATE DATABASE universitas; mysql> SHOW DATABASES; | Database | | mysql | | test | | universitas | mysql> USE universitas; Database changed

8 Create Table CREATE TABLE (,,...); mysql> CREATE TABLE pegawai -> (nik CHAR(4), nama VARCHAR(20));

9 Select Db dan Show Table SELECT DATABASE(); mysql> SELECT DATABASE(); | DATABASE() | | universitas | SHOW TABLES; mysql> SHOW TABLES; | Tables_in_universitas | | pegawai |

10 Menghapus table DROP TABLE ; mysql> DROP TABLE karyawan; mysql> SHOW TABLES; Empty set (0.01 sec)

11 Melihat deskripsi table DESCRIBE ; EXPLAIN ; SHOW COLUMNS FROM ; mysql> DESCRIBE pegawai; | Field | Type | Null | Key | Default | Extra | | nik | varchar(4) | YES | | NULL | | | nama | varchar(20) | YES | | NULL | |

12 MENGUBAH NAMA TABLE ALTER TABLE RENAME ; mysql> ALTER TABLE pegawai RENAME karyawan; mysql> SHOW TABLES; | Tables_in_universitas | | karyawan |

13 Menambah Kolom di Akhir ALTER TABLE ADD COLUMN ; mysql> ALTER TABLE karyawan -> ADD COLUMN gaji int(5); mysql> DESCRIBE karyawan; | Field | Type | Null | Key | Default | Extra | | nik | varchar(4) | YES | | NULL | | | nama | varchar(20) | YES | | NULL | | | gaji | int(5) | YES | | NULL | |

14 Tambah Kolom di Awal ALTER TABLE ADD COLUMN FIRST; mysql> ALTER TABLE karyawan -> ADD COLUMN tgl_lahir date FIRST; mysql> DESCRIBE karyawan; | Field | Type | Null | Key | Default | Extra | | tgl_lahir | date | YES | | NULL | | | nik | varchar(4) | YES | | NULL | | | nama | varchar(20) | YES | | NULL | | | gaji | int(5) | YES | | NULL | |

15 Menyisipkan kolom baru ALTER TABLE ADD COLUMN AFTER ; mysql> ALTER TABLE karyawan -> ADD COLUMN bonus int(4) AFTER nik; mysql> DESCRIBE karyawan; | Field | Type | Null | Key | Default | Extra | | tgl_lahir | date | YES | | NULL | | | nik | varchar(4) | YES | | NULL | | | bonus | int(4) | YES | | NULL | | | nama | varchar(20) | YES | | NULL | | | gaji | int(5) | YES | | NULL | |

16 Ubah nama kolom dan tipe datanya ALTER TABLE CHANGE ; mysql> ALTER TABLE karyawan -> CHANGE nik nip char(4); mysql> DESCRIBE karyawan; | Field | Type | Null | Key | Default | Extra | | tgl_lahir | date | YES | | NULL | | | nip | varchar(4) | YES | | NULL | | | bonus | int(4) | YES | | NULL | | | nama | varchar(20) | YES | | NULL | | | gaji | int(5) | YES | | NULL | |

17 Mengubah tipe data kolom ALTER TABLE MODIFY ; mysql> ALTER TABLE karyawan -> MODIFY nip int(5); mysql> DESCRIBE karyawan; | Field | Type | Null | Key | Default | Extra | | tgl_lahir | date | YES | | NULL | | | nip | int(5) | YES | | NULL | | | bonus | int(4) | YES | | NULL | | | nama | varchar(20) | YES | | NULL | | | gaji | int(5) | YES | | NULL | |

18 Menghapus kolom ALTER TABLE DROP COLUMN ; mysql> ALTER TABLE karyawan -> DROP COLUMN tgl_lahir; mysql> DESCRIBE karyawan; | Field | Type | Null | Key | Default | Extra | | nip | int(5) | YES | | NULL | | | bonus | int(4) | YES | | NULL | | | nama | varchar(20) | YES | | NULL | | | gaji | int(5) | YES | | NULL | |

19 Create table dgn primary key CREATE TABLE (kolom1 tipe_data1 PRIMARY KEY, kolom2 tipe_data2); mysql> CREATE TABLE ktp -> (no_ktp int(15) PRIMARY KEY, -> nama varchar(20),tgl_lahir date, -> alamat varchar(30)); mysql> DESCRIBE ktp; | Field | Type | Null | Key | Default | Extra | | no_ktp | int(15) | | PRI | 0 | | | nama | varchar(20) | YES | | NULL | | | tgl_lahir | date | YES | | NULL | | | alamat | varchar(30) | YES | | NULL | |

20 mysql> DESCRIBE mahasiswa; | Field | Type | Null | Key | Default | Extra | | nrp | varchar(7) | | PRI | | | | nama | varchar(20) | YES | | NULL | | | tgl_lahir | date | YES | | NULL | | | ipk | double(3,2) | YES | | NULL | | mysql> DESCRIBE mata_kuliah; | Field | Type | Null | Key | Default | Extra | | kd_mk | varchar(5) | | PRI | | | | nama_mk | varchar(15) | YES | | NULL | | | sks | int(1) | YES | | NULL | | mysql> DESCRIBE pengambilan; | Field | Type | Null | Key | Default | Extra | | nrp | varchar(7) | YES | | NULL | | | kd_mk | varchar(5) | YES | | NULL | | | nilai | int(3) | YES | | NULL | |

21 Jawab mysql> CREATE TABLE mahasiswa -> (nrp varchar(7) PRIMARY KEY, -> nama varchar(20), -> tgl_lahir date,ipk double(3,2)) mysql> CREATE TABLE mata_kuliah -> (kd_mk varchar(5)PRIMARY KEY, -> nama_mk varchar(15), -> sks int(1)); mysql> CREATE TABLE pengambilan -> (nrp varchar(7),kd_mk char(5), -> nilai int(3));

22 Tambah record INSERT INTO (kolom1,kolom2,kolom3) VALUES (data1,data2,data3); mysql> INSERT INTO mahasiswa -> (nrp,nama,tgl_lahir,ipk) VALUES -> (' ','Dodi',' ',3.5); mysql> SELECT * FROM mahasiswa; | nrp | nama | tgl_lahir | ipk | | | Dodi | | 3.50 |

23 Update record UPDATE SET = WHERE ; mysql> UPDATE mahasiswa SET ipk=3.25 -> WHERE nrp=' '; mysql> SELECT * FROM mahasiswa; | nrp | nama | tgl_lahir | ipk | | | Dodi | | 3.25 | | | Billy | | 2.75 | | | Wiro | | 2.27 |

24 Delete record DELETE FROM WHERE ; mysql> DELETE FROM mahasiswa -> WHERE nama='Dodi'; mysql> SELECT * FROM mahasiswa; | nrp | nama | tgl_lahir | ipk | | | Billy | | 2.75 | | | Wiro | | 2.27 |

25 Truncate (Mengkosongkan table) TRUNCATE TABLE ; TRUNCATE ; mysql> TRUNCATE TABLE mahasiswa; mysql> SELECT * FROM mahasiswa; Empty set (0.00 sec)

26 Auto increment CREATE TABLE (kolom_kunci> int(n) AUTO_INCREMENT PRIMARY KEY, ); mysql> CREATE TABLE warga ( -> no int(3) AUTO_INCREMENT PRIMARY KEY, -> nama varchar(20),usia int(2)); mysql> DESCRIBE warga; | Field | Type | Null | Key | Default | Extra | | no | int(3) | | PRI | NULL | auto_increment | | nama | varchar(20) | YES | | NULL | | | usia | int(2) | YES | | NULL | |

27 Auto increment (2) mysql> INSERT INTO warga(nama,usia) -> VALUES('Tora',33); mysql> INSERT INTO warga(nama) VALUES('Aming'); mysql> INSERT INTO warga(usia) VALUES(27); mysql> SELECT * FROM warga; | no | nama | usia | | 1 | Tora | 33 | | 2 | Aming | NULL | | 3 | NULL | 27 |

28 Auto increment (3) mysql> DELETE FROM warga WHERE nama='Tora'; mysql> INSERT INTO warga(nama,usia) -> VALUES('Jojon',40); mysql> SELECT * FROM warga; | no | nama | usia | | 4 | Jojon | 40 | | 2 | Aming | NULL | | 3 | NULL | 27 |

29 Nilai default ALTER TABLE MODIFY DEFAULT ; mysql> ALTER TABLE warga -> MODIFY usia int(2) DEFAULT 25; mysql> DESCRIBE warga; | Field | Type | Null | Key | Default | Extra | | no | int(3) | | PRI | NULL | auto_increment | | nama | varchar(20) | YES | | NULL | | | usia | int(2) | YES | | 25 | |

30 Nilai Default mysql> INSERT INTO warga(nama) VALUES('Eko'); mysql> INSERT INTO warga(nama,usia) -> VALUES('Tarsan',68); mysql> INSERT INTO warga() VALUES(); mysql> SELECT * FROM warga; | no | nama | usia | | 1 | Parto | 33 | | 2 | Akri | 32 | | 3 | Eko | 25 | | 4 | Tarsan | 68 | | 5 | NULL | 25 |

31 NOT NULL ALTER TABLE MODIFY NOT NULL; mysql> CREATE TABLE anggota ( -> nama varchar(20) NOT NULL, -> tgl_lahir date,telp varchar(15)); mysql> DESCRIBE anggota; | Field | Type | Null | Key | Default | Extra | | nama | varchar(20) | | | | | | tgl_lahir | date | YES | | NULL | | | telp | varchar(15) | YES | | NULL | | NOT NULL berarti tidak kolom tersebut tidak boleh dikosongkan

32 Foreign Key ALTER TABLE ADD FOREGIN KEY (kolom_anak)REFERENCES ( ); mysql> ALTER TABLE pengambilan ADD FOREIGN -> KEY(nrp) REFERENCES mahasiswa(nrp); mysql> ALTER TABLE pengambilan ADD FOREIGN -> KEY(kd_mk) REFERENCES -> mata_kuliah(kd_mk); Foreign Key = Kunci Tamu Field yang menjadi kunci utama di table lain dan masuk menjadi field di suatu table yang berhubungan dengan table yg memiliki kunci utama tersebut

33 Zerofill mysql> CREATE TABLE barang -> (nama varchar(20), -> harga int(5) ZEROFILL); mysql> DESCRIBE barang; | Field | Type | Null | Key | Default | Extra | | nama | varchar(20) | YES | | NULL | | | harga | int(5) unsigned zerofill | YES | | NULL | | mysql> INSERT INTO barang VALUES('Kopi',500); mysql> INSERT INTO barang VALUES('Gula',300); mysql> INSERT INTO barang VALUES('Susu',1100); mysql> SELECT * FROM barang; | nama | harga | | Kopi | | | Gula | | | Susu | |

34 Isi data mysql> SELECT * FROM mahasiswa; | nrp | nama | tgl_lahir | ipk | | | Spiderman | | 3.33 | | | Superman | | 2.45 | | | Batman | | 2.77 | | | Robin | | 3.77 | | | Profesor X | | 3.51 | | | Wonder Woman | | 2.19 | mysql> SELECT * FROM mata_kuliah; | kd_mk | nama_mk | sks | | IF001 | Menembak | 3 | | IF002 | Menyamar | 2 | | IF003 | Terbang | 2 | | IF004 | Memanjat | 3 |

35 Backup table CREATE TABLE AS SELECT * FROM ; mysql> CREATE TABLE mhs AS -> SELECT * FROM mahasiswa; mysql> CREATE TABLE matkul AS -> SELECT * FROM mata_kuliah;

36 Select And n Or mysql> SELECT * FROM pengambilan -> WHERE kd_mk='IF002' AND nilai>85; | nrp | kd_mk | nilai | | | IF002 | 88 | | | IF002 | 88 | | | IF002 | 88 | mysql> SELECT * FROM pengambilan -> WHERE kd_mk='IF002' OR nilai>85; | nrp | kd_mk | nilai | | | IF003 | 90 | | | IF002 | 88 | | | IF002 | 88 | | | IF002 | 88 | | | IF002 | 82 |

37 Select Not n Alias mysql> SELECT * FROM pengambilan -> WHERE NOT (kd_mk='IF002'); | nrp | kd_mk | nilai | | | IF004 | 70 | | | IF003 | 90 | mysql> SELECT nama "Nama", -> ipk "Indeks Prestasi Kumulatif" -> FROM mahasiswa; | Nama | Indeks Prestasi Kumulatif | | Spiderman | 3.33 | | Superman | 2.45 | | Batman | 2.77 | | Robin | 3.77 | | Profesor X | 3.51 | | Wonder Woman | 2.19 |

38 Order by SELECT FROM [WHERE ] ORDER BY [ASC|DESC]; mysql> SELECT * FROM mahasiswa -> WHERE ipk <3 -> ORDER BY nama DESC; | nrp | nama | tgl_lahir | ipk | | | Wonder Woman | | 2.19 | | | Superman | | 2.45 | | | Batman | | 2.77 |

39 Operator like Menampilkan data mahasiswa angkatan 2005 mysql> SELECT * FROM mahasiswa -> WHERE nrp LIKE '05%'; | nrp | nama | tgl_lahir | ipk | | | Robin | | 3.77 | | | Profesor X | | 3.51 |

40 Like (2) Menampilkan data mahasiswa yang berulang tahun pada bulan mei mysql> SELECT * FROM mahasiswa -> WHERE tgl_lahir LIKE '%05-__'; | nrp | nama | tgl_lahir | ipk | | | Batman | | 2.77 | | | Profesor X | | 3.51 |

41 Operator Matematika mysql> SELECT ipk "IPK", ipk+1 "+10" -> ipk-10 "-10", ipk*10 "*10", -> ipk/10 "/10", ipk%2 “%2” -> FROM mahasiswa; | IPK | +10 | -10 | *10 | /10 | %2 | | 3.33 | 4.33 | | | | 1.33 | | 2.45 | 3.45 | | | | 0.45 | | 2.77 | 3.77 | | | | 0.77 | | 3.77 | 4.77 | | | | 1.77 | | 3.51 | 4.51 | | | | 1.51 | | 2.19 | 3.19 | | | | 0.19 |

42 Sum, Count mysql> SELECT SUM(nilai) "nilaitotal" -> FROM pengambilan -> WHERE nrp=' '; | nilai total | | 158 | mysql> SELECT COUNT(8) “Jml Mhs Angkt 04” -> FROM mahasiswa -> WHERE nrp LIKE '04%'; | Jml Mhs Angkt 04 | | 4 |

43 Avg, Max, Min mysql> SELECT AVG(ipk) -> FROM mahasiswa -> WHERE nrp LIKE '05%'; | AVG(ipk) | | | mysql> SELECT MIN(ipk) "IPK Terkecil", -> MAX(ipk) "IPK Terbesar" -> FROM mahasiswa; | IPK Terkecil | IPK Terbesar | | 2.19 | 3.77 |

44 Group by SELECT FROM GROUP BY ; mysql> SELECT kd_mk,COUNT(kd_mk) -> FROM pengambilan -> GROUP BY kd_mk; | kd_mk | COUNT(kd_mk) | | IF002 | 4 | | IF003 | 1 | | IF004 | 1 |

45 Group By (2) mysql> SELECT nrp,COUNT(*),AVG(nilai), -> MAX(nilai),MIN(nilai) -> FROM pengambilan -> GROUP BY nrp; | nrp | COUNT(*) | AVG(nilai) | MAX(nilai) | MIN(nilai) | | | 2 | | 88 | 70 | | | 1 | | 88 | 88 | | | 2 | | 90 | 88 | | | 1 | | 82 | 82 |

46 Fungsi matematika ABS(n) POW(x,y) SQRT(n); CEILING(n) dan FLOOR(n) ROUND(n [,m ]) dan TRUNCATE(n,m) RADIANS(n) dan DEGREES(n) SIN(n), COS(n), dan TAN(n) PI() BIN(n), OCT(n), dan HEX(n) FORMAT(m,n)

47 Fungsi String UCASE(x) dan UPPER(x) LCASE(x) dan LOWER(x) ASCII(n) dan CHAR(x) CHAR_LENGTH(x) dan LENGTH(x) CONCAT(x,y [,z,..]) STRCMP(x,y); ENCODE(x,y) dan DECODE(x,y) ENRYPT(x) dan PASSWORD(x) COALESCE(x,y); INSERT(x,m,n,y) LTRIM(x), RTRIM(x), dan TRIM(x) LPAD(x,y,z) dan RPAD(x,y,z) REVERSE(x) REPLACE(x,y,z) LEFT(x,n), RIGHT(x,n), dan MID(x,m,n)

48 Fungsi ASCII dan CHAR mysql> SELECT ASCII('B'),CHAR(66); | ASCII('B') | CHAR(66) | | 66 | B | mysql> SELECT nama_mk,ASCII(nama_mk) FROM mata_kuliah; | nama_mk | ASCII(nama_mk) | | Menembak | 77 | | Menyamar | 77 | | Terbang | 84 | | Memanjat | 77 |

49 LENGTH dan CHAR_LENGTH mysql> SELECT nama,LENGTH(nama), -> CHAR_LENGTH(nama) -> FROM mahasiswa; | nama | LENGTH(nama) | CHAR_LENGTH(nama) | | Spiderman | 9 | 9 | | Superman | 8 | 8 | | Batman | 6 | 6 | | Robin | 5 | 5 | | Profesor X | 10 | 10 | | Wonder Woman | 12 | 12 |

50 STRCMP mysql> SELECT STRCMP('Spiderman','SPIDERMAN'); | STRCMP('Spiderman','SPIDERMAN') | | 0 | mysql> SELECT STRCMP('Spiderman','Batman'); | STRCMP('Spiderman','Batman') | | 1 | mysql> SELECT STRCMP('Spiderman','Spidermanz'); | STRCMP('Spiderman','Spidermanz') | | -1 |

51 ENCODE dan DECODE mysql> SELECT nama,ENCODE(nama,'key'), -> DECODE(ENCODE(nama,'key'),'key') -> FROM mahasiswa; | nama | acak | DECODE(ENCODE(nama,'key'),'key') | | Spiderman | H? | Spiderman | | Superman |.+ | Superman | | Batman | { | Batman | | Robin | ZG* | Robin | | Profesor X | mn6R | Profesor X | | Wonder Woman | $B-sb | Wonder Woman |

52 ENCRYPT dan PASSWORD mysql> SELECT nama,ENCRYPT(nama),PASSWORD(nama) -> FROM mahasiswa; | nama | ENCRYPT(nama) | PASSWORD(nama) | | Spiderman | DyCsAXu0bZaao | 162ed2e56022ac99 | | Superman | DydyB2x2xXmXo | dd330aac14 | | Batman | DyIlblnd77dfw | d423db292 | | Robin | DyyMAZuf5ODFE | 406ad76459c5d464 | | Profesor X | Dyy9LvL6qAONQ | 70a37e8212baf12a | | Wonder Woman | Dysfyzf4PkiUk | 730b e7931 |

53 TRIM, LTRIM, RTRIM mysql> SELECT LTRIM(' Batman '), -> RTRIM(' Batman '), -> TRIM(' Batman '); | LTRIM(' Batman ') | RTRIM(' Batman ') | TRIM(' Batman ') | | Batman | Batman | Batman | mysql> SELECT LENGTH(' Batman ') "asli", -> LENGTH(LTRIM(' Batman ')) "LTRIM", -> LENGTH(RTRIM(' Batman ')) "RTRIM", -> LENGTH(TRIM(' Batman ')) "TRIM"; | asli | LTRIM | RTRIM | TRIM | | 12 | 9 | 9 | 6 |

54 LPAD, RPAD mysql> SELECT nama, -> LPAD(nama,15,'*'), -> RPAD(nama,15,'#') -> FROM mahasiswa; | nama | LPAD(nama,15,'*') | RPAD(nama,15,'#') | | Spiderman | ******Spiderman | Spiderman###### | | Superman | *******Superman | Superman####### | | Batman | *********Batman | Batman######### | | Robin | **********Robin | Robin########## | | Profesor X | *****Profesor X | Profesor X##### | | Wonder Woman | ***Wonder Woman | Wonder Woman### |

55 LEFT, RIGHT, MID mysql> SELECT nama, -> LEFT(nama,3), -> RIGHT(nama,3), -> MID(nama,2,3) -> FROM mahasiswa; | nama | LEFT(nama,3) | RIGHT(nama,3) | MID(nama,2,3) | | Spiderman | Spi | man | pid | | Superman | Sup | man | upe | | Batman | Bat | man | atm | | Robin | Rob | bin | obi | | Profesor X | Pro | r X | rof | | Wonder Woman | Won | man | ond |

56 LEFT mysql> SELECT LEFT(nrp,2)"angkatan",AVG(ipk) -> FROM mahasiswa -> GROUP BY angkatan; | angkatan | AVG(ipk) | | 04 | | | 05 | |

57 Fungsi Tanggal CURDATE() dan CURRENT_DATE() CURTIME() dan CURRENT_TIME() SYSDATE(), NOW(), dan CURRENT_TIMESTAMP() DATE_ADD(d,INTERAL n DAY) dan DATE_SUB(d,INTERVAL n DAY) DATE_FORMAT(d,f) DAYOFWEEK(d), DAYOFMONTH(d), dan DAYOFYEAR(d) DAYNAME(d) dan MONTHNAME(d) DAY(d), MONTH(d), dan YEAR(d) HOUR(d), MINUTE(d), dan SECOND(d) QUARTER(d)

58 DATE_ADD, DATE_SUB mysql> SELECT tgl_lahir "asli", -> DATE_ADD(tgl_lahir, INTERVAL 7 DAY) -> "DATE_ADD", -> DATE_SUB(tgl_lahir,INTERVAL 7 DAY) -> "DATE_SUB" -> FROM mahasiswa; | asli | DATE_ADD | DATE_SUB | | | | | | | | | | | | | | | | | | | | | | | | |

59 DATE_FORMAT mysql> SELECT nama,tgl_lahir, -> DATE_FORMAT(tgl_lahir,'%a,%d %M %Y') -> "format tgl" -> FROM mahasiswa; | nama | tgl_lahir | format tgl | | Spiderman | | Mon,26 February 1990 | | Superman | | Sat,22 October 1988 | | Batman | | Tue,14 May 1985 | | Robin | | Fri,27 March 1992 | | Profesor X | | Thu,23 May 1991 | | Wonder Woman | | Sun,12 July 1992 |

60 Format untuk DATE_FORMAT %a Format singkat nama hari (Sun..Sat) %b Format singkat bulan (Jan..Dec) %D Format tanggal teurut (1st, 2nd, 3rd,...) %d Format tanggal 2 digit (00..31) %H Format jam 24 jam (00..23) %h Format jam 12 jam (01..12) %i Format menit 2 digit (00..59) %M Format nama bulan (January..December) %m Format angka bulan (00..12)

61 DAYOFWEEK, MONTH, dan YEAR mysql> SELECT tgl_lahir, -> DAYOFWEEK(tgl_lahir) "dayofweek", -> DAYOFMONTH(tgl_lahir) "dayofmonth", -> DAYOFYEAR(tgl_lahir) "day of year" -> FROM mahasiswa; | tgl_lahir | day of week | day of month | day of year | | | 2 | 26 | 57 | | | 7 | 22 | 296 | | | 3 | 14 | 134 | | | 6 | 27 | 87 | | | 5 | 23 | 143 | | | 1 | 12 | 194 |

62 DAY dan MONTHNAME mysql> SELECT tgl_lahir, -> DAYNAME(tgl_lahir), -> MONTHNAME(tgl_lahir) -> FROM mahasiswa; | tgl_lahir | DAYNAME(tgl_lahir) | MONTHNAME(tgl_lahir) | | | Monday | February | | | Saturday | October | | | Tuesday | May | | | Friday | March | | | Thursday | May | | | Sunday | July |

63 Fungsi lainnya ISNULL(a) IFNULL(a,b) IF(a,b,c)

64 ISNULL mysql> INSERT INTO pengambilan(nrp,kd_mk) -> VALUES(' ','IF004'); mysql> SELECT * FROM pengambilan -> WHERE ISNULL(nilai); | nrp | kd_mk | nilai | | | IF004 | NULL |

65 IFNULL mysql> SELECT nrp, -> kd_mk, -> IFNULL(nilai,'kosong') -> FROM pengambilan; | nrp | kd_mk | IFNULL(nilai,'kosong') | | | IF004 | 70 | | | IF003 | 90 | | | IF002 | 88 | | | IF002 | 88 | | | IF002 | 88 | | | IF002 | 82 | | | IF004 | kosong |

66 IF mysql> SELECT nrp, -> nilai, -> IF(nilai>=90,'Hebat','Baik')“keterangan” -> FROM pengambilan; | nrp | nilai | keterangan | | | 70 | Baik | | | 90 | Hebat | | | 88 | Baik | | | 88 | Baik | | | 88 | Baik | | | 82 | Baik | | | NULL | Baik |

67 IF (2) mysql> SELECT nrp,nilai, -> IF(nilai>=90,'A',IF(nilai>=80,'B','C')) -> "keterangan" -> FROM pengambilan; | nrp | nilai | keterangan | | | 70 | C | | | 90 | A | | | 88 | B | | | 88 | B | | | 88 | B | | | 82 | B | | | NULL | C |

68 IF (3) mysql> SELECT nrp,nilai, -> IF(nilai>=90,'A',IF(nilai>=80,'B', -> IF(ISNULL(nilai),'KOSONG','C'))) -> "keterangan" -> FROM pengambilan; | nrp | nilai | keterangan | | | 70 | C | | | 90 | A | | | 88 | B | | | 88 | B | | | 88 | B | | | 82 | B | | | NULL | KOSONG |

69 Relasi Cross Join SELECT FROM CROSS JOIN ; mysql> SELECT * -> FROM mahasiswa,pengambilan; mysql> SELECT * -> FROM mahasiswa,pengambilan,mata_kuliah; mysql> SELECT * -> FROM mahasiswa CROSS JOIN pengambilan; mysql> SELECT * -> FROM mahasiswa CROSS JOIN pengambilan -> CROSS JOIN mata_kuliah;

70 Inner Join SELECT FROM INNER JOIN ON ; mysql> SELECT * -> FROM mahasiswa INNER JOIN pengambilan -> ON mahasiswa.nrp=pengambilan.nrp; | nrp | nama | tgl_lahir | ipk | nrp | kd_mk | nilai | | | Spiderman | | 3.33 | | IF004 | 70 | | | Superman | | 2.45 | | IF003 | 90 | | | Spiderman | | 3.33 | | IF002 | 88 | | | Superman | | 2.45 | | IF002 | 88 | | | Batman | | 2.77 | | IF002 | 88 | | | Robin | | 3.77 | | IF002 | 82 | | | Superman | | 2.45 | | IF004 | NULL |

71 Table Aliasing mysql> SELECT m.nrp,m.nama, p.kd_mk, p.nilai -> FROM mahasiswa m -> INNER JOIN pengambilan p -> ON m.nrp=p.nrp; | nrp | nama | kd_mk | nilai | | | Spiderman | IF004 | 70 | | | Superman | IF003 | 90 | | | Spiderman | IF002 | 88 | | | Superman | IF002 | 88 | | | Batman | IF002 | 88 | | | Robin | IF002 | 82 | | | Superman | IF004 | NULL |

72 Inner join > 2 table mysql> SELECT m.nrp,m.nama,mk.nama_mk,p.nilai -> FROM mahasiswa m -> INNER JOIN pengambilan p -> ON m.nrp=p.nrp -> INNER JOIN mata_kuliah mk -> ON p.kd_mk=mk.kd_mk; | nrp | nama | nama_mk | nilai | | | Spiderman | Memanjat | 70 | | | Superman | Terbang | 90 | | | Spiderman | Menyamar | 88 | | | Superman | Menyamar | 88 | | | Batman | Menyamar | 88 | | | Robin | Menyamar | 82 | | | Superman | Memanjat | NULL |

73 Bentuk lain inner join SELECT FROM, WHERE ; mysql> SELECT m.nrp,m.nama,mk.nama_mk,p.nilai -> FROM mahasiswa m, pengambilan p, -> mata_kuliah mk -> WHERE m.nrp=p.nrp AND p.kd_mk=mk.kd_mk; | nrp | nama | nama_mk | nilai | | | Spiderman | Memanjat | 70 | | | Superman | Terbang | 90 | | | Spiderman | Menyamar | 88 | | | Superman | Menyamar | 88 | | | Batman | Menyamar | 88 | | | Robin | Menyamar | 82 | | | Superman | Memanjat | NULL |

74 Data Control Language CREATE IDENTIFIED BY ' '; mysql> CREATE USER -> IDENTIFIED BY ‘123anton'; SELECT user,host,password FROM user; mysql> SELECT user,host,password FROM user; | user | host | password | | root | localhost | | | root | my-ubuntu | | | debian-sys-maint | localhost | 1d272cd33c9de0ee | | anton | localhost | 6a4c41e8176a7981 |

75 Drop User DROP ; mysql> DROP USER mysql> SELECT user,host,password FROM user; | user | host | password | | root | localhost | | | root | my-ubuntu | | | debian-sys-maint | localhost | 1d272cd33c9de0ee |

76 NEXT PHP


Download ppt "Pemrograman Internet Mobile Antonius R.C, S.Kom, M.Cs MySQL."

Similar presentations


Ads by Google