SQL - Structured query language introduction

Post on 28-Jul-2015

104 views 4 download

Transcript of SQL - Structured query language introduction

SQL

INTRODUCTIONSQL (Structured Query Language) is a

computer language aimed to store, manipulate, and retrieve data stored in relational databases.

IBM implemented the language, originally called Sequel, as part of the System R project in the early 1970s..

The first commercial relational database was released by Relational Software later becoming oracle.

INTRODUCTIONThe SQL language has several parts:Data-definition language (DDL). provides

commands for defining relation schemas, deleting relations, and modifying relation schemas.

Interactive data-manipulation language (DML). It includes also commands to insert tuples into, delete tuples from, and modify tuples in the database.

View definition. includes commands for defining views.

Transaction control. includes commands for specifying the beginning and ending of transactions.

INTRODUCTIONEmbedded SQL and dynamic SQL. define how

SQL statements can be embedded within general-purpose programming languages, such as C, C++, Java, PL/I, Cobol, Pascal, and Fortran.

Integrity. The SQL DDL includes commands for specifying integrity constraints that the data stored in the database must satisfy. Updates that violate integrity constraints are disallowed.

Authorization (DCL). The SQL DDL includes commands for specifying access rights to relations and views.

Data Definition Language

DDLDDL - Data Definition Language:

Statements used to define the database structure or schema. Some examples:

CREATE - to create objects in the databaseALTER - alters the structure of the

databaseDROP - delete objects from the databaseRENAME - rename an object

Schema Definition in SQLCreate table : Example : create table branch (branch-name char(15),

branch-city char(30),assets integer,primary key (branch-name),

check (assets >= 0))Delete :Delete table : drop table rDelete tuples : Delete from rAlter table : Add Attribute : alter table r add ADDrop attribute : alter table r drop A

Data Manipulation Language

Data Manipulation LanguageDML- Data Manipulation Language:

Statements used for managing data within schema objects.

SELECT - retrieve data from the a database

INSERT - insert data into a tableUPDATE - updates existing data within

a tableDELETE - deletes all records from a

table, the space for the records remain CALL - call a PL/SQL or Java

subprogram

Example Setting Schema & AttributesBranch-schema = (branch-name, branch-city, assets)Customer-schema = (customer-name, customer-

street, customer-city)Loan-schema = (loan-number, branch-name, amount)Borrower-schema = (customer-name, loan-number)Account-schema = (account-number, branch-name,

balance)Depositor-schema = (customer-name, account-

number)

Basic SQL QuerySelect Clause :1. select branch-name from loan Retain Duplicates2. select distinct branch-name from loan Remove

duplicates3. select all branch-name from loan Retain

DuplicatesWhere Clause :1. select loan-number from loan where branch-name

= ’Perryridge’ and amount > 1200From Clause :select customer-name, borrower.loan-number,

amount from borrower, loan where borrower.loan-number = loan.loan-number

Basic SQL QueryRename Operation :select customer-name, borrower.loan-number as

loan-id, amount from borrower, loan where borrower.loan-number = loan.loan-number

Tuple variables :select customer-name, T.loan-number, S.amount

from borrower as T, loan as S where T.loan-number = S.loan-number

Tuple variables are most useful for comparing two tuples in the same relation.

String Operations :select customer-name from customer where

customer-street like ’%Main%’

Basic SQL QueryOrdering the Display of Tuples : select distinct customer-name from borrower,

loan where borrower.loan-number = loan.loan-number and branch-name = ’Perryridge’ order by customer-name

select * from loan order by amount desc, loan-number asc

Set OperationsUnion Operation : find all customers having a

loan, an account, or both(select customer-name from depositor) union (select

customer-name from borrower) Removes Duplicates

Intersect Operation : find all customers who have both a loan and an account

(select distinct customer-name from depositor) intersect (select distinct customer-name from borrower) Removes Duplicates

Except Operation : find all customers who have an account but no loan

(select distinct customer-name from depositor) except (select customer-name from borrower)

Aggregate Functionsavg, min, max, sum, countExample : “Find the average balance for each

customer who lives in Harrison and has at least three accounts.”

select depositor.customer-name, avg (balance)from depositor, account, customerwhere depositor.account-number = account.account-

number and depositor.customer-name = customer.customer-name

andcustomer-city = ’Harrison’group by depositor.customer-namehaving count (distinct depositor.account-number) >=

3

Nested SubqueriesSet Membership : Example : find those

customers who are borrowers from the bank and who appear in the list of account holders

select distinct customer-name from borrower where customer-name in (select customer-name from depositor)

Set Comparison : Example : Find the names of all branches that have assets greater than those of at least one branch located in Brooklyn.

select branch-name from branch where assets > some (select assets from branch where branch-city = ’Brooklyn’)

>some : greater than at least one member

ViewsFind for each branch the sum of the amounts

of all the loans at the branch. create view branch-total-loan(branch-name,

total-loan) asselect branch-name, sum(amount) from loan

groupby branch-name

Complex QueriesDerived Relations : Example : “find the maximum

across all branches of the total balance at each branch.”

select max(tot-balance) from (select branch-name, sum(balance) from account

group by branch-name) as branch-total (branch-name, tot-balance)with Clause : Example : find accounts with the

maximum balance; if there are many accounts with the same maximum balance, all of them are selected.

with max-balance (value) asselect max(balance) from account

select account-number from account, max-balancewhere account.balance = max-balance.value

Modification of the DatabaseDeletion : delete from account where branch-name =

’Perryridge’Insertion : insert into account (account-number, branch-

name, balance) values (’A-9732’, ’Perryridge’, 1200)

Updates : Example : “Pay 5 percent interest on accounts whose balance is greater than average”

update account set balance = balance * 1.05where balance > select avg (balance) from account

Joined Relations

Joined Relations

Joined Relations

Joined Relations