SQL 2. Database TRANSACTION Tabel yang terlibat : Customer berisi data pelanggan (nama, alamat, dll) OderInfo berisi info pemesanan oleh pelanggan (tgl.

Slides:



Advertisements
Presentasi serupa
Aljabar Relasional Materi pertemuan.
Advertisements

SQL.
MATA KULIAH : “LOGIKA DAN ALGORITMA”
KUMPULAN PERINTAH SQL PENDY.
PERINTAH SQL.
DDL(DATA DEFINITION LANGUAGE) DML (DATA MANIPULATION LANGUAGE)
Praktikum Database Query Tingkat Lanjut Abdul Kadir.
Bahasa Pada Model Data Relasional
SQL ADVANCEADVANCE. SQL Data Type MySQL Text Type : 9/7/2014By : Suwondo, S.Kom2.
RELASI & JOIN.
SQL: Queries II, Constraints & Triggers (Chap. 5 – Ramakrishnan)
DML Lanjutan Pertemuan Minggu Ke-10.
Model & Aljabar Relasional
SQL.
PRAKTIKUM BASIS DATA (4) TEKNIK INFORMATIKA UNIVERSITAS MERCU BUANA
Pertemuan : 8 Basis Data Terapan
SQL Part 3 Latar Belakang Desain Tabel Normalisasi Aljabar relasional
Pertemuan : Basisdata Terapan
Aljabar Relasional Pertemuan 22.
JOINED TABLE Untuk menampilkan data dari dua atau lebih tabel, maka tabel – tabel tersebut harus dihubungkan terlebih dahulu  JOIN.
DATA AGREGAT MENGGUNAKAN GROUP FUNCTIONS
Microsoft SQL Server DDL dan DML dasar
SQL.
Data Definition Language dan Data Manipulation Language
Praktikum Database Subquery Lanjutan dan View Abdul Kadir.

SQL (Structured Query Language) Materi Pertemuan
MANAJEMEN BASIS DATA Pertemuan 6 SQL - SELECT.
P E R T E M U A N 12 SISTEM BASIS DATA.
Data Types Data Definition Language Referential Constraint SQL Query
Advanced Query Formulation dengan SQL
SQL-Aggregate dengan Fungsi GROUP, HAVING dan subQuery
SQL: DML (2) Basis Data Pertemuan 07.
MANAJEMEN BASIS DATA Pertemuan 8 SUBQUERY.
© 2010 © Rizki Arif Firdaus © 2010 © 2010.
PERTEMUAN 10 QUERY KOMERSIAL LANJUTAN Agus Riyanto, S.Kom.
SQL (Structured Query Language)
Basis Data Terapan Antonius Wahyu Sudrajat, S. Kom., M.T.I Perintah Drop Sintak Menghapus Tabel Drop Aturan: Harus dimulai dari table yang paling children.
Pertemuan 6 Aljabar Relational (2) Betha Nurina Sari,M.Kom.
Aljabar Relasi SBD.
BAB 4 QUERY FORMULATION with SQL
SQL (Structure Query Language)
Structure Query Language
Rekayasa Perangkat Lunak Materi 3 (Database)
FUNGSI-FUNGSI AKSES MySql
SQL.
Operasi Relasional Basis Data
Bahasa query terpan.
Pemrograman Visual Akuntansi III
Praktikum Database Subquery Lanjutan dan View
SQL (Structure Query Language)
Query.
JOIN.
Using Subqueries to Solve Queries
- Modul 5 - Query Lanjut 2/20/2018 7:46 PM
SQL OVERVIEW.
SQL (Structured Query Language)
Sistem Manajemen Basis Data
Structured Query Language (SQL)
SQL Data Manipulation IK203 Sistem Basis Data Pertemuan #12
SQL (2) (Structure Query Language)
Structured Query Language
Query dalam sql server Pertemuan 6.
Sesi VI Query dengan SQL
Aplikasi Database Dosen : Sarwo, Skom,.Mkom Url :
Basis Data Bab 3 Structured Query Language (SQL).
Praktikum Sistem Basis Data - 2
Praktikum 2 - Sistem Basis Data 1
BAHASA BASIS DATA Retreiving Data dan Manipulation Function
Pengantar Teknologi SIM 2 (pertemuan 7)
Transcript presentasi:

SQL 2

Database TRANSACTION Tabel yang terlibat : Customer berisi data pelanggan (nama, alamat, dll) OderInfo berisi info pemesanan oleh pelanggan (tgl pesan, tgl kirim, dsb) OrderLine berisi info barang-barang yang dipesan beserta jumlahnya Item berisi info detail setiap barang Stock berisi info jumlah stok barang Barcode berisi kode barang dan barcode-nya

Select From Multiple Table Ingin membuat list item yang dijual dan jumlah stoknya

SELECT i.description, s.quantity FROM stock s, item i WHERE s.item_id = i.item_id;

JOIN Ingin membuat list item yang dijual dan jumlah stoknya

Join SELECT (column_list) FROM table_name [INNER | {LEFT |RIGHT | FULL} OUTER] JOIN table_name ON qualification_list WHERE …

LEFT JOIN SELECT i.description, s.quantity FROM item i LEFT JOIN stock s ON i.item_id = s.item_id; Menampilkan seluruh baris pada tabel yang disebut pada bagian kiri, tetapi menampilkan nilai null untuk setiap kolom yang tidak terdapat relasinya pada tabel sisi kanan

RIGHT JOIN SELECT i.description, s.quantity FROM item i RIGHT JOIN stock s ON i.item_id = s.item_id; Menampilkan seluruh baris pada tabel yang disebut pada bagian kanan, tetapi menampilkan nilai null untuk setiap kolom yang tidak terdapat relasinya pada tabel sisi kiri

INNER JOIN Menampilkan hanya baris yang cocok dengan kondisi pencarian SELECT i.description, s.quantity FROM item i INNER JOIN stock s ON i.item_id = s.item_id; Bisa juga dengan ditulis JOIN saja Hasilnya sama dengan statement SELECT i.description, s.quantity FROM item i, stock s where i.item_id = s.item_id;

FULL JOIN Mengembalikan semua baris (baik ada pasangannya atau tidak) pada kedua sisi SELECT i.description, s.quantity FROM item i FULL JOIN stock s ON i.item_id = s.item_id;

Nested Subquery Adalah ekspresi select-from-where yang nested dengan statemen select yang lain Contoh SELECT * FROM item WHERE cost_price > (SELECT avg(cost_price) FROM item) AND sell_price < (SELECT avg(sell_price) FROM item);

Subquery dengan IN/NOT IN Ingin mengetahui item beserta jumlah stoknya, yang cost price-nya > 10 SELECT s.item_id, s.quantity FROM stock s, item i WHERE i.cost_price > 10 AND s.item_id = i.item_id Atau menggunakan klausa IN/NOT IN SELECT * FROM stock WHERE item_id IN (SELECT item_id FROM item WHERE cost_price > 10

Subquery dengan EXIST/NOT EXIST Klausa exists mengembalikan true jika argumen subquery tidak kosong Ingin melist semua customer yang melakukan pemesanan SELECT fname, lname FROM customer c WHERE EXISTS (SELECT orderinfo_id FROM orderinfo oi WHERE oi.customer_id = c.customer_id); Bisa juga SELECT distinct fname, lname FROM customer c, orderinfo o where c.customer_id=o.customer_id

Ingin mengetahui tanggal pemesanan untuk customer yang tinggal di Bingham SELECT * FROM orderinfo o, (SELECT * FROM customer c WHERE town = 'Bingham') c WHERE c.customer_id = o.customer_id; Bisa juga SELECT * FROM orderinfo o,customer c WHERE c.customer_id = o.customer_id and c.town='Bingham';

Operasi Himpunan Find all customers who have a loan, an account, or both: (select customer_name from depositor) union (select customer_name from borrower) Find all customers who have both a loan and an account. (select customer_name from depositor) intersect (select customer_name from borrower) Find all customers who have an account but no loan. (select customer_name from depositor) except (select customer_name from borrower)

Constraint Yaitu batasan atau syarat dari data yang diisikan Ada 2 macam : – Table Constraint – Column Constraint

Column Constraint Batasan hanya berlaku hanya pada suatu kolom CREATE TABLE testcolcons ( colnotnull INT NOT NULL, colunique INT UNIQUE, colprikey INT PRIMARY KEY, coldefault INT DEFAULT 42, colcheck INT CHECK( colcheck < 42) );

Table Constraint Table-level constraints can refer to more than one column. Table-level constraints are listed after all the columns. CREATE TABLE ttconst ( mykey1 int, mykey2 int, mystring varchar(15), CONSTRAINT cs1 CHECK (mystring <> ''), CONSTRAINT cs2 PRIMARY KEY(mykey1, mykey2) );

View When you have a complex database, or sometimes when you have various users with different permissions, you need to create the illusion of a table, or a view. Syntax CREATE VIEW name-of-view AS select- statement;

CREATE VIEW item_price AS SELECT item_id, description, sell_price FROM item; SELECT * FROM item_price;

Delete or Replace View Delete View DROP VIEW name-of-view Replace View CREATE OR REPLACE VIEW name-of-view AS select-statement

Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower(customer_name, loan_number)

11. Find customer name with loan amounts between $500 and $1300 select b.customer_name from borrower b, loan l where b.loan_number = l.loan_number and l.amount between 500 and Find the name, loan number and loan amount of all customers having a loan at the Perryridge or downtown branch. select customer_name, borrower.loan_number, amount from borrower, loan where (borrower.loan_number = loan.loan_number and branch_name = 'Perryridge‘) or (borrower.loan_number = loan.loan_number and branch_name = ‘Downtown‘)

13. Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id. select customer_name, borrower.loan_number as loan_id, amount from borrower, loan where borrower.loan_number = loan.loan_number 14. List in alphabetic order the names of all customers having a loan in Perryridge branch select distinct customer_name from borrower, loan where borrower loan_number = loan.loan_number and branch_name = ' Perryridge ' order by customer_name

15. Find the number of depositors for each branch. select branch_name, count (distinct customer_name) from depositor, account where depositor.account_number = account.account_number group by branch_name 16. Find the names of all branches where the average account balance is more than $1,200. select branch_name, avg (balance) from account group by branch_name having avg (balance) > Find all customers who have both an account and a loan at the bank. select distinct customer_name from borrower where customer_name in (select customer_name from depositor )

18. Find all customers who have a loan at the bank but do not have an account at the bank select distinct customer_name from borrower where customer_name not in (select customer_name from depositor ) 19. Find all customers who have a loan at Perryridge branch and also have an account select distinct customer_name from (select customer_name from borrower,loan where borrower.loan_number=loan.loan_number and loan.branch_name="perryridge") where customer_name in (select customer_name from depositor ) 20. Find all customers who have both an account and a loan at the Perryridge branch select distinct customer_name from (select customer_name from borrower,loan where borrower.loan_number=loan.loan_number and loan.branch_name="perryridge") where customer_name in (select customer_name from account,depositor where account.account_number=depositor.account_number and account.branch_name="perryridge")

customer (customer_id, title, fname,lname) orderinfo(orderinfo_id, customer_id, date_placed) orderline(orderinfo_id, item_id, quantity) item(item_id, description, cost_price, sell_price) stock(item_id, quantity) barcode(barcode_ean, item_id) Bagaimana query-nya? Melist semua tgl pemesanan beserta nama customernya Melist semua item beserta barcode-nya Mengetahui item apa yang dibeli oleh customer bernama Ann Stones Melist tanggal pemesanan, deskripsi item yang dipesan, dan stoknya saat itu

customer (customer_id, title, fname,lname) orderinfo(orderinfo_id, customer_id, date_placed) orderline(orderinfo_id, item_id, quantity) item(item_id, description, cost_price, sell_price) stock(item_id, quantity) barcode(barcode_ean, item_id) Melist semua tgl pemesanan beserta nama customernya select c.fname, c.lname, o.date_placed from customer c, orderinfo o where c.customer_id=o.customer_id

customer (customer_id, title, fname,lname) orderinfo(orderinfo_id, customer_id, date_placed) orderline(orderinfo_id, item_id, quantity) item(item_id, description, cost_price, sell_price) stock(item_id, quantity) barcode(barcode_ean, item_id) Melist semua item beserta barcode-nya select i.item_id, i.description, b.barcode_ean from item i, barcode b where b.item_id=i.item_id

customer (customer_id, title, fname,lname) orderinfo(orderinfo_id, customer_id, date_placed) orderline(orderinfo_id, item_id, quantity) item(item_id, description, cost_price, sell_price) stock(item_id, quantity) barcode(barcode_ean, item_id) Mengetahui item apa yang dibeli oleh customer bernama Ann Stones select distinct i.item_id, i.description, c.fname, c.lname from item i, orderline o, orderinfo oi, customer c where c.customer_id=oi.customer_id and oi.orderinfo_id=o.orderinfo_id and i.item_id=o.item_id and c.fname='Ann' and c.lname='Stones‘

customer (customer_id, title, fname,lname) orderinfo(orderinfo_id, customer_id, date_placed) orderline(orderinfo_id, item_id, quantity) item(item_id, description, cost_price, sell_price) stock(item_id, quantity) barcode(barcode_ean, item_id) Melist tanggal pemesanan, deskripsi item yang dipesan, dan stoknya saat itu select o.date_placed, i.description, s.quantity from orderinfo o, orderline d, item i, stock s where o.orderinfo_id=d.orderinfo_id and d.item_id=i.item_id and i.item_id=s.item_id

Ingin mengetahui kota apa saja yang ditinggali oleh lebih dari dua orang customer Ingin mengetahui item apa yang cost-pricenya lebih besar dari rata-rata

Ingin mengetahui kota apa saja yang ditinggali oleh lebih dari dua customer SELECT count(*), town FROM customer GROUP BY town HAVING count(*) > 2

Ingin mengetahui item apa yang cost-pricenya lebih besar dari rata-rata SELECT * FROM item WHERE cost_price > (SELECT avg(cost_price) FROM item)