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)