Thursday, June 25, 2009

Structured Query Language

What you will learn hear
- further DML commands
- DDL commands

Schema Used in Examples
- A Part of a Bank Database












Nested Nested Sub queries
- SQL provides a mechanism for the nesting of sub queries.
- A sub query is a select-from-where expression that is nested within another query.
- A common use of sub queries is to perform tests for set membership, set comparisons, and set cardinality.

Example Query


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

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

Set Comparison

- Find all branches that have greater assets than some branch located in kandy.
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = ‘kandy’

- Same query using > some clause
select branch_name
from branch
where assets > some (select assets
from branch
where branch_city = ‘kandy’)

Definition of Some Clause





















Definition of all Clause


Wednesday, June 17, 2009

Example For SQL

Schema Used in Examples
-
A Part of a Bank Database


Projecting Specific Columns From a Table

- Eg: Project all customer names, their streets & cities listed in the customer table
SELECT customer_name, customer_street,customer_city
FROM customer;


List List customer_city in the first column, and , customer_street in the second column in the second column

- We merely change the order of the projected attributes
SELECT customer_city, customer_street
FROM customer;

Notice that we have 2 sets of duplicate rows in the answer to our query. We
can eliminate these duplicate rows.



DISTINCT Keyword in SQL SELECT

- To Eliminate Duplicates in the Answer to our Query, use the DISTINCT Keyword in
SQL SELECT
SELECT distinct customer_city, customer_street
FROM customer;

- We have eliminated the duplicate rows in the answer to our query.


The Asterisk (*) in SELECT

- In Order to Project All Attributes: We Use The Asterisk ( * ) in SELECT
SELECT *
FROM customer;

is equivalent to
SELECT customer_name, customer_street, customer_city
FROM c
ustomer;


Project Specific Rows Which Satisfy a Predicate

SELECT *
FROM branch

WHERE branch_city = ‘kandy';
- In the “WHERE” clause, describe the predicate (i.e., condition which determines which rows are projected)
- E.g. Project all rows from branch which are branch city is ‘kandy’





Tuesday, June 16, 2009

Submitting SQL Queries to a DBMS


Submitting SQL Queries to a DBMS

- Generally, we submit SQL Queries to a DBMS, by illustrating the process for three different DBMS products:
Microsoft Access (we will discuss in this module) Microsoft SQL Server Oracle
- With some minor exceptions, the SQL will be the same for each of the above
Microsoft Access is supportive of SQL, but not as fully supportive as we might wish it to be

A part of a bank database: 6 Tables and Their Attribut
es


The Way of submitting SQL queries to DBMS
Data in the Tables:


step 1
Creating an SQL Query Using MS Access

- Select Queries Object, then “New”


step 2
Choose SQL View



step 3
Enter SQL Statements, then “Run” the Query


step 4
The Dynaset (Answer) to Query


Basic Structure of SQL and Key Clause

Basic Structure
- SQL is based on set and relational operations with certain modifications and enhancements
- A typical SQL query has the form:
select A1, A2, ..., An
from r1, r2, ..., rm
where P
Ais represent attributes
ris represent relations
P is a predicate.
-This query is equivalent to the relational algebra (to be discussed later) expression ∏A1, A2, ..., An(σP (r1 x r2 x ... x rm))
- The result of an SQL query is a relation.

Schema Used in Examples
- A Part of a Bank












The select Clause

- The select clause list the attributes desired in the result of a query (corresponds to the projection operation of the relational algebra)
- E.g. find the names of all branches in the loan relation
select branch_name
from loan
- In the “pure” relational algebra syntax, the query would be: ∏branch-name(loan)
- SQL does not permit the ‘-’ character in names,
Use, e.g., branch_name instead of branch-name in a real implementation
- SQL names are case insensitive, i.e. you can use capital or small letters.
- The general form of the SELECT clause
Select [distinct] attribute (s)
from table (s)
[where condition]
[group by attributes (s)]
[having condition]
[order by attributes (s)]
- SQL allows duplicates in relations as well as in query results
- To force the elimination of duplicates, insert the keyword distinct after select
- e.g Find the names of all branches in the loan relations, and remove duplicates
select distinct branch_name
from loan
- The keyword all specifies that duplicates not be removed.
select all branch_name
from loan
- An asterisk in the select clause denotes “all attributes”
select *
from loan
The select clause can contain arithmetic expression involving the operation +,-,*, and/,and
operating on constants or
attributes of tuples
- The query:
select loan_number,branch-number,amount*100
from loan
would return a relation which is the same as the loan relations,
except that the attribute amount is multiplied by 100

The where Clause
- The where clause specifies conditions that the result must satisfy (corresponds to the selection predicate of the relational algebra)
- e.g Get the customer names for customers in kandy
select customer_name
from customer
where customer_city=‘kandy’
-To find all loan number for loans made at the ‘colombo street-kandy’
with loan amounts greater than Rs.100000
select loan_number
from loan
where branch_name = ‘colombo street-kandy’ and amount >100000
- Comparison results can be combined using the logical connectives and, or, and not.
- Comparisons can be applied to results of arithmetic expressions.

The from Clause

- The from clause lists the relations involved in the query (corresponds to the Cartesian product operation of the relational algebra)
- Find the Cartesian product borrower x loan
select *
from borrower, loan
- Find the name, loan number and loan amount of all customers having a loan at the ‘colombo street-kandy’
select borrower.customer_name, borrower.loan_number, loan.amount
from borrower, loan
where borrower.loan_number = loan.loan_number and
loan.branch_name = ‘colombo street-kandy’

The Rename Operation
- The SQL allows renaming relations and attributes using the as
clause: old_name as new_name
- e.g Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id.
select borrower.customer_name, borrower.loan_number as
loan_id, amount
from borrower, loan
where borrower.loan_number = loan.loan_number

Tuple Variables
- Tuple variables are defined in the from clause via the use of the as clause.
- e.g. Find the customer names and their loan numbers for all customers having a loan at some branch
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = ‘kandy’
- e.g. Find the names of all branches that have greater assets than some branch located in kandy
select customer_name, T.loan_number, S.amount
from borrower as T, loan as S
where T.loan_number = S.loan_number

String Operations
- SQL includes a string-matching operator for comparisons on character strings. Patterns are described using two special characters:
asterisk ( * ). The * character matches any substring.
question ( ? ). The ? character matches any character.
- Find the names of all customers whose street includes the sub string “Main”.
select customer_name
from customer
where customer_street like ‘*Main*’

Ordering the Display of Tuples
- List in alphabetic order the names of all customers having a loan in branch ‘colombo street-kandy’
select distinct customer_name
from borrower, loan
where borrower loan_number = loan.loan-number and
branch_name = ‘colombo street-kandy’
order by customer-name
- We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default.
E.g. order by customer_name desc

DBMS Languages

Data Manipulation Language (DML)
- Used to specify database retrievals and updates
- DML commands (data sublanguage) can be embedded in a general-purpose programming language (C, COBOL etc)
- Alternatively, stand-alone DML commands can be applied directly (structure query language-SQL)
- select, insert, update, delete are the basic operations

Data Definition Language (DDL)
- Used by the DBA and database designers to specify the conceptual schema of a database
- the DDL is also used to define internal and external schemas (views)
- Create table, Alter table, drop table, create view, drop view are the basic operations