37
COMP 5138 COMP 5138 Relational Database Relational Database Management Systems Management Systems Semester 2, 2007 Semester 2, 2007 Lecture 5B Lecture 5B Introduction to SQL Introduction to SQL

COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5B Introduction to SQL

Embed Size (px)

Citation preview

COMP 5138COMP 5138

Relational Database Relational Database

Management Systems Management Systems

Semester 2, 2007Semester 2, 2007

Lecture 5BLecture 5B

Introduction to SQLIntroduction to SQL

2222

L5L5 SQL Part ISQL Part I

Conceptual Data ModelingEntity Relationship ModelEnhanced E-R Model(UML)

Relational Data ModelBasic ConceptsTransformation Rules E-R Model to RMRelational Algebra

Data NormalizationFunctional DependenciesNormal FormsNormalization

Review of The First Weeks

3333

L5L5 SQL Part ISQL Part I

Overview of SQL

Basic SQL Query

Aggregate Functions

Set Operations

Today’s Agenda

4444

L5L5 SQL Part ISQL Part ISQL - The Structured Query

Language

SQL is the standard declarative query language for RDBMSDescribing what data we are interested in, but not how to retrieve it.

Based on SEQUELIntroduced in the mid-1970’s as the query language for IBM’s System (Structured English Query Language)

ANSI standard since 1986, ISO-standard since 19871989: revised to SQL-891992: more features added – SQL-921999: major rework – SQL:1999 (SQL 3)SQL:2003 – ‘bugfix release’ of SQL:1999 plus SQL/XML

5555

L5L5 SQL Part ISQL Part IBenefits of a Standardized DB

Language

Reduced training costs

Productivity

Application portabilityLimited by vendor-specific variations!

Application longevity

Reduced dependency on a single vendor

Cross-system communication

6666

L5L5 SQL Part ISQL Part I SQL Overview

DDL (Data Definition Language)Create, drop, or alter the relation schema

DML (Data Manipulation Language)The retrieval of information stored in the database

A Query is a statement requesting the retrieval of informationThe portion of a DML that involves information retrieval is called a query language

The insertion of new information into the databaseThe deletion of information from the databaseThe modification of information stored in the database

DCL (Data Control Language)Commands that control a database, including administering privileges and users

7777

L5L5 SQL Part ISQL Part I

Overview of SQL

Basic SQL Query

Aggregate Functions

Set Operations

Today’s Agenda

8888

L5L5 SQL Part ISQL Part I SELECT Statement

Used for queries on single or multiple tables

Clauses of the SELECT statement:SELECT Lists the columns (and expressions) that should be

returned from the queryFROM Indicate the table(s) from which data will be obtainedWHERE Indicate the conditions to include a tuple in the resultGROUP BY Indicate the categorization of tuplesHAVING Indicate the conditions to include a category ORDER BY Sorts the result according to specified criteria

The result of an SQL query is a relation

A SFW-query is equivalent to the relational algebra expression A1, A2, ..., An ( condition (R1 x R2 x ... x Rm) )

9999

L5L5 SQL Part ISQL Part I Running Example

10101010

L5L5 SQL Part ISQL Part IExample: Basic One-table

SQL Query

List the names of all customers from Sydney.

select customer_name from customer where customer_city=‘Sydney’

Corresponding relational algebra expression

customer_name ( customer_city=‘Sydney’ (customer) )

Note: SQL does not permit the ‘-’ character in names, Use, e.g., branch_name instead of branch-name in a real implementation.

Note: SQL names are case insensitive, i.e. you can use capital or small letters.

You may wish to use upper case where-ever we use bold font.

π

11111111

L5L5 SQL Part ISQL Part I The WHERE Clause

The where clause specifies conditions that database tuples must satisfy, in order to contribute to the result

The where clause is a boolean that gets evaluated on each tuple corresponds to the selection predicate of the relational algebra.

Comparison operators in SQL: = , > , >= , < , <= , != , <>

Comparison results can be combined using the logical connectives and, or, and not.

To find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. SELECT loan_number

FROM loanWHERE branch_name = ‘Perryridge’ AND amount > 1200

12121212

L5L5 SQL Part ISQL Part I The WHERE Clause (ctd)

Comparisons etc can be applied to columns from the database relation(s) named in the FROM clause

Each column can be named simply (eg amount) or qualified (ie as tablename.columnname)

Comparisons can be applied to results of arithmetic expr.

To find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. SELECT loan.loan_number

FROM loanWHERE loan.branch_name = ‘Perryridge’ AND loan.amount > 1200

SELECT loan_number FROM loanWHERE branch_name = ‘Perryridge’ AND (amount - 1200) > 0

13131313

L5L5 SQL Part ISQL Part I The WHERE Clause (cont’d)

SQL includes a between comparison operator (called “range queries”)

e.g. Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is, >= $90,000 and <=$100,000)

SELECT loan_number FROM loan WHERE amount BETWEEN 90000 AND 100000

14141414

L5L5 SQL Part ISQL Part I String Operations

SQL includes a string-matching operator for comparisons on character strings.

LIKE is used for string matching

Patterns are described using two special characters (“wildcards”):percent (%). The % character matches any substring.underscore (_). The _ character matches any character.

Find the names of all customers whose street includes the substring “Main”. select customer_name

from customerwhere customer_street like ‘%Main%’

SQL supports a variety of string operations such asconcatenation (using “||”)converting from upper to lower case (and vice versa)finding string length, extracting substrings, etc.

15151515

L5L5 SQL Part ISQL Part I The SELECT clause

The SELECT clause indicates what appears in the output relation

A common possibility is to name columns from the database relation

Simply (columnname) or qualified (tablename.columnname)

These columns (from those rows that satisfy the WHERE clause) are placed into the output relationCorresponds to projection operator in relational algebra

An asterisk in the select clause denotes all attributesSELECT * FROM loanThis is equivalent to

SELECT loan_number, branch_name, amount FROM loanThe benefit of using * is that query still works if schema changes

Eg extra columns are added

16161616

L5L5 SQL Part ISQL Part I The SELECT Clause (ctd)

The select clause can contain arithmetic expressions involving the operations +, -, * and /, and operating on constants or attributs of tuples.The query:

SELECT loan_number, branch_number, amount*100 FROM loanwould return a relation which is the same as the loan relation except that the amount-values are multiplied by 100.

Naming expressions: we can give a name to attribute in the output relation by as new_name

SELECT loan_number, branch_number, amount*100 AS percent_amount FROM loan

17171717

L5L5 SQL Part ISQL Part I Order By Clause

List all customers (name) from Sydney in alphabetical order.

select customer_name from customer where customer_city=‘Sydney’order by customer_name

Ordering: Two optionsASC ascending oder (default)DESC descending order

You can order by more than one attributee.g., order by country desc, name asc

18181818

L5L5 SQL Part ISQL Part I Duplicates

In contrast to the relational algebra, SQL allows duplicates in relations as well as in query results.

To force the elimination of duplicates, insert the keyword distinct after select.

Find the names of all branches in the loan relations, and remove duplicates

select distinct branch_namefrom loan

The keyword all specifies that duplicates not be removed.select all branch_namefrom loan

19191919

L5L5 SQL Part ISQL Part I Multi-table queries

The from clause lists the relations involved in the querycorresponds to the Cartesian product operation of the relational algebra.join-predicates must be explicitly stated in the where clauseRemember to qualify any attribute name that occurs in several tables

Examples: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 Perryridge branch.

SELECT customer_name,borrower,loan_number,amount FROM borrower, loan WHERE borrower.loan-number = loan.loan_number

AND branch-name = ‘Perryridge’

20202020

L5L5 SQL Part ISQL Part I Join Example

What grades where achieved by the students?

SELECT name, number, grade FROM Students, Enrolled WHERE Students.sid = Enrolled.sid;

Join involves multiple tables in FROM clause

WHERE clause performs the equality check for common columns of the two tables

21212121

L5L5 SQL Part ISQL Part I Aliases

Some queries need to refer to the same relation twiceIn this case, aliases are given to the relation nameExample: For each employee, retrieve the employee's name, and the name of his or her immediate supervisor.

SELECT E.FNAME, E.LNAME, S.FNAME,S.LNAME FROM EMPLOYEE E, EMPLOYEE S WHERE E.SUPERSSN = S.SSN

We can think of E and S as two different copies of EMPLOYEE;E represents employees in role of supervisees and S represents employees in role of supervisors

Alias can also be used just to shorten the query text

SELECT name, number, grade FROM Students S, Enrolled E WHERE S.sid = E.sid;

22222222

L5L5 SQL Part ISQL Part I Exercise Example 1

varchar varchar int int

empid name room

lecturerssid number empid mark

assessment

sid number

enrolled

grade

number titlecredit_points

courses

lecturer

int varchar int int int varchar varchar

sid name birthdate

students

country

int varchar date varchar

int int char

23232323

L5L5 SQL Part ISQL Part I Example Queries

List all courses by title

select title from courses

List all courses.

select * from courses

Who is teaching “COMP5138”?select name from courses, lecturerswhere number=‘COMP5138’ and lecturer=empid

24242424

L5L5 SQL Part ISQL Part I

Find the students with marks between 0 and 10.

select sid from assessment

where mark between 0 and 10

List students who got a distinction or better in COMP5138.Tip: use in (not between) comparison operator for sets

select sid from enrolledwhere number =‘COMP5138’ and grade in (‘D’, ‘H’)

List in alphabetic order the names of all students

select * from students order by name

Example Queries

25252525

L5L5 SQL Part ISQL Part I

Overview of SQL

Basic SQL Query

Aggregate Functions

Set Operations

Today’s Agenda

26262626

L5L5 SQL Part ISQL Part I Aggregate Functions

These functions operate on the multiset of values of a column of a relation, and return a value

avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values

Note: with aggregate functions there is a single row in the result relation

so you can’t also have single-valued columns in the select clause (except with grouping, see next lecture)

27272727

L5L5 SQL Part ISQL Part I Aggregate Functions

Step A: Use FROM clause and WHERE clause to determine a set of rows (possibly from a cartesian product) which meet the where condition

Call the relation Q

Step B: The output is a single row, whose entries are each obtained by applying some aggregate function to one or more columns of the relation Q

28282828

L5L5 SQL Part ISQL Part IExamples for Aggregate

Functions

How many students are enrolled? Two possible interpretations (give different answers!)

select count(*) from Enrolledselect count(distinct sid) from Enrolled

What was the average mark for COMP5138? select avg(mark) AS avg_mark

from assessment where number=‘COMP5138’Which was the best and worst mark for COMP5138?select max(mark) AS max_mark, min(mark) AS min_mark

from assessment where number = ‘COMP5138’

29292929

L5L5 SQL Part ISQL Part I

Overview

DDL Statements

Basic SQL Query

Aggregate Functions

Set Operations

Today’s Agenda

30303030

L5L5 SQL Part ISQL Part I Set Operations

The set operations union, intersect, and except (Oracle: minus) operate on relations and correspond to the relational algebra operations Each of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions union all, intersect all and except all.

Suppose a tuple occurs m times in r and n times in s, then, it occurs:

m + n times in r union all s

min(m,n) times in r intersect all smax(0, m – n) times in r except all s

(not supported by Oracle)(not supported by Oracle)

31313131

L5L5 SQL Part ISQL Part I Set Operations

Find all customers who have a loan, an account, or both:

(select customer_name from depositor)union(select customer_name from borrower)

Find all customers who have both a loan and an account

(select customer_name from depositor)intersect(select customer_name from borrower)

Find all customers who have an account but no loan(select customer_name from depositor)

except(select customer_name from borrower)

32323232

L5L5 SQL Part ISQL Part IExamples for Set Operations

List all names in the database.

select name from students unionselect name from lecturers

Which students did not enroll in any course?

select sid from students minusselect sid from enrolled

Find students who enrolled both for ‘COMP5138’ and ‘COMP5318’.

select sid from enrolled where number=‘COMP5138’ intersectselect sid from enrolled where number=‘COMP5318’

33333333

L5L5 SQL Part ISQL Part I

R1

S1

B1

Exercise Example 2

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.5

58 rusty 10 35.0

bid bname color

101 Interlake blue

102 Interlake red

103 Clipper green

104 Marine red

sid bid day

22 101 10/10/96

58 103 11/12/96

34343434

L5L5 SQL Part ISQL Part I

Find sid’s of sailors who’ve reserved a red or a green boat

SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’)

SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’UNIONSELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

Example Queries

35353535

L5L5 SQL Part ISQL Part I

Find sid’s of sailors who’ve reserved a red and a green boat

SELECT S.sidFROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color=‘red’ AND B2.color=‘green’)

SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’INTERSECTSELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

Example Queries

36363636

L5L5 SQL Part ISQL Part I Wrap-Up

SQL Basic QuerySELECT … FROM … WHERE … ORDER BY …

Aggregate Functions

Set OperationsUnion, Intersect, and Except

37373737

L5L5 SQL Part ISQL Part I Remember: SELECT Statement

Clauses of the SELECT statement:SELECT Lists the columns (and expressions) that should be

returned from the queryFROM Indicate the table(s) from which data will be obtainedWHERE Indicate the conditions to include a tuple in the resultGROUP BY Indicate the categorization of tuplesHAVING Indicate the conditions to include a category ORDER BY Sorts the result according to specified criteria

The result of an SQL query is a relation

A SFW-query is equivalent to the relational algebra expression A1, A2, ..., An ( condition (R1 x R2 x ... x Rm) )