• Tidak ada hasil yang ditemukan

Pertemuan 6 PI Database (MySQL)

N/A
N/A
Protected

Academic year: 2018

Membagikan "Pertemuan 6 PI Database (MySQL)"

Copied!
41
0
0

Teks penuh

(1)

Pertemuan 6 PI

Database

(MySQL)

(2)

Woriing with Databases

If you need to store complex

information, ieep the information

very secure, or handle many users

accessing the data at once, a

database is much better than a fat

fle for long-term storage. Also, if

(3)

Understanding database

software

 A database is an electronic fle cabinet

that stores information in an organized manner so that you can fnd it when you need it.

 Technically, the term database refers to

the fle or group of fles that holds the

actual data. The data is accessed by using a set of programs called a Database

Management System (DBMS). Almost all DBMSs these days are Relational

Database Management Systems

(4)

 One of PHP’s strengths is its support for many

diferent DBMSs. PHP supports over 20

databases. It supports the following popular RDBMSs, as well as others that are less well inown:

 IBM DB2

 Informix

 Ingres

 Microsoft SQL Server (MS SQL)

 mSQL

 MySQL

 Oracle

 PostgreSQL

(5)

 In addition, PHP ofers support for ODBC, which

stands for the Open Database Connectivity

standard, a standard database access method developed by Microsoft. Many DBMSs

understand ODBC, particularly Windows DBMSs. Using ODBC support in PHP, you can access

some databases that are not specifcally

supported, such as DB2 and Access. Also, you can use ODBC to access several diferent

databases with the same code. To use ODBC to communicate with a database, the database

(6)

Choosing a RDBMS depends on your needs. You may need to consider some of the following issues:

 Cost: The cost of the RDBMS software ranges from

free to quite pricey. MySQL, mSQL, and PostgreSQL are open source software, meaning they’re free.

Other RDBMSs, such as Sybase, MS SQL Server, and Oracle, are commercial software with prices that

range from moderate to astronomical.

 Features: The features provided by an RDBMS vary.

For example, mSQL has a small set of features, but this may be enough for some purposes. On the other hand, Oracle can do everything but drive your car. In general, the more features the RDBMS has, the more computer resources it requires and the higher its

(7)

 Resources: Some RDBMSs require more resources,

such as disi space and memory, than others. For example, mSQL is very small and lightweight, requiring very little overhead. MySQL was also developed to be small. On the other hand, Oracle, depending on which products and tools you install, can require many resources.

 Support: Commercial software and open source

software provide support diferently:

 Commercial: Commercial software provides a

method for customers to get technical support from the company that sold them the software.

 Open source: Open source software does not

provide a direct phone line to a software company. Open source software is supported by the

(8)

 After you choose which database you’re

going to use, you need to install the

database software and fgure out how to use it. You need to inow how to design and create a database that you can then access from a PHP script. In general, a database has two parts: a structure to hold the data and the data itself.

 The structure consists of the database

itself and tables within the database that hold the data. You need to design the

database structure before you can store data in it. RDBMS tables are organized

(9)

Understanding database

support in PHP

 PHP communicates with databases by

using functions designed specifcally to interact with databases. PHP includes a set of functions for each database it

supports. For example, to communicate with MySQL 4.0 or earlier, you use

functions such as mysql_connect() and mysql_query() and to communicate with MySQL 4.1 or later, you use functions such as mysqli_connect() and

mysqli_query(). To communicate with Sybase, you use functions such as

sybase_connect() and sybase_query().

 By default, PHP includes support for

ODBC. For database support other than ODBC, you must add support for the

(10)

Introduction to MySQL

Relational databases

Database design

SQL

Creating databases

Creating tables

Selecting from, deleting, and

(11)

Relational Databases

 A database is a collection of tables

 Columns defne attributes of the data

 All data in a column must have the same data type

 A record is stored in a row

First Name Last Name Phone

Nadia Li 2687

Madhu Charu 7856

Ajuma Kinsaka 4489

Wade Randal 5257

Helen Clark 2147

Employees

table name

(12)

MySQL Data Types

Numeric Data Types (INT, TINYINT,

SMALLINT, MEDIUMINT, BIGINT,

FLOAT (M,D), DOUBLE(M, D),

DECIMAL(M,D))

Date and Time Types (DATE,

DATETIME, TIMESTAMP, TIME,

YEAR(M))

String Types (CHAR(M),

VARCHAR(M), BLOB or TEXT,

TINYBLOB or TINYTEXT,

(13)

Table Creation Syntax

The table creation command requires:

Name of the table

Names of felds

Defnitions for each feld

The generic table creation syntax is

(14)

Example

mysql> CREATE TABLE

grocery_inventory (

-> id int not null primary iey

auto_increment,

-> item_name varchar (50) not null,

-> item_desc text,

-> item_price foat not null,

-> curr_qty int not null

(15)

Using the INSERT

Command

INSERT INTO table_name (column

list) VALUES (column values);

Example:

insert into grocery_inventory (id,

item_name, item_desc, item_price,

curr_qty) values ('1', 'Apples',

'Beautiful, ripe apples.', '0.25',

1000);

insert into grocery_inventory values

(16)

Using the SELECT

Command

SELECT expressions_and_columns

FROM table_name [WHERE

some_condition_is_true] [ORDER BY

some_column [ASC | DESC]] [LIMIT

ofset, rows]

Example:

SELECT * FROM grocery_inventory;

SELECT id, item_name, curr_qty

(17)

SELECT * FROM

grocery_inventory;

+---+---+---+---+---+

| id | item_name | item_desc | item_price | curr_qty |

+---+---+---+---+---+

| 1 | Apples | Beautiful, ripe apples. | 0.25 | 1000 |

| 2 | Bunches of Grapes | Seedless grapes. | 2.99 | 500 |

| 3 | Bottled Water (6-paci) | 500ml spring water. | 2.29 | 250 |

(18)

SELECT id, item_name,

curr_qty FROM

grocery_inventory;

+----+---+---+

| id | item_name |

curr_qty |

+----+---+---+

| 1 | Apples | 1000

|

| 2 | Bunches of Grapes | 500

|

| 3 | Bottled Water (6-paci) | 250

|

(19)

Ordering SELECT

Results

 mysql> select id, item_name, curr_qty from grocery_inventory order by item_name;

 select id, item_name, curr_qty from grocery_inventory order by item_name desc;

Limiting your Result:

LIMIT startnumber,numberofrows

 The frst row that you want to retrieve is

startnumber, and the number of rows that you

want to retrieve is numberofrows. If startnumber is not specifed, 1 is assumed. To select only the frst three members who live in Texas, use this query:

select id, item_name, curr_qty from grocery_inventory -> order by curr_qty limit 3;

 SELECT * FROM grocery_inventory ORDER BY curr_qty LIMIT 0, 3;

 SELECT * FROM grocery_inventory ORDER BY curr_qty LIMIT 3, 3;

(20)

Using WHERE in Your

Queries

SELECT expressions_and_columns

FROM table_name [WHERE

some_condition_is_true]

mysql> select * from

grocery_inventory where curr_qty =

500;

+----+---+---+---+---+

| id | item_name | item_desc | item_price | curr_qty |

+----+---+---+---+---+

| 2 | Bunches of Grapes | Seedless grapes. | 2.99 | 500 |

| 5 | Pears | Anjou, nice and sweet. | 0.5 | 500 |

(21)

Using Operators in WHERE

Clauses

 select * from grocery_inventory where item_price between 1.50

and 3.00;

+----+---+---+---+---+

| id | item_name | item_desc | item_price | curr_qty |

+----+---+---+---+---+

| 2 | Bunches of Grapes | Seedless grapes. | 2.99 | 500 |

| 3 | Bottled Water (6-paci) | 500ml spring water. | 2.29 | 250 |

| 4 | Bananas | Bunches, green. | 1.99 | 150 |

+----+---+---+---+---+

 Operator:

= (Equal to)

!= (Not equal to)

<= (Less than or equal to) < (Less than)

(22)

String Comparison Using

LIKE

mysql> select * from

grocery_inventory where item_name

liie 'A%';

%— Matches multiple characters

_— Matches exactly one character

+----+---+---+---+---+

| id | item_name | item_desc | item_price | curr_qty |

+----+---+---+---+---+

| 1 | Apples | Beautiful, ripe apples. | 0.25 | 1000 |

| 6 | Avocado | Large Haas variety. | 0.99 | 750 |

(23)

Selecting from Multiple

Tables

 mysql> select * from fruit;  +----+---+

 | id | fruitname |  +----+---+  | 1 | apple |  | 2 | orange |  | 3 | grape |  | 4 | banana |  +----+---+

 mysql> select * from color;  +----+---+

 | id | colorname |  +----+---+  | 1 | red |  | 2 | orange |  | 3 | purple |  | 4 | yellow |  +----+---+

(24)

 mysql> select * from fruit, color;  +----+---+----+---+

 | id | fruitname | id | colorname |  +----+---+----+---+

 | 1 | apple | 1 | red |  | 2 | orange | 1 | red |  | 3 | grape | 1 | red |  | 4 | banana | 1 | red |

(25)

 mysql> select fruitname, colorname from fruit,

color where fruit.id = color.id;

 +---+---+  | fruitname | colorname |  +---+---+  | apple | red |  | orange | orange |  | grape | purple |  | banana | yellow |  +---+---+

 mysql> select id, fruitname, colorname from

fruit, color where fruit.id = color.id; ERROR 1052: Column: 'id' in feld list is ambiguous

 mysql> select fruit.id, fruitname, colorname

(26)

Using JOIN

mysql> select fruitname, colorname

from fruit inner join color on fruit.id

= color.id;

mysql> select frstname, lastname,

email from master_name left join

email on master_name.name_id =

email.name_id;

mysql> select frstname, lastname,

(27)

 mysql> select name_id, frstname, lastname from

master_name;

+---+---+---+

| name_id | frstname | lastname | +---+---+---+

(28)

mysql> select name_id, email from

email;

(29)

mysql> select frstname,

lastname, email fom

master_name left join email on

master_name.name_id =

email.name_id;

+---+---+---+ | frstname | lastname | email | +---+---+---+ | John | Smith | NULL |

| Jane | Smith | NULL | | Jimbo | Jones | NULL | | Andy | Smith | NULL | | Chris | Jones | NULL |

| Anna | Bell | annabell@aol.com | | Jimmy | Carr | NULL |

| Albert | Smith | NULL |

(30)

 mysql> select frstname, lastname, email

from master_name right join email on

master_name.name_id = email.name_id; +---+---+---+ | frstname | lastname | email |

+---+---+---+ | John | Doe | jdoe@yahoo.com | | Anna | Bell | annabell@aol.com |

(31)

Using the UPDATE

Command to Modify

Records

 UPDATE table_name SET column1='new

value', column2='new value2' [WHERE some_condition_is_true]

Example:

 mysql> update fruit set fruit_name =

'grape';

Conditional Update

 mysql> update fruit set fruit_name =

'grape' where fruit_name = 'grappe';

 mysql> update grocery_inventory set

(32)

REPLACE INTO table_name (column

list) VALUES (column values);

(33)

Using the DELETE

Command

 DELETE FROM table_name [WHERE

some_condition_is_true] [LIMIT rows]

 mysql> delete from fruit;

Conditional DELETE

 mysql> delete from fruit where status =

'rotten';

 DELETE FROM table_name [WHERE

some_condition_is_true] [ORDER BY

some_column [ASC | DESC]] [LIMIT rows]

 mysql> delete from access_log order by

(34)

DIAGRAM RELASI ANTAR TABEL

(DbRental)

TbKonsumen

Kode Konsumen * Nama Konsumen No KTP

No Telp Alamat

TbTransaksi

Kode Transaksi * tanggal

Kode Konsumen** Kode Mobil ** Lama Pinjam Uang Muka

TbMobil

(35)

STRUKTUR TABEL

Database name: DbRENTAL Table name : TbKonsumen

Primary Key : KodeKonsumen Foreign Key :

-No Field Name Field Type Field Size

1 KodeKonsumen TEXT 4

2 NamaKonsumen TEXT 30

3 NoKTP TEXT 20

4 NoTELP TEXT 20

(36)

Database name: DbRENTAL Table name : TbMobil

Primary Key : KodeMobil Foreign Key :

-No Field Name Field Type Field Size

1 KodeMobil TEXT 3

2 JenisMobil TEXT 20

3 NoPolisi TEXT 10

(37)

Database name: DbRENTAL Table name : TbTransaksi

Primary Key : KodeTransaksi

Foreign Key : KodeKonsumen, KodeMobil

No Field Name Field Type Field Size

1 KodeTransaksi TEXT 8

2 Tanggal DATE

3 KodeKonsumen TEXT 4

4 KodeMobil TEXT 3

5 LamaPinjam NUMBER

(38)

Kode Konsumen Nama Konsumen No KTP No TELP Alamat

K001 Akas AL 105019310578300

5 02270253382 Kampung Baru 19B RT.2/5 ujungberung Bandung 40611

K002 Nurwan 105019310578302

6 0227805766 Jl. Kaum Kaler 20 RT.2/5 Ujungberung Bandung 40611

K003 Susilawati 105019310578303

7 085624425252 Jl. A Yani 800 Bandung

K004 Dinda 105019310578302

8 0227812344 Jl. AH Nasution 105 Bandung

K005 Yulvianisa 105019310578301

9 0227831418 Jl. Sindang laya 327A Bandung

K006 Wilanda 105019310578301

1 081802058289 Jl. Asia Afrika 8 Bandung

(39)

Kode Mobil Jenis Mobil No Polisi Tarif Sewa

M01 MiniBus/Suzuki Carry1.0 D 5854 DU Rp. 200,000

M02 Minibus/Espass D 4588 FH Rp. 250,000

M03 Sedan/Corolla Altis1.8J D 1000 KK Rp. 380,000

M04 Sedan/Lancer D 9955 BK Rp. 400,000

M05 MiniMPV/Honda Jazz D 5523 JJ Rp. 250,000

(40)

Kode Transaksi Tanggal Kode Konsumen Kode Mobil Lama Pinjam Uang Muka

F-02-001 2/9/2007 K001 M02 3 Rp. 300,000

F-02-002 2/9/2007 K002 M04 7 Rp. 1,000,000

F-02-003 8/9/2007 K004 M02 1 Rp. 50,000 F-02-004 11/9/2007 K003 M05 4 Rp. 200,000

F-02-005 11/9/2007 K002 M01 2 Rp. 100,000

(41)

Quiz

1. Buat Syntax SQL untui membuat tabel

Konsumen, Mobil dan Transaisi!

2. Buat Syntax SQL untui menginputian data

ie tabel ionsumen!

3. Buat Syntax SQL untui mengedit Nama

Susilawati menjadi Susi Susilawati

4. Buat Syntax SQL untui Menampilian Kode

Transaisi, Tanggal, Nama Konsumen, Jenis Mobil, Lama Pinjam, dan Uang Muia?

5. Buat Syntax SQL untui Menampilian

Gambar

table nameEmployees
Table name : TbKonsumen
Table name : TbMobil
Table name : TbTransaksi

Referensi

Dokumen terkait

Sementara dalam hukum positif yang berlaku tidak mensyaratkan adanya sanksi atau hukuman baik dalam dalam bentuk kurungan penjara maupun santunan atau ganti rugi terhadap

Keywords: ​ ​ teenage pregnancy; low birth weight; less chronic energy; ​ anemia. 1 Departemen Biostatistik, Epidemiologi, dan Populasi Kesehatan, Fakultas Kedokteran,

Laporan Akhir ini disusun berdasarkan apa yang telah kami lakukan pada saat pengerjaan alat simulasi baik dari proses pembuatan, proses pengujian dan perencanaan

Tujuan dari penelitian ini adalah untuk mengetahui apakah sistem pengukuran kinerja dan sistem reward berpengaruh terhadap kinerja manajerial pada UD.Surya Chemical dan

Pada penelitian ini, yang menjadi kajian utamanya adalah penataan kurikulum prodi- prodi yang ada di jurusan Teknik Elektro – UNP dengan tujuan, untuk memastikan

It can be concluded that this capacity building and workshop can improve the teachers’ understanding in preparing local government financial statement so that the teachers’ be ready

Segala puji dan syukur penulis panjatkan kehadirat Allah SWT, karena berkat rahmat, hidayah dan karunia-Nya maka penulis dapat menyelesaikan skripsi ini yang

Manajer Investasi melalui Bank Kustodian pada Tanggal Emisi wajib menyerahkan Unit Penyertaan ETF PHILLIP MSCI INDONESIA EQUITY INDEX kepada Dealer Partisipan dan/atau Sponsor