Вы находитесь на странице: 1из 20

SQL

Muhtadin, ST. MT.

Basis Data
Masis
DataMultimedia
Multimedia &&Lab.
Lab.By
By::Muhtadin
Muhtadin

SQL

Basis Data Multimedia & Lab. By : Muhtadin

Identifying vs Non Identifying Relationship

Identifying Relationship :
Primary key dari child table menjadi foreign key pada parent table
Foreign key ikut serta sebagai primary key pada parent table
Non-Identifying Relationship
Primary key dari child table dimasukkan sebagai foreign key pada
parent table
Namun, foreign key tersebut tidak ikut menjadi primary key pada
parent table.
Non-identifying relationship dapat berupa optional atau mandatory,
yang berarti bahwa nilai foreign key tersebut dapat berupa NULL
atau tidak boleh NULL

Basis Data Multimedia & Lab. By : Muhtadin

Identifying vs Non Identifying Relationship Contoh


Identifying Relationship :
A Person has one or more phone numbers. If they had just one
phone number, we could simply store it in a column of Person.
Since we want to support multiple phone numbers, we make a
second table PhoneNumbers, whose primary key includes
the person_id referencing the Person table
Non-Identifying Relationship :
A foreign key on Person.state referencing the primary key
of States.state. Person is a child table with respect to States. But a
row in Person is not identified by its state attribute. I.e. state is not
part of the primary key of Person

Basis Data Multimedia & Lab. By : Muhtadin

Disable foreign key checks


Foreign key memastikan data integrity
Terkadang, me-non aktifkan foreign key checks sangat berguna,
mis. Ketika memasukkan data (import data) dalam tabel yang
memiliki foreign key.

Disable foreign key checks :


SET foreign_key_checks = 0
Enable foreign key checks :
SET foreign_key_checks = 1

Basis Data Multimedia & Lab. By : Muhtadin

SQL ?
SQL dibaca sequel
Didukung oleh banyak commercial database systems
Standardized ada update dan selalu diperbaharui
Interactive via GUI atau menggunakan command prompt, dapat
juga dilakukan dengan memasukkan kedalam kode program.
Declarative, berdasarkan relational algebra

Basis Data Multimedia & Lab. By : Muhtadin

SQL ?
Data Definition Language (DDL)
Create table
Drop Table

Data Manipulation Language (DML)


Select
Insert
Delete
Update

Commands lainnya
Indexes, constrains, views, triggers, transaction,
Basis Data Multimedia & Lab. By : Muhtadin

SELECT Statement

SELECT A1, A2, An

FROM R1, R2, Rn

[INNER | LEFT |RIGHT] JOIN R ON conditions]


WHERE conditions

GROUP BY group

HAVING group_conditions

ORDER BY column_1 [ASC | DESC]

LIMIT offset, row_count

Basis Data Multimedia & Lab. By : Muhtadin

what to return
relations
combine filter
group rows
filters group
specifies orde
number of rows

Filter Rows Menggunakan MySQL WHERE


Operator :
>,<,=
BETWEEN
LIKE
Menggunakan wild char (% and _)
Dapat dikombinasikan dengan operator NOT
IN
IS NULL

Basis Data Multimedia & Lab. By : Muhtadin

MySQL ORDER BY
SELECT col1, col2,...
FROM tabel
ORDER BY col1 [ASC|DESC], col2 [ASC|DESC],...

Contoh dari database star_enterprise :


SELECT contactLastname,
contactFirstname
FROM customers
ORDER BY contactLastname;
SELECT orderNumber, status
FROM orders
ORDER BY FIELD(status, 'In Process',
'On Hold',
'Cancelled',
'Resolved',
'Disputed',
'Shipped');
Basis Data Multimedia & Lab. By : Muhtadin

10

MySQL GROUP BY
SELECT col1, col2,..,aggregate_function(expression)
FROM tabel
WHERE conditions
GROUP BY col1, col2, ...

Contoh dari database star_enterprise :


SELECT status, count(*)
FROM orders
GROUP BY status

SELECT status, count(*)


FROM orders
GROUP BY status DESC;

Basis Data Multimedia & Lab. By : Muhtadin

11

MySQL HAVING
SELECT col1, col2,..,aggregate_function(expression)
FROM tabel
GROUP BY col1, col2, ...
HAVING conditions

Contoh dari database star_enterprise :


SELECT ordernumber,
sum(quantityOrdered) AS itemsCount,
sum(priceeach) AS total
FROM orderdetails
group by ordernumber
having total > 1000 AND itemsCount > 600

Basis Data Multimedia & Lab. By : Muhtadin

12

MySQL Subquery

Contoh dari database star_enterprise :


SELECT customerNumber,
checkNumber,
amount
FROM payments
WHERE amount > (
SELECT AVG(amount)
FROM payments
)
Basis Data Multimedia & Lab. By : Muhtadin

13

Inner Join

Contoh dari database star_enterprise :


SELECT productCode,
productName,
textDescription
FROM products T1
INNER JOIN productlines T2 ON T1.productline =
T2.productline

Basis Data Multimedia & Lab. By : Muhtadin

14

Inner Join - Contoh


tb_anggota

tb_buku

tb_penerbit

Basis Data Multimedia & Lab. By : Muhtadin

15

Inner Join - example


SELECT
A.nama, A.alamat, B.judul, B.pengarang,
B.idtb_penerbit, C.nama
FROM
((tb_anggota A inner join tb_buku B ON
A.idtb_buku = B.idtb_buku) inner join tb_penerbit C on
B.idtb_penerbit = C.idtb_penerbit)

Equivalent dengan SQL:


SELECT

A.nama, A.alamat, B.judul, B.pengarang,


B.idtb_penerbit, C.nama
FROM
((tb_anggota A inner join tb_buku B) inner join
tb_penerbit C )
WHERE
(A.idtb_buku = B.idtb_buku) and (C.idtb_penerbit
= B.idtb_penerbit)
Basis Data Multimedia & Lab. By : Muhtadin

16

Stored Procedure

Structure

Stored Procedure Name

Parameter

IN, OUT, INOUT

Body
Used by Call Statement

Basis Data Multimedia & Lab. By : Muhtadin

17

Stored Procedure
Contoh dari database star_enterprise :
DELIMITER $$
CREATE PROCEDURE GetCustomerLevel(
in p_customerNumber int(11),
out p_customerLevel varchar(10))
BEGIN
DECLARE creditlim double;
SELECT creditlimit INTO creditlim
FROM customers
WHERE customerNumber = p_customerNumber;
IF creditlim > 50000 THEN
SET p_customerLevel = 'PLATINUM';
ELSEIF (creditlim <= 50000 AND creditlim >= 10000) THEN
SET p_customerLevel = 'GOLD';
ELSEIF creditlim < 10000 THEN
SET p_customerLevel = 'SILVER';
END IF;
END$$

Basis Data Multimedia & Lab. By : Muhtadin

18

Reff
Mysql tutorial : www.mysqltutorial.org

Basis Data Multimedia & Lab. By : Muhtadin

19

THANK YOU

Basis Data Multimedia & Lab. By : Muhtadin

20

Вам также может понравиться