Docsity

SQL: Understanding the Basics of Structured Query Language, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download SQL: Understanding the Basics of Structured Query Language and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

SQL

Background

  • IBM developed the original version of SQL

at San Jose Research Laboratory

  • SQL has clearly established itself as the

standard relational-database language

  • Evolved as The Sequel language, its name has

changed to SQL (Structured Query Language)

A typical SQL query

select A 1, A 2,…,A n from r1,r 2 ,…,rn where P

A represents attribute r represents relation P represents predicate

“Find all loan number for loans made at the San Jose branch with loan amounts greater than $1200.”

select loan-number from loan Where branch-name = ‘San Jose’ and amount >

  • If we want to force the elimination of duplicates, we insert the keyword distinct after select

select distinct branch-name from loan

  • We also can use tge keyword all to specify exlicitly that duplicates are not removed

select all branch-name from loan

  • String Operations

The most commonly used operation strings is

pattern matching using the operator like

Precent (%):The % character

matchs any substing.

Underscore(_): The _character

matchs any character.

  • We consider the following example:

‘San Jose%’ match any string

beginning with “ San Jose”.

‘%idge%’ matches any string

containing “idge”

‘---’ match any string of exactly three

characters.

‘---%’ match any string of at least three

characters.

  • The order by clause cause the tuples in the result of a query to appear in sortf order.

To list in alphabetic order all customers who have a loan at the San Jose branch.

select distinct customer-name from borrow, loan where borrow.loan-number = loan.loan- numebr and branch-name =‘San Jose’ order by customer-name

Set Operations

  • The Union Operation:

To find all customer having a loan, an account, or both at the bank.

( select customer-name from depositor union ( select customer-name from borrower)

  • The Except Operation:

To find all customers who have an account but no loan at the bank.

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

Aggregate Function

  • Aggregate function are functions that take a collection of values as input and return a single value

Five built-in aggregate function:

  • Average: avg
  • Minimum: min
  • Maximum: max
  • Total: sum
  • Count: count

Null Values

  • SQL allows the use of null values to indicate

absence of information about the value of an

attribute.

select loan-number

from loan

where amount is null

Modification of the database

  • Deletion

A delete request is expressed in much the same way as q query. We can delete only whole tuples.

delete from r where P

Delete all account tuples in the San Jose branch

delete from account where branch-name = ‘San Jose’

  • Updates

We may wish to change a value in a tuple without changing all value in the tuple, then we can use update statement.

Suppose that annual interest payment are being made, and all balance are to be increased by 5 percent. update account set balance = balance * 1.