Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

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

Presentasi serupa


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

1 SQL 2

2

3 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

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

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

6 JOIN Ingin membuat list item yang dijual dan jumlah stoknya

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

8 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

9 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

10 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;

11 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;

12 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);

13 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

14 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

15 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';

16 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)

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

18 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) );

19 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) );

20 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;

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

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

23 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)

24 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 1300 12. 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‘)

25 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

26 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) > 1200 17. 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 )

27 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")

28 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

29 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

30 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

31 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‘

32 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

33 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

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

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


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

Presentasi serupa


Iklan oleh Google