24
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 Lecture 20 A First Course in Database Systems

603 Database Systems

  • Upload
    zulema

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

603 Database Systems. Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 A First Course in Database Systems. SQL. Selection in SQL: The selection operator of relational algebra , and much more, is available through the WHERE clause in SQL. SELECT L - PowerPoint PPT Presentation

Citation preview

Page 1: 603 Database Systems

603 Database Systems

Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E.

Lecture 20Lecture 20

A First Course in Database Systems

Page 2: 603 Database Systems

SQLSelection in SQL:

The selection operator of relational algebra, and much more, is available through the WHERE clause in SQL.

SELECT L

FROM R

WHERE C

L( C (R))

Page 3: 603 Database Systems

SQL SELECT L

FROM R

WHERE C

L( C (R))

We start with the relation in the FROM clause, apply

to each tuple whatever condition is indicated in the WHERE clause, and then project onto the list of attributes and / or expressions in the SELECT clause.

Page 4: 603 Database Systems

SQLThe following query asks for all the movies made after 1970 that are in black-and-white.

SELECT title

FROM movie

WHERE year > 1970 AND NOT inColor ;

In this condition, we have the AND of two booleans.

The first is an ordinary comparison, but the second is the attribute inColor, negated.

Page 5: 603 Database Systems

SQLConsider the Query:

SELECT title

FROM Movie

WHERE (year . 1970 OR length < 90)

AND studioName = ‘MGM’ ;

The parentheses are needed here because the precedence of logical operators in SQL is the same as

in most other languages: AND takes precedence over

OR, and NOT takes precedence over both.

Page 6: 603 Database Systems

SQLRetrieve the name and address of all employees who work in the ‘Research’ department.

SELECT FNAME, LNAME, ADDRESS

FROM EMPLOYEE, DEPARTMENT

WHERE DNAME = ‘Research’ AND

DNUMBER = DNO ;FNAME LNAME ADDRESS

John Smith 731 Fondren, Houston, Tx

Franklin Wong 638 Voss, Houston, Tx

Ramesh Narayan 975 Fire Oak, Humble, Tx

Joyce English 5631 Rice, Houston, Tx

Page 7: 603 Database Systems

SQLFor every project located in ‘Stafford’, list the project number, the controlling department number, and the department managers’ last name , address, and birthdate.

SELECT PNUMBER, DNUM, LNAME, ADDRESS,

BDATE

FROM PROJECT, DEPARTMENT, EMPLOYEE

WHERE DNUM=DNUMBER AND

MGRSSN=SSN AND

PLOCATION=‘Stafford’ ;

Page 8: 603 Database Systems

SQLSELECT PNUMBER, DNUM, LNAME, ADDRESS,

BDATE

FROM PROJECT, DEPARTMENT, EMPLOYEE

WHERE DNUM=DNUMBER AND

MGRSSN=SSN AND

PLOCATION=‘Stafford’ ;

The JOIN condition DNUM=DNUMBER relates a project to its controlling department

Page 9: 603 Database Systems

SQLSELECT PNUMBER, DNUM, LNAME, ADDRESS,

BDATE

FROM PROJECT, DEPARTMENT, EMPLOYEE

WHERE DNUM=DNUMBER AND

MGRSSN=SSN AND

PLOCATION=‘Stafford’ ;

The JOIN condition MGRSSN=SSN relates the controlling department to the employee who manages that department

Page 10: 603 Database Systems

The Database Language SQLDealing With Ambiguous Attribute Names and Aliasing:

In SQL the same name can be used for two (or more)

attributes ==> attributes must be in different relations

Given that this happens and a query refers to two or

more attributes with the same name

==> we must qualify the attribute with the relation

name to prevent ambiguity

Page 11: 603 Database Systems

SQL LanguageDealing With Ambiguous Attribute Names and Aliasing:

To qualify the attribute name with the relation name prefix the relation name to the attribute name and separate the two by a period.

Ex. COMPANY Schema - DNO and LNAME of EMPLOYEE

relation were called DNUMBER and NAME

and

DNAME attribute of DEPARTMENT was called NAME.

Page 12: 603 Database Systems

SQL Language

Ex. COMPANY Schema - DNO and LNAME of EMPLOYEE

relation were called DNUMBER and NAME

and

DNAME attribute of DEPARTMENT was called NAME.

SELECT FNAME, EMPLOYEE.NAME, ADDRESS

FROM EMPLOYEE, DEPARTMENT

WHERE DEPARTMENT.NAME=‘Research’ AND DEPARTMENT.DNUMBER=EMPLOYEE.DNUMBER ;

Page 13: 603 Database Systems

SQLAmbiquity also arises when queries refer to the same relation twice!!

Ex. For each employee, retrieve the employee’s first name and last name and the first and last 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

E & S ==> aliases for EMPLOYEE relation

Page 14: 603 Database Systems

SQL

Unspecified WHERE clauses and use of ‘*’:

A missing WHERE clause indicates no condition on tuple selection, hence, all tuples of the relation specified in the FROM clause qualify to be selected for query result

A missing WHERE clause condition WHERE TRUE

for every row in the tuple

Page 15: 603 Database Systems

SQLQuery: Select all EMPLOYEE SSNs.

SELECT SSN

FROM EMPLOYEE

SSN

123456789

333445555

. for all tuples in EMPLOYEE

.

88866555

Page 16: 603 Database Systems

SQL

Select all combinations of an EMPLOYEE SSN and a Department DNAME.

SELECT SSN, DNAME

FROM EMPLOYEE, DEPARTMENT

result is the Cartesian Crossproduct

Page 17: 603 Database Systems

SQLRetrieve all attribute values of EMPLOYEE tuples who work in DEPARTMENT number 5.

SELECT *

FROM EMPLOYEE

WHERE DNO=5 ;

RESULT => all attributes for employees of DNO=5.

Page 18: 603 Database Systems

SQL

Retrieve all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT he or she works in for every employee of the ‘Research’ department.

SELECT *

FROM EMPLOYEE, DEPARTMENT

WHERE DNAME=‘Research’ AND

DNO=DNUMBER ;

Page 19: 603 Database Systems

SQLSpecify the Cross Product of the EMPLOYEE and DEPARTMENT relations.

SELECT *

FROM EMPLOYEE , DEPARTMENT

Page 20: 603 Database Systems

SQL

Tables as Sets in SQL:

In general SQL does not treat a relation as a set

==> duplicate tuples can appear more than once

or

in the result of a query

Page 21: 603 Database Systems

SQLSQL does not automatically eliminate duplicate

tuples in the results of queries for the following

reasons:

* Duplicate elimination is an expensive

operation. One way to implement it is to

sort the tuples first and then eliminate them.

* The user may want to see duplicate tuples

in the result of a query.

* When an aggregate function is applied to

tuples, in most cases we don’t want to

eliminate duplicates.

Page 22: 603 Database Systems

SQL

If you want to eliminate duplicates of tuples from the

result of a SQL query use the keyword DISTINCT in the SELECT clause

==> distinct tuples should remain in the result

==> a relation that is a set of tuples

Page 23: 603 Database Systems

SQL

If we are interested only in distinct salary values (each value appearing once) use keyword DISTINCT:

SELECT DISTINCT SALARY

FROM EMPLOYEE

SALARY

30000

40000

50000

Page 24: 603 Database Systems

SQL

Next Lecture

MORE SQL