SQL - Structured query language introduction

24
SQL

Transcript of SQL - Structured query language introduction

Page 1: SQL - Structured query language introduction

SQL

Page 2: SQL - Structured query language introduction

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.

Page 3: SQL - Structured query language introduction

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.

Page 4: SQL - Structured query language introduction

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.

Page 5: SQL - Structured query language introduction

Data Definition Language

Page 6: SQL - Structured query language introduction

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

Page 7: SQL - Structured query language introduction

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

Page 8: SQL - Structured query language introduction

Data Manipulation Language

Page 9: SQL - Structured query language introduction

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

Page 10: SQL - Structured query language introduction

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)

Page 11: SQL - Structured query language introduction

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

Page 12: SQL - Structured query language introduction

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%’

Page 13: SQL - Structured query language introduction

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

Page 14: SQL - Structured query language introduction

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)

Page 15: SQL - Structured query language introduction

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

Page 16: SQL - Structured query language introduction

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

Page 17: SQL - Structured query language introduction

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

Page 18: SQL - Structured query language introduction

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

Page 19: SQL - Structured query language introduction

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

Page 20: SQL - Structured query language introduction

Joined Relations

Page 21: SQL - Structured query language introduction

Joined Relations

Page 22: SQL - Structured query language introduction

Joined Relations

Page 23: SQL - Structured query language introduction

Joined Relations

Page 24: SQL - Structured query language introduction