Anda di halaman 1dari 6

[Type text] Page 1

MATERI MINGGU 1 Prak. BASISDATA


PENGANTAR MY SQL
LABORATORIUM 4 BASISDATA

Melihat dafar database di komputer :
RUMUS :
SHOW DATABASES;
HASIL :
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| webauth |
+--------------------+

Membuat database baru :

RUMUS :
CREATE DATABASE(spasi)NAMA_DATABASE;
CONTOH :
mysql> create database lab4;
Query OK, 1 row affected (0.09 sec)

Memakai database yang telah kita buat

RUMUS :
USE(spasi)NAMA_DATABASE;
CONTOH :
mysql> use lab4;
Database changed

Membuat tabel baru :

RUMUS :
CREATE TABLE NAMA_TABEL
(
Nama_Kolom1(spasi)tipe_data(ukuran),
Nama_Kolom2(spasi)tipe_data(ukuran),
[Type text] Page 2

Nama_Kolom3(spasi)tipe_data(ukuran),
Nama_Kolom4(spasi)tipe_data(ukuran));



CONTOH :
mysql> create table t_mhs(nim varchar(10), nama_mhs varchar(50),
alamat varchar(50), no_telp varchar(12));

Query OK, 0 rows affected (0.16 sec)


Melihat hasil tabel yang kita buat :

RUMUS :
DESC(spasi)NAMA_TABEL;

CONTOH :
mysql> desc t_mhs;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| nim | varchar(10) | YES | | NULL | |
| nama_mhs | varchar(50) | YES | | NULL | |
| alamat | varchar(50) | YES | | NULL | |
| no_telp | varchar(12) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.08 sec)mysql> desc mahasiswa;


Input Data ke Dalam Tabel :
1. Menggunakan Rumus INSERT
2. Menggunakan Rumus LOAD DATA

1. Menginputkan data ke dalam tabel menggunakan perintah
INSERT
RUMUS :
Mysql >INSERT(spasi)INTO(spasi)NAMA_TABEL(spasi)VALUES
>(
>DATA_KOLOM1,
[Type text] Page 3

>DATA_KOLOM2,
>DATA_KOLOM3,
>DATA_KOLOM4
>);


CONTOH :

mysql> insert into t_mhs values( '111051005', 'Sholeh',
'Sleman','085785231478');
Query OK, 1 row affected (0.05 sec)

mysql> select *from t_mhs;
+-----------+----------+--------+--------------+
| nim | nama_mhs | alamat | no_telp |
+-----------+----------+--------+--------------+
| 111051005 | Sholeh | Sleman | 085785231478 |
+-----------+----------+--------+--------------+
5 rows in set (0.00 sec)

2. Menginputkan data ke dalam tabel menggunakan perintah
LOAD DATA

Persiapkan file yang berisi data yang akan diinputkan ke tabel
di database kita, sesuai dengan urutan yg kita buat di MYSQL.
Simpan dengan format .txt

CONTOH : (dibuat di MS.EXEL dan disimpan dengan nama data.txt
= format TEXT(MS-DOS) )
111051001 Uning Jogja 085786452315
111051002 Yuli Bantul 081354687923
111051003 Wanto Sleman 088869874563
111051004 Catur Bantul 081244455566


RUMUS :
LOAD(spasi)DATA(spasi)LOCAL(spasi)INFILE(spasi)
LOKASI-FILE(spasi)
INTO(spasi)
TABLE(spasi)
NAMA_TABEL
[Type text] Page 4

;

CONTOH :
mysql> load data local infile 'D://km1/data.txt' into table
t_mhs;
Query OK, 4 rows affected (0.03 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0

mysql> select*from t_mhs;
+-----------+----------+------------+---------------+
| nim | nama_mhs | alamat_mhs | no_telp |
+-----------+----------+------------+---------------+
|111051001 | Uning | Jogja | 085786452315
|111051002 | Yuli | Bantul | 081354687923
|111051003 | Wanto | Sleman | 088869874563
|111051004 | Catur | Bantul | 081244455566
+-----------+----------+------------+---------------+
4 rows in set (0.00 sec)

Menghapus database dan tabel

Buat dulu table baru

mysql> create table mat_kul(id_matkul varchar(10), nama_matkul
varchar(50), sks int(1), semester int(1), dosen varchar(10));
Query OK, 0 rows affected (0.02 sec)

mysql> desc mat_kul;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id_matkul | varchar(10) | YES | | NULL | |
| nama_matkul | varchar(50) | YES | | NULL | |
| sks | int(1) | YES | | NULL | |
| semester | int(1) | YES | | NULL | |
| dosen | varchar(10) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.03 sec)

Lihat tabel yang ada

[Type text] Page 5

mysql> show tables;
+----------------+
| Tables_in_lab4 |
+----------------+
| mat_kul |
| t_mhs |
+----------------+
2 rows in set (0.00 sec)

Hapus TABEL

DROP(spasi)TABLE(spasi)NAMA_TABEL

mysql> drop table mat_kul;
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+----------------+
| Tables_in_lab4 |
+----------------+
| t_mhs |
+----------------+
1 row in set (0.00 sec)

Buat database baru

mysql> create database coba;
Query OK, 1 row affected (0.02 sec)

Lihat database yang ada

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| coba |
| lab4 |
| mysql |
| performance_schema |
| test |
[Type text] Page 6

+--------------------+
6 rows in set (0.00 sec)

HAPUS DATABASE

mysql> drop database coba;
Query OK, 0 rows affected (0.06 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lab4 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)