SQL: An Example Query: find information for loans made at the Perryridge branch with loan amounts greater than $1200. select loan-number, branch-name, amount from loan where branch-name = Perryridge and amount > 1200
Result Notes SQL – table (result set) where – record TRUE/FALSE
Basic Structure of SQL Expression Revisited Copyright CAU DBLAB select A 1, A 2,..., A n from r 1, r 2,..., r m where P n Semantically, the order of processing clauses is 1. FROM: Let OneTab = r 1 x r 2 x... x r m Then, we have select A 1, A 2,..., A n from OneTab where P 2. WHERE: check each tuple of OneTab one by one if it satisfies P, and choose only those tuples that does. 3. SELECT: retrieve listed attributes (i.e., A 1, A 2,..., A n ) from each of the chosen tuples. n Notes: l DBMS SQL processor does not necessarily process the SQL expression in such a way. l Usually, it retrieves query answers in a much more efficient way.
Commercial DBMS & ISQL DBMS products: Oracle, SQL Server, DB2, MySQL, … ISQL Interactive SQL l ISQL tools of representative commercial DBMSs Oracle: SQL*PLUS, SQL Server: Query Analyzer DB2: command editor MySQL: mysql client n For further details of ISQL and commercial DBMS products, refer to the documents in the course home page DBMS DBMS ISQL Oracle ISQL SQL Server ISQL Copyright CAU DBLAB
SQL Exercises with Commercial DBMS & ISQL n Exercises Table Creation Tuple Insertion (Deletion, Update) Data Retrieval & Update Copyright CAU DBLAB
Union Compatibility Given SQL expressions r and s, for r union s, r intersect s, and r except s, to be valid, the following two conditions should be satisfied: 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (e.g., 2nd column of r deals with the same type of values as does the 2nd column of s)
Semantic of SQL Expression Copyright CAU DBLAB Example query: select branch-name, avg (balance) from account where branch-name like R% group by branch-name having avg (balance) > 1200 order by branch-name (semantic) order of evaluation: from where group by having select order by Note once again that DBMS SQL processor does not necessarily process the SQL expression in such a way. Usually, it retrieves query answers in a much more efficient way.
SQL SQL SQL Part I §3.1 - §3.7 §3.10 Modification of the Database SQL Part II §3.8 Complex Queries §3.9 Views §3.11 Joined Relations
Classification of SQL Commands DML(Data Manipulation Language) Query: Select-From-Where Update: Insert, Delete, Update n DDL(Data Definition Language): l create table, alter table, drop table l create index drop index n DCL(Data Control Language): l commit work, rollback work l grant, revoke Copyright CAU DBLAB