0% found this document useful (0 votes)
44 views

SQL 2006

This document discusses the key components of SQL including data definition language, data manipulation language, relation schemas, basic SQL SELECT commands involving the select, from, where clauses, string operations, ordering results, set operations, aggregate functions, nested subqueries, views, and modification of databases using deletion statements. SQL is used to query, manipulate, and define relational databases through interaction with tables, rows, and columns.

Uploaded by

Mur Haban
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

SQL 2006

This document discusses the key components of SQL including data definition language, data manipulation language, relation schemas, basic SQL SELECT commands involving the select, from, where clauses, string operations, ordering results, set operations, aggregate functions, nested subqueries, views, and modification of databases using deletion statements. SQL is used to query, manipulate, and define relational databases through interaction with tables, rows, and columns.

Uploaded by

Mur Haban
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

SQL

Structured Query Language

Parts of SQL Language Data-definition language (DDL)


Interactive data-manipulation language (DML) Embedded DML. Bentuk embedded SQL dirancang untuk digunakan dalam GPL : Cobol, Pascal, C, dll. View definition Authorization Integrity Transaction control.

Relation schemas
Branch_schema = (branch_name, branch_city, assets) Customer_schema = (customer_name, customer_street, customer_city) Loan_schema = (branch_name, loan_number, amount) Borrower_schema = (customer_name, loan_number) Account_schema = (branch_name, account_number, balance) Depositor_schema = (customer_name, account_number)

Basic Structure
select A1, A2, , An from r1, r2, , rm where P Ekuivalen dengan ekspresi aljabar relasional A1,
A2, , An(P

(r1 r2 rm))

Basic SQL SELECT command


SELECT FROM JOIN WHERE columns tables conditions criteria What do you want to see ? What tables are involved ? How are the tables joined ? What are the constraints ?

The Select Clause


select branch_name from loan select distinct branch_name from loan select all branch_name from loan select branch_name, loan_number, amount*100 from loan

The Where Clause


select loan_number from loan where branch_name=STIE and amount>1200 select loan_number from loan where amount between 90000 and 100000

The From Clause


select distinct customer_name, borrower.loan_number from borrower, loan where borrower.loan_number=loan.loan_number

select distinct customer_name, borrower.loan_number from borrower, loan where borrower.loan_number=loan.loan_number and branch_name=STIE

Rename Operation
Bentuk umum : old_name as new_name select distinct customer_name, borrower.loan_number as loan_id from borrower, loan where borrower.loan_number=loan.loan_ number and branch_name=STIE

Tuple Variables
select distinct customer_name, T.loan_number from borrower as T, loan as S where T.loan_number=S.loan_number
select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_name=UGM

String Operations
Pattern matching : operator like, dengan dua karakter khusus : Percent (%) : % karakter matches sebarang substring. Underscore ( _ ) : karakter _ matches sebarang karakter

String Operations (Cont)


select customer_name from customer where customer_street like %bumi% Note : ada like ada counterpart nya not like

String Operations (Cont)

Functions on character strings : concatenation (biasanya ||), menentukan panjang strings, meng-extract substrings, mengkonversi antara uppercase dan lowercase, dst.

Ordering the Display of Tuples select distinct customer_name,


borrower.loan_number as loan_id from borrower, loan where borrower.loan_number=loan.loan_num ber and branch_name=STIE oder by customer_name
select * from loan order by amount desc, loan_number asc

Duplicates
Diketahui multiset relation r1 dan r2, 1) Jika ada c copies t1 r1, dan t1 memenuhi pemilihan , maka ada c1 copies dari t1 (r1). 2) Untuk masing-masing copy dari t1 r1, ada copy dari tuple A(t1) dalam A(r1), di mana A(t1) menunjukkan projeksi dari tuple tunggal t1. 3) Jika ada c1 copies dari t1 r1, dan c2 copies dari t2 r2, ada c1*c2 copies dari t1.t2 r1 r2.

Set Operations
Union Operasi union secara otomatis menghilangkan duplicates union all. Intersect Seperti operasi union, jika ingin mempertahankan duplicates, digunakan intersect all. Except Seperti operasi union dan intersection (dalam memperlakukan duplicates).

Aggrregate Functions
select avg(balance) from account where branch_name=UGM select branch_name, avg(balance) from account group by branch_name having avg(balance) > 1200

Aggrregate Functions (cont)


select branch_name, count(distinct customer_name) from depositor, account where depositor.account_number = account.account_number group by branch_name select branch_name, avg(balance) from account group by branch_name having avg(balance) > 1200

Aggrregate Functions (cont)


select avg(balance) from account select count(*) from customer
SQL tidak mengijinkan distinct dengan count(*). distinct syah digunakan dengan max dan min. Jika klausa where dan having muncul bersama dalam query yang sama, predikat dalam klausa where yang pertama dikerjakan (diterapkan).

Aggrregate Functions ( cont ) select depositor.customer_name,


avg(balance) from depositor, account, customer where depositor.account_number = account.account_number and depositor.customer_name = customer.customer_name and customer_city = Jakarta group by depositor.customer_name having count ( distinct depositor.account_number ) >= 3

Nested Subqueries
Set membership
select distinct customer from borrower where customer_name in (select customer from depositor) select distinct customer_name from borrower where customer_name not in (select customer_name from depositor)

Nested Subqueries ( cont ) select distinct customer_name


from borrower, loan where borrower.loan_number = loan.loan_number and branch_name = STIE and (branch_name, customer_name) in (select branch_name, customer_name from depositor, account where depositor.account_number = account.account_number)

Set Comparison
select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = Bandung select branch_name from branch where assets >some (select assets from branch where branch_city = Bandung)

Set Comparison ( cont ) SQL memperbolehkan <some, <=some,


>=some, =some, dan <>some comparisons. =some identik dengan in, sementara <>some tidak sama seperti not in. Keyword any sinonim dengan some dalam SQL. Sebagaimana untuk some, SQL memperbolehkan <all, <=all, >=all, =all, dan <>all. Perlihatkan bahwa <>all identik dengan not in.

Set Comparison (cont)

select branch_name from branch where assets >all (select assets from branch where branch_name = UGM) select branch_name from account group by branch_name having avg(balance) >=all (select avg(balance) from account group by branch_name)

Test for Empty Relations


select customer_name from borrower where exist (select * from depositor

where depositor.customer_name = borrower.customer_name)

Test for Empty Relations (cont) select distinct S.customer_name


from depositor as S where not exist ((select branch_name from branch where branch_city = Yogyakarta) except (select R.branch_name from depositor as T, account as R where T.account_number = R.account_number and S.customer_name = T.customer_name))

Test For the Absence of The Duplicate Tuple


Konstruk unique mengembalikan nilai true jika argument subquery tidak memuat tupel-tupel duplikat.
select T.customer-name from depositor as T where unique (select R.customer-name from account, depositor as R where T.customer-name = R.customer-name and R.account-number = account.account-number and account.branch-name = UGM)

Test For the Absence of The Duplicate Tuple - Cont


Konstruk not unique digunakan untuk menguji keberadaan tupel-tupel duplikat subquery. Secara formal, uji unique pada relasi didefinisikan gagal (fail) jika hanya jika relasi memuat dua tupel t1 dan t2 sedemikian hingga t1 = t2. Karena uji t1 = t2 gagal jika ada field dari t1 atau t2 null, memungkinkan uji unique true meskipun ada beberapa tupel yang sama.

Derived Relations
(select branch-name, avg(balance) from depositor group by branch-name) as result(branch-name, avg-balance)

select branch-name,avg-balance from (select branch-name, avg(balance) from depositor group by branch-name) as result (branch-name, avg-balance) where avg-balance > 1200

Views
Bentuk perintah membuat view : create view v as <query expression> dengan <query expression> merupakan ekspresi query yang syah. create view branch-total-loan(branch-name, total-loan) as select branch-name, sum(amount) from loan group by branch-name

Modification of The Database


Deletion, diekspresikan dengan delete from r where P
delete from account where branch-name in (select branch-name from branch where branch-city = Jakarta)

You might also like