Selasa, 17 Desember 2013

MySQL


//Membuat Database
mysql> create database kantor;
Query OK, 1 row affected (0.03 sec)

//untuk mengakses database
mysql> use kantor;
Database changed

//Membuat tabel
mysql> create table pegawai (
    -> nip char(10) not null primary key,
    -> nama varchar(40) not null,
    -> alamat varchar(80) not null,
    -> sex char(1),
    -> gaji int default 0);
Query OK, 0 rows affected (0.05 sec)

//memasukkan data ke dalam tabel pegawai
mysql> insert into pegawai (nip,nama,alamat,sex,gaji)
    -> values
    -> ("001","Totok","Jalan Raya 10 Yogya","P","1500000");
Query OK, 1 row affected (0.03 sec)


//melihat data tabel pegawai berbentuk tabel
mysql> select * from pegawai \g
+-----+-------+---------------------+------+---------+
| nip | nama  | alamat              | sex  | gaji    |
+-----+-------+---------------------+------+---------+
| 001 | Totok | Jalan Raya 10 Yogya | P    | 1500000 |
+-----+-------+---------------------+------+---------+
1 row in set (0.00 sec)

//menampilkan data tabel pegawai yang berbentuk List
mysql> select * from pegawai \G
*************************** 1. row ***************************
   nip: 001
  nama: Totok
alamat: Jalan Raya 10 Yogya
   sex: P
  gaji: 1500000
1 row in set (0.00 sec)


//Memasukkan data ke dalam tabel pegawai
mysql> insert into pegawai (nip,nama,alamat,sex,gaji)
    -> values
    -> ("002","Andi","Tahunan UH 1 / 234 Yogya","P","4000000");
Query OK, 1 row affected (0.00 sec)

mysql> insert into pegawai (nip,nama,alamat,sex,gaji)
    -> values
    -> ("003","Siti","Jalan Berbatu 5 Solo","W","750000");
Query OK, 1 row affected (0.00 sec)

mysql> insert into pegawai (nip,nama,alamat,sex,gaji)
    -> values
    -> ("004","Rudi","Kanoman RT 2 RW 1 Yogya","P","900000");
Query OK, 1 row affected (0.00 sec)

//Melihat data nama dan alamat dari tabel pegawai
mysql> select nama,alamat from pegawai;
+-------+--------------------------+
| nama  | alamat                   |
+-------+--------------------------+
| Totok | Jalan Raya 10 Yogya      |
| Andi  | Tahunan UH 1 / 234 Yogya |
| Siti  | Jalan Berbatu 5 Solo     |
| Rudi  | Kanoman RT 2 RW 1 Yogya  |
+-------+--------------------------+
4 rows in set (0.00 sec)


//menampilkan data tabel pegawai diurutkan berdasarkan nama
mysql> select *  from pegawai order by nama;
+-----+-------+--------------------------+------+---------+
| nip | nama  | alamat                   | sex  | gaji    |
+-----+-------+--------------------------+------+---------+
| 002 | Andi  | Tahunan UH 1 / 234 Yogya | P    | 4000000 |
| 004 | Rudi  | Kanoman RT 2 RW 1 Yogya  | P    |  900000 |
| 003 | Siti  | Jalan Berbatu 5 Solo     | W    |  750000 |
| 001 | Totok | Jalan Raya 10 Yogya      | P    | 1500000 |
+-----+-------+--------------------------+------+---------+
4 rows in set (0.03 sec)

//menampilkan data pegawai dimana nama=siti
mysql> select *  from pegawai where nama="siti";
+-----+------+----------------------+------+--------+
| nip | nama | alamat               | sex  | gaji   |
+-----+------+----------------------+------+--------+
| 003 | Siti | Jalan Berbatu 5 Solo | W    | 750000 |
+-----+------+----------------------+------+--------+
1 row in set (0.00 sec)

//menampilkan nip,nama,dan gaji dari tabel pegawai berdasarkan gaji tertinggi
mysql> select nip,nama,gaji from pegawai order by gaji desc;
+-----+-------+---------+
| nip | nama  | gaji    |
+-----+-------+---------+
| 002 | Andi  | 4000000 |
| 001 | Totok | 1500000 |
| 004 | Rudi  |  900000 |
| 003 | Siti  |  750000 |
+-----+-------+---------+
4 rows in set (0.00 sec)


//menampilkan data tabel pegawai dimana sex=P
mysql> select * from pegawai where sex="P";
+-----+-------+--------------------------+------+---------+
| nip | nama  | alamat                   | sex  | gaji    |
+-----+-------+--------------------------+------+---------+
| 001 | Totok | Jalan Raya 10 Yogya      | P    | 1500000 |
| 002 | Andi  | Tahunan UH 1 / 234 Yogya | P    | 4000000 |
| 004 | Rudi  | Kanoman RT 2 RW 1 Yogya  | P    |  900000 |
+-----+-------+--------------------------+------+---------+
3 rows in set (0.00 sec)

//membuat tabel jabatan
mysql> create table jabatan (
    -> kodejabat char(2) not null primary key,
    -> namajabat varchar(20),
    -> tunjangan float default 0);
Query OK, 0 rows affected (0.03 sec)

//memasukkan data ke dalam tabel jabatan
mysql> INSERT INTO jabatan
    -> VALUES
    -> ("01","DIREKTUR",500000);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO jabatan
    -> VALUES
    -> ("02","MANAGER",250000);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO jabatan
    -> VALUES
    -> ("03","STAFF",0);
Query OK, 1 row affected (0.00 sec)

//melihat isi data tabel jabatan
mysql> SELECT * FROM jabatan;
+-------------+-------------+-----------+
| kodejabatan | namajabatan | tunjangan |
+-------------+-------------+-----------+
| 01          | DIREKTUR    |    500000 |
| 02          | MANAGER     |    250000 |
| 03          | STAFF       |         0 |
+-------------+-------------+-----------+
3 rows in set (0.00 sec)

Posting Lebih Baru Posting Lama Beranda

0 komentar:

Posting Komentar