Perintah – Perintah Dasar MySQL


Perintah – Perintah Dasar MySQL
  •  Membuat database  
           CREATE DATABASE db_name
  • Menghapus database

DROP DATABASE db_name

  • Membuat Tabel
          CREATE TABLE tb_name(field 1 type_data, field 2 type_data)
  • Menghapus table
          DROP TABLE tb_name
  • Menambah Primary Key
         ALTER TABLE tb_name ADD PRIMARY KEY(field_name)
  • Menambah Primary Key dengan nama constraint
         ALTER TABLE tb_name ADD CONSTRAINT const_name PRIMARY KEY(field_name)
  • Menghapus Primary Key
         ALTER TABLE tb_name DROP PRIMARY KEY(field_name)
  • Menambah Foreign Key
         ALTER TABLE tb_name ADD FOREIGN KEY(field_name)
         REFERENCES table_references_name(primary_field_table_references)
         ON CASCADE
         ON RESTRICT
        cascade –> Mengupdate jika terjadi perubahan di tabel induk
        restrict   –> Peringatan jangan mendelete field pada tabel induk karena sedang digunakan di tabel anak
  • Menambah Foreign Key dengan nama constraint
         ALTER TABLE tb_name ADD CONSTRAINT const_name  FOREIGN KEY(field_name)
         REFERENCES table_references_name(primary_field_table_references)
  • Menghapus Foreign Key
         ALTER TABLE tb_name DROP FOREIGN KEY(field_name)
  • Menambah record
          INSERT INTO tb_name VALUES(record 1, record 2, record 3…)
          atau
          INSERT INTO tb_name (column 1, column2, column3) VALUES (record 1, record 2, record 3)
  • Fungsi SELECT
          SELECT field
          FROM table_name
          WHERE field = ‘value’
  • Fungsi UPDATE
          UPDATE table_name
          SET column1 = ‘val1′, column2=’val2’
          WHERE somecolumn = somevalue
  • Fungsi DELETE
          DELETE FROM table_name
          WHERE somecolumn = somevalue
Pada fungsi SELECT, UPDATE, dan DELETE pada syntax WHERE bosa menggunakan operator perbandingan seperti >, <, =, !=, >=, <= dan juga operator logika OR, AND dan NOT.
  • Fungsi IN
          SELECT *FROM table_name
          WHERE field IN (‘value’)
  • Fungsi NOT IN
          SELECT *FROM table_name
          WHERE field NOT IN (‘value’)
  • Mencari Record yang berawalan -x
          SELECT field
          FROM table
          WHERE field LIKE ‘x%’
  • Mencari Record yang berakhiran -x
          SELECT field
          FROM table
          WHERE field LIKE ‘%x’
  • Menampilkan field dari tabel – tabel yang berbeda
          SELECT mahasiswa.nama, kuliah.nilai
          FROM mahasiswa, kuliah
          WHERE mahasiswa.nim= kuliah.nim
  • INNERJOIN
          SELECT table1.*, table2.*
          FROM table1 INNERJOIN table2
          ON table1.pk = table2.pk
  • OUTERJOIN
          SELECT *FROM table1 NATURAL LEFTJOIN table2
          dan
          SELECT *FROM table1 NATURAL RIGHTJOIN table2
  • Rata – rata

SELECT AVG(field) from table_name

  • Jumlah

SELECT SUM(field) from table_name

  • Nilai Max

SELECT MAX(field) from table_name

  • Nilai MIN

SELECT MIN(field) from table_name

  • Jumlah record

SELECT COUNT(field) from table_name
atau
SELECT COUNT(distinct field) from table_name  –> untuk field yang memiliki record yang sama

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s