603 database systems senior lecturer: laurie webster ii, m.s.s.e.,m.s.e.e., m.s.bme, ph.d., p.e....

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

Upload: amanda-elliott

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

SQL *“DROP X name” deletes the created element of kind X with that name Example: CREATE TABLE Sells ( bar CHAR (20), beer VARCHAR (20) price REAL ) ; DROP TABLE Sells;

TRANSCRIPT

Page 1: 603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in 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 18Lecture 18A First Course in Database Systems

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

SQLDefining a Database Schema:

CREATE TABLE name (list of elements).* Principal elements are attributes and their

types, but key declarations and constraints also appear.

* Similar CREATE X commands for other schema elements X: views, indexes, assertions, triggers.

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

SQL* “DROP X name” deletes the created element of

kind X with that name Example:

CREATE TABLE Sells (bar CHAR (20),beer VARCHAR (20)price REAL

) ;

DROP TABLE Sells;

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

SQLSQL includes some features from relational algebra but is based largely on TRC.

Request: Get the first and last names of employees with Salaries greater than $50,000.TRC: {t.FNAME, t.LNAME | EMPLOYEE (t) and t.SALARY > $50,000}

SQL: SELECT T.FNAME, T.LNAMEFROM EMPLOYEE AS TWHERE T.SALARY > 50,000 ;

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

SQL

As a language, SQL has two major components:

1) DDL (Data Definition Language) to define the database structure2) DML (Data Manipulation Language) for retrieving and updating data

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

SQLSQL: SELECT T.FNAME, T.LNAME

FROM EMPLOYEE AS TWHERE T.SALARY > 50,000 ;

SELECT ==> extremely powerful command capable of performing the equivalent of the following three relational algebra statements in a single statement!

1. Selection2. Projection3. Join

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

SQL

SQL Syntax:

CREATE TABLE <tablename> ( <columname> <columntype>[NOT NULL] {,<columname><columntype>[NOT NULL]})

DROP TABLE <tablename>..

DROP VIEW <viewname>

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

SQL

SQL Queries:

* Principal form:SELECT desired attributesFROM tuple variables - range over

relationsWHERE condition about tuple variables

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

SQL

Example Database Schema:Beers (name, manf)Bars (name, addr, license)Drinkers (name, addr, phone)Likes (drinker, beer)Sells (bar, beer, price)Frequents (drinker, bar)

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

SQLQuery: What beers are made by Anheuser-Busch?

Beers ( name, manf)

SELECT nameFROM BeersWHERE manf = ‘Anheuser-Busch’ ;

Bud Bud Lite Michelob

nameResulting Relation

Note single quotes for strings

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

SQLSELECT *FROM BeersWHERE manf = ‘Anheuser-Busch’ ;

name manf

Bud Anheuser-Busch

Bud Lite Anheuser-Busch

Michelob Anheuser-Busch

* yields all of the attributes of Beers

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

The Database Language SQLRenaming Columns:

Beers ( name , manf)

SELECT name AS beerFROM BeersWHERE manf = ‘Anheuser-Busch’ ;

beerBudBud LiteMichelob

Column label ‘name’ changed to ‘beer’

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

SQL LanguageSimple Queries in SQL:

Perhaps the simplest form of query in SQL asks for thosetuples of some one relation that satisfy a condition.

Database Schema below about movies:

Movie(title, year, length, inColor, studioName, producerC#)StarsIn(movieTitle, movieYear, starName)MovieExec(name, address, cert#, netWorth)Studio(name, address, presC#)

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

SQL LanguageQuery (SQL):

Ask about the relation Movie for all movies produced by Disney Studios in 1990.

SELECT *FROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

FROM clause ==> relation or relations to which the query refers. In our example, the query is about the relation Movie.

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

SQL

SELECT *FROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

WHERE clause ==> is a condition, much like the selection-condition in relational algebra.

Tuples must satisfy the condition in order to match the query.

studioName attribute has value ‘Disney’year attribute has value 1990

Both must be TRUETuples with all attributes

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

SQL

SELECT *FROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

The result of the query is the Movie tuples for those movies produced by Disney in 1990, for example, Pretty Woman.title year length inColor studioName producerC#Pretty Women 1990 119 true Disney 999

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

SQLProjection ( ) in SQL:SELECT title, lengthFROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

The results of this query is the table with two columns:title length

Pretty Woman 119 . . . . . .

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

SQLSELECT title, lengthFROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

We can modify this query to produce a relation with attributes name and duration in place of title and length as follows:

SELECT title AS name, length AS durationFROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

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

SQLSELECT title AS name, length AS durationFROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

Results:

name durationPretty Woman 119

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

SQLSELECT title AS name, length*0.016667 AS lengthInHoursFROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

Results:

name lengthInHours Pretty Woman 1

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

SQLSELECT title AS name, length*0.016667 AS lengthInHours

FROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

We allow constants as expressions in SELECT:

SELECT title, length*0.016667 AS length, ‘hrs.’ AS inHoursFROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

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

SQLSELECT title, length*0.016667 AS length, ‘hrs.’ AS inHours

FROM MovieWHERE studioName = ‘Disney’ AND year = 1990 ;

Results:

name length inHours Pretty Woman 1.98334 hrs.

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

SQL

Next Lecture

MORE SQL