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

Preview:

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;

Citation preview

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

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.

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;

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 ;

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

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

SQL

SQL Syntax:

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

DROP TABLE <tablename>..

DROP VIEW <viewname>

SQL

SQL Queries:

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

relationsWHERE condition about tuple variables

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)

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

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

The Database Language SQLRenaming Columns:

Beers ( name , manf)

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

beerBudBud LiteMichelob

Column label ‘name’ changed to ‘beer’

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#)

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.

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

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

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 . . . . . .

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 ;

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

Results:

name durationPretty Woman 119

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

Results:

name lengthInHours Pretty Woman 1

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 ;

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.

SQL

Next Lecture

MORE SQL

Recommended