Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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
Defining 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;
SQL
SQL 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
FROM
WHERE
T.FNAME, T.LNAME
EMPLOYEE AS T
T.SALARY > 50,000 ;
SQL
As a language, SQL has two major components:
1) DDL (Data Definition Language) to define the
database structure
2) DML (Data Manipulation Language) for
retrieving and updating data
SQL
SQL: SELECT
FROM
WHERE
T.FNAME, T.LNAME
EMPLOYEE AS T
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. Selection
2. Projection
3. 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 attributes
FROM
tuple variables - range over
relations
WHERE 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)
SQL
Query: What beers are made by Anheuser-Busch?
Beers ( name, manf)
SELECT
FROM
WHERE
Note single quotes for
strings
name
Beers
manf = ‘Anheuser-Busch’ ;
name
Bud
Bud Lite
Michelob
Resulting Relation
SQL
SELECT
FROM
WHERE
*
Beers
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 SQL
Renaming Columns:
Beers ( name , manf)
SELECT
FROM
WHERE
Column label
‘name’ changed
to ‘beer’
name AS beer
Beers
manf = ‘Anheuser-Busch’ ;
beer
Bud
Bud Lite
Michelob
SQL Language
Simple Queries in SQL:
Perhaps the simplest form of query in SQL asks for those
tuples 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 Language
Query (SQL):
Ask about the relation Movie for all movies produced by
Disney Studios in 1990.
SELECT
FROM
WHERE
*
Movie
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
WHERE
Tuples with
all attributes
*
Both must be TRUE
Movie
studioName = ‘Disney’ AND year = 1990 ;
WHERE clause ==> is a condition, much like the selectioncondition in relational algebra.
Tuples must satisfy the condition in order to match the query.
studioName attribute has value ‘Disney’
year attribute has value 1990
SQL
SELECT
FROM
WHERE
*
Movie
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
Pretty Women
year
1990
length inColor studioName
119
true
Disney
producerC#
999
SQL
Projection ( ) in SQL:
SELECT
FROM
WHERE
title, length
Movie
studioName = ‘Disney’ AND year = 1990 ;
The results of this query is the table with two columns:
title
length
Pretty Woman
...
119
...
SQL
SELECT
FROM
WHERE
title, length
Movie
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
FROM
WHERE
title AS name, length AS duration
Movie
studioName = ‘Disney’ AND year = 1990 ;
SQL
SELECT
FROM
WHERE
title AS name, length AS duration
Movie
studioName = ‘Disney’ AND year = 1990 ;
Results:
name
Pretty Woman
duration
119
SQL
SELECT title AS name, length*0.016667 AS lengthInHours
FROM
Movie
WHERE studioName = ‘Disney’ AND year = 1990 ;
Results:
name
Pretty Woman
lengthInHours
1
SQL
SELECT title AS name, length*0.016667 AS lengthInHours
FROM
Movie
WHERE studioName = ‘Disney’ AND year = 1990 ;
We allow constants as expressions in SELECT:
SELECT title, length*0.016667 AS length, ‘hrs.’ AS inHours
FROM
Movie
WHERE studioName = ‘Disney’ AND year = 1990 ;
SQL
SELECT title, length*0.016667 AS length, ‘hrs.’ AS inHours
FROM
Movie
WHERE studioName = ‘Disney’ AND year = 1990 ;
Results:
name
Pretty Woman
length
1.98334
inHours
hrs.
SQL
Next Lecture
MORE SQL