SlideShare a Scribd company logo
SQL: The Query
Language
Part 3

CS186, Fall 2005
R &G - Chapters 5-6

It is not every question
that deserves an answer.
Publius Syrus. 42 B. C.
Null Values
• Field values in a tuple are sometimes unknown (e.g.,
a rating has not been assigned) or inapplicable (e.g.,
no spouse’s name).
– SQL provides a special value null for such situations.
• The presence of null complicates many issues. E.g.:
– Special operators needed to check if value is/is not null.
– Is rating>8 true or false when rating is equal to null? What
about AND, OR and NOT connectives?
– We need a 3-valued logic (true, false and unknown).
– Meaning of constructs must be defined carefully. (e.g.,
WHERE clause eliminates rows that don’t evaluate to true.)
– New operators (in particular, outer joins) possible/needed.
Joins
SELECT (column_list)
FROM table_name
[INNER | {LEFT |RIGHT | FULL } OUTER] JOIN table_name
ON qualification_list
WHERE …

Explicit join semantics needed unless it is an INNER
join
(INNER is default)
Inner Join
Only the rows that match the search conditions are
returned.
SELECT s.sid, s.name, r.bid
FROM Sailors s INNER JOIN Reserves r
ON s.sid = r.sid
Returns only those sailors who have reserved boats
SQL-92 also allows:
SELECT s.sid, s.name, r.bid
FROM Sailors s NATURAL JOIN Reserves r
“NATURAL” means equi-join for each pair of
attributes with the same name
SELECT s.sid, s.name, r.bid
FROM Sailors s INNER JOIN Reserves r
ON s.sid = r.sid

sid
22
31
95

sname rating age
Dustin
7
45.0
Lubber 8
55.5
Bob
3
63.5

sid bid
day
22 101 10/10/96
95 103 11/12/96

s.sid s.name r.bid
22 Dustin
101
95 Bob
103
Left Outer Join
Left Outer Join returns all matched rows, plus
all unmatched rows from the table on the left
of the join clause
(use nulls in fields of non-matching tuples)
SELECT s.sid, s.name, r.bid
FROM Sailors s LEFT OUTER JOIN Reserves r
ON s.sid = r.sid
Returns all sailors & information on whether
they have reserved boats
SELECT s.sid, s.name, r.bid
FROM Sailors s LEFT OUTER JOIN Reserves r
ON s.sid = r.sid

sid
22
31
95

sname rating age
Dustin
7
45.0
Lubber 8
55.5
Bob
3
63.5

s.sid
22
95
31

sid bid
day
22 101 10/10/96
95 103 11/12/96

s.name r.bid
Dustin
101
Bob
103
Lubber
Right Outer Join
Right Outer Join returns all matched rows, plus
all unmatched rows from the table on the
right of the join clause
SELECT r.sid, b.bid, b.name
FROM Reserves r RIGHT OUTER JOIN Boats b
ON r.bid = b.bid
Returns all boats & information on which ones
are reserved.
SELECT r.sid, b.bid, b.name
FROM Reserves r RIGHT OUTER JOIN Boats b
ON r.bid = b.bid

sid bid
day
22 101 10/10/96
95 103 11/12/96
r.sid

bid
101
102
103
104

b.bid
22
95

101
102
103
104

bname
Interlake
Interlake
Clipper
Marine

b.name
Interlake
Interlake
Clipper
Marine

color
blue
red
green
red
Full Outer Join
Full Outer Join returns all (matched or
unmatched) rows from the tables on both
sides of the join clause
SELECT r.sid, b.bid, b.name
FROM Reserves r FULL OUTER JOIN Boats b
ON r.bid = b.bid
Returns all boats & all information on
reservations
SELECT r.sid, b.bid, b.name
FROM Reserves r FULL OUTER JOIN Boats b
ON r.bid = b.bid

sid bid
22
95

day

101 10/10/96
103 11/12/96
r.sid

b.bid
22
95

bid
101
102
103
104

101
102
103
104

bname
Interlake
Interlake
Clipper
Marine

b.name
Interlake
Interlake
Clipper
Marine

color
blue
red
green
red

Note: in this case it is the same as the ROJ because
bid is a foreign key in reserves, so all reservations must
have a corresponding tuple in boats.
Conceptual SQL Evaluation

SELECT
[DISTINCT] target-list
FROM
relation-list
WHERE
qualification
GROUP BY grouping-list
HAVING
group-qualification

Project away columns
(just keep those used in
SELECT, GBY, HAVING)

SELECT

[DISTINCT]

Eliminate
duplicates

Apply selections
(eliminate rows)

WHERE

HAVING

Eliminate
groups

Relation
cross-product

FROM

GROUP BY

Form groups
& aggregate
Sorting the Results of a Query
• ORDER BY column [ ASC | DESC] [, ...]
SELECT S.rating, S.sname, S.age
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid
AND R.bid=B.bid AND B.color=‘red’
ORDER BY S.rating, S.sname;

• Can order by any column in SELECT list,
including expressions or aggs:
SELECT S.sid, COUNT (*) AS redrescnt
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid
AND R.bid=B.bid AND B.color=‘red’
GROUP BY S.sid
ORDER BY redrescnt DESC;
Views: Defining External DB
Schemas
CREATE VIEW view_name
AS select_statement
Makes development simpler
Often used for security
Not instantiated - makes updates tricky
CREATE VIEW Reds
AS SELECT B.bid, COUNT (*) AS scount
FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color=‘red’
GROUP BY B.bid
Views Instead of Relations in
Queries
CREATE VIEW Reds
AS SELECT B.bid, COUNT (*) AS scount
FROM Boats B, Reserves R
WHERE R.bid=B.bid AND B.color=‘red’
GROUP BY B.bid
b.bid

scount
102

1

Reds

SELECT bname, scount
FROM Reds R, Boats B
WHERE R.bid=B.bid
AND scount < 10
Discretionary Access Control
GRANT privileges ON object TO users
[WITH GRANT OPTION]
• Object can be a Table or a View
• Privileges can be:
• Select
• Insert
• Delete
• References (cols) – allow to create a
foreign key that references the specified
column(s)
• All
• Can later be REVOKEd
• Users can be single users or groups
Two more important topics
• Constraints
• SQL embedded in other languages
Integrity Constraints (Review)
• An IC describes conditions that every legal
instance of a relation must satisfy.
– Inserts/deletes/updates that violate IC’s are disallowed.
– Can be used to ensure application semantics (e.g., sid is
a key), or prevent inconsistencies (e.g., sname has to be
a string, age must be < 200)
• Types of IC’s : Domain constraints, primary key
constraints, foreign key constraints, general
constraints.
– Domain constraints: Field values must be of right type.
Always enforced.
– Primary key and foreign key constraints: you know them.
General
Constraints

•

•

•
•

CREATE TABLE Sailors
( sid INTEGER,
sname CHAR(10),
rating INTEGER,
age REAL,
Useful when
PRIMARY KEY (sid),
more general ICs
CHECK ( rating >= 1
than keys are
AND rating <=
involved.
CREATE TABLE Reserves
Can use queries
( sname CHAR(10),
to express
bid INTEGER,
constraint.
day DATE,
Checked on
PRIMARY KEY (bid,day),
insert or update.
CONSTRAINT noInterlakeRes
Constraints can
CHECK (`Interlake’ <>
be named.
( SELECT B.bname
FROM Boats B
WHERE B.bid=bid)))

10 ))
Constraints Over Multiple Relations

•
•
•

•

•
•

CREATE TABLE Sailors
( sid INTEGER,
Number of boats
sname CHAR(10),
plus number of
rating INTEGER,
Awkward and
sailors is < 100
wrong!
age REAL,
Only checks
PRIMARY KEY (sid),
sailors!
Only required to CHECK
hold if the
( (SELECT COUNT (S.sid) FROM Sailors S)
associated table is
+ (SELECT COUNT (B.bid) FROM
non-empty.
ASSERTION is the

right solution; not
associated with
either table.
Unfortunately, not
supported in many
DBMS.
Triggers are

Boats B) < 100 )

CREATE ASSERTION smallClub
CHECK
( (SELECT COUNT (S.sid) FROM Sailors
+ (SELECT COUNT (B.bid)
FROM Boats B) < 100 )

S)
Writing Applications with SQL
• SQL is not a general purpose programming
language.
+ Tailored for data retrieval and manipulation
+ Relatively easy to optimize and parallelize
- Can’t write entire apps in SQL alone
Options:
Make the query language “Turing complete”
Avoids the “impedance mismatch”
but, loses advantages of relational language simplicity

Allow SQL to be embedded in regular programming
languages.
Q: What needs to be solved to make the latter
approach work?
Embedded SQL
•

DBMS vendors usually provide “host language bindings”

•

General pattern

•
•

–
–
–
–

E.g. for C or COBOL
Allow SQL statements to be called from within a program
Typically you preprocess your programs
Preprocessor generates calls to a proprietary DB connectivity
library

– One call to connect to the right database (login, etc.)
– SQL statements can refer to host variables from the
language
Typically vendor-specific
– We won’t look at any in detail, we’ll look at standard stuff
Problem
– SQL relations are (multi-)sets, no a priori bound on the
number of records. No such data structure in C.
– SQL supports a mechanism called a cursor to handle this.
Just to give you a flavor
EXEC SQL SELECT S.sname, S.age
INTO :c_sname,:c_age
FROM Sailors S
WHERE S.sid = :c_sid
Cursors
•
•
•
•
•

Can declare a cursor on a relation or query
Can open a cursor
Can repeatedly fetch a tuple (moving the cursor)
Special return value when all tuples have been retrieved.
ORDER BY allows control over the order in which tuples are
returned.
• Fields in ORDER BY clause must also appear in SELECT clause.

•
•

Can also modify/delete tuple pointed to by a cursor
– A “non-relational” way to get a handle to a particular tuple
There’s an Embedded SQL syntax for cursors
– DECLARE <cursorname> CURSOR FOR <select stmt>
– FETCH FROM <cursorname> INTO <variable names>
– But we’ll use JDBC instead
Database APIs: Alternative to
embedding
• Rather than modify compiler, add a library
with database calls (API)
– special procedures/objects
– passes SQL strings from language, presents
result sets in a language-friendly way
– ODBC a C/C++ standard started on Windows
– JDBC a Java equivalent
– Most scripting languages have similar things
• E.g. For Perl there is DBI, “oraPerl”, other packages

• Mostly DBMS-neutral
– at least try to hide distinctions across different
DBMSs
Architecture

Application
ODBC driver

Data Source
• A lookup service maps “data source names” (“DSNs”) to drivers
– Typically handled by OS

•
•
•
•

Based on the DSN used, a “driver” is linked into the app at runtime
The driver traps calls, translates them into DBMS-specific code
Database can be across a network
ODBC is standard, so the same program can be used (in principle) to
access multiple database systems
• Data source may not even be an SQL database!
ODBC/JDBC
• Various vendors provide drivers
– MS bundles a bunch into Windows
– Vendors like DataDirect and OpenLink sell drivers for
multiple OSes
• Drivers for various data sources
– Relational DBMSs (Oracle, DB2, SQL Server, Informix, etc.)
– “Desktop” DBMSs (Access, Dbase, Paradox, FoxPro, etc.)
– Spreadsheets (MS Excel, Lotus 1-2-3, etc.)
– Delimited text files (.CSV, .TXT, etc.)
• You can use JDBC/ODBC client s over many data
sources
– E.g. MS Query comes with many versions of MS Office
(msqry32.exe)
• Can write your own Java or C++ programs against
xDBC
JDBC
• Part of Java, very easy to use
• Java comes with a JDBC-to-ODBC bridge
– So JDBC code can talk to any ODBC data source
– E.g. look in your Windows Control Panel for
JDBC/ODBC drivers!
• JDBC tutorial online
– http://developer.java.sun.com/developer/Books/JDBC
Tutorial/
JDBC Basics: Connections
• A Connection is an object representing a login to a
database
// GET CONNECTION
Connection con;
try {
con = DriverManager.getConnection(
"jdbc:odbc:sailorsDB",
userName,password);
} catch(Exception e){ System.out.println(e);

• Eventually you close the connection

// CLOSE CONNECTION
try { con.close(); }
catch (Exception e) { System.out.println(e); }

}
JDBC Basics: Statements
• You need a Statement object for each SQL
statement
// CREATE STATEMENT
Statement stmt;
try {
stmt = con.createStatement();
} catch (Exception e){
System.out.println(e);
}

Soon we’ll say stmt.executeQuery(“select …”);
CreateStatement cursor behavior
•

•

•

•

Two optional args to createStatement:
– createStatement(ResultSet.<TYPE>,
ResultSet.<CONCUR>)
– Corresponds to SQL cursor features
<TYPE> is one of
– TYPE_FORWARD_ONLY: can’t move cursor backward
– TYPE_SCROLL_INSENSITIVE: can move backward, but doesn’t
show results of any updates
– TYPE_SCROLL_SENSITIVE: can move backward, will show
updates made while result set is open
<CONCUR> is one of
– CONCUR_READ_ONLY: this statement doesn’t allow updates
– CONCUR_UPDATABLE: this statement allows updates
Defaults:
– TYPE_FORWARD_ONLY and CONCUR_READ_ONLY
JDBC Basics: ResultSet
•

•

A ResultSet object serves as a cursor for the statement’s
results (stmt.executeQuery() )
// EXECUTE QUERY
ResultSet results;
try {
results = stmt.executeQuery(
"select * from Sailors")
} catch (Exception e){
System.out.println(e); }

Obvious handy methods:
– results.next() advances cursor to next tuple

• Returns “false” when the cursor slides off the table (beginning or end)

– “scrollable” cursors:

• results.previous(), results.relative(int), results.absolute(int),
results.first(), results.last(), results.beforeFirst(), results.afterLast()
ResultSet Metadata
•

Can find out stuff about the ResultSet schema via
ResultSetMetaData
ResultSetMetaData rsmd = results.getMetaData();
int numCols = rsmd.getColumnCount();
int i, rowcount = 0;

•

// get column header info
for (i=1; i <= numCols; i++){
if (i > 1) buf.append(",");
buf.append(rsmd.getColumnLabel(i));
}
buf.append("n");

Other ResultSetMetaData methods:
– getColumnType(i), isNullable(i), etc.
Getting Values in Current of Cursor
• getString
// break it off at 100 rows max
while (results.next() && rowcount < 100){
// Loop through each column, getting the
// column data and displaying
for (i=1; i <= numCols; i++) {
if (i > 1) buf.append(",");
buf.append(results.getString(i));
}
buf.append("n");
rowcount++;

•

}

Similarly, getFloat, getInt, etc.
Updating Current of Cursor
• Update fields in current of cursor:
result.next();
result.updateInt("Rating", 10);

• Also updateString, updateFloat, etc.
• Or can always submit a full SQL UPDATE
statement
– Via executeQuery()
• The original statement must have been
CONCUR_UPDATABLE in either case!
Cleaning up Neatly
try {
// CLOSE RESULT SET
results.close();
// CLOSE STATEMENT
stmt.close();
// CLOSE CONNECTION
con.close();
} catch (Exception e) {
System.out.println(e);
}
Putting it Together (w/o try/catch)
Connection con =
DriverManager.getConnection("jdbc:odbc:weblog",userN
ame,password);
Statement stmt = con.createStatement();
ResultSet results =
stmt.executeQuery("select * from Sailors")
ResultSetMetaData rsmd = results.getMetaData();
int numCols = rsmd.getColumnCount(), i;
StringBuffer buf = new StringBuffer();
while (results.next() && rowcount < 100){
for (i=1; i <= numCols; i++) {
if (i > 1) buf.append(",");
buf.append(results.getString(i));
}
buf.append("n");
}
results.close(); stmt.close(); con.close();
Similar deal for web scripting languages
• Common scenario today is to have a web
client
– A web form issues a query to the DB
– Results formatted as HTML
• Many web scripting languages used
– jsp, asp, PHP, etc.
– most of these are similar, look a lot like JDBC with
HTML mixed in
E.g. PHP/Postgres
<?php

$conn = pg_pconnect("dbname=cowbook user=jmh
password=secret");
if (!$conn) {
echo "An error occured.n";
exit;
}
$result = pg_query ($conn, "SELECT * FROM Sailors");
if (!$result) {
echo "An error occured.n"; exit;
}
$num = pg_num_rows($result);
for ($i=0; $i < $num; $i++) {
$r = pg_fetch_row($result, $i);
for ($j=0; $j < count($r); $j++) {
echo "$r[$j]&nbsp;";
}
echo "<BR>";
}

?>
API Summary
APIs are needed to interface DBMSs to
programming languages
• Embedded SQL uses “native drivers” and is
usually faster but less standard
• ODBC (used to be Microsoft-specific) for C/C++
• JDBC the standard for Java
• Scripting languages (PHP, Perl, JSP) are
becoming the preferred technique for web-based

More Related Content

Viewers also liked

phoenix overview presentation
phoenix overview presentation phoenix overview presentation
phoenix overview presentation
Saurabh Ranjan
 
อัลบั้มรูปโรงเรียนบ้านร่าปู
อัลบั้มรูปโรงเรียนบ้านร่าปูอัลบั้มรูปโรงเรียนบ้านร่าปู
อัลบั้มรูปโรงเรียนบ้านร่าปูkhunrit
 
Hukum tatacara-sunnah-kaifiyah-sholat-ied-dan-takbiran
Hukum tatacara-sunnah-kaifiyah-sholat-ied-dan-takbiranHukum tatacara-sunnah-kaifiyah-sholat-ied-dan-takbiran
Hukum tatacara-sunnah-kaifiyah-sholat-ied-dan-takbiran
Hannif Mardani
 
Pemodelan & simulasi 2
Pemodelan & simulasi 2Pemodelan & simulasi 2
Pemodelan & simulasi 2
Hannif Mardani
 
PostgreSQL: Mέθοδοι για Data Replication
PostgreSQL: Mέθοδοι για Data ReplicationPostgreSQL: Mέθοδοι για Data Replication
PostgreSQL: Mέθοδοι για Data Replication
Jimmy Angelakos
 
Using PostgreSQL with Bibliographic Data
Using PostgreSQL with Bibliographic DataUsing PostgreSQL with Bibliographic Data
Using PostgreSQL with Bibliographic Data
Jimmy Angelakos
 
Ace 10 high efficiency plumbing fixtures are flush with savings
Ace 10 high efficiency plumbing fixtures are flush with savingsAce 10 high efficiency plumbing fixtures are flush with savings
Ace 10 high efficiency plumbing fixtures are flush with savings
Troy Aichele
 
Watersmart innovations 2010 cost of capacity
Watersmart innovations 2010   cost of capacityWatersmart innovations 2010   cost of capacity
Watersmart innovations 2010 cost of capacity
Troy Aichele
 
Ace 10 conference green plumbersusa - 2 tips from each of the 5 core courses
Ace 10 conference green plumbersusa - 2 tips from each of the 5 core coursesAce 10 conference green plumbersusa - 2 tips from each of the 5 core courses
Ace 10 conference green plumbersusa - 2 tips from each of the 5 core courses
Troy Aichele
 
Eισαγωγή στην PostgreSQL - Χρήση σε επιχειρησιακό περιβάλλον
Eισαγωγή στην PostgreSQL - Χρήση σε επιχειρησιακό περιβάλλονEισαγωγή στην PostgreSQL - Χρήση σε επιχειρησιακό περιβάλλον
Eισαγωγή στην PostgreSQL - Χρήση σε επιχειρησιακό περιβάλλον
Jimmy Angelakos
 
Presentation gmp
Presentation gmpPresentation gmp
Presentation gmp
khunrit
 
Presentation gmp
Presentation gmpPresentation gmp
Presentation gmp
khunrit
 

Viewers also liked (13)

phoenix overview presentation
phoenix overview presentation phoenix overview presentation
phoenix overview presentation
 
อัลบั้มรูปโรงเรียนบ้านร่าปู
อัลบั้มรูปโรงเรียนบ้านร่าปูอัลบั้มรูปโรงเรียนบ้านร่าปู
อัลบั้มรูปโรงเรียนบ้านร่าปู
 
Hukum tatacara-sunnah-kaifiyah-sholat-ied-dan-takbiran
Hukum tatacara-sunnah-kaifiyah-sholat-ied-dan-takbiranHukum tatacara-sunnah-kaifiyah-sholat-ied-dan-takbiran
Hukum tatacara-sunnah-kaifiyah-sholat-ied-dan-takbiran
 
Pemodelan & simulasi 2
Pemodelan & simulasi 2Pemodelan & simulasi 2
Pemodelan & simulasi 2
 
PostgreSQL: Mέθοδοι για Data Replication
PostgreSQL: Mέθοδοι για Data ReplicationPostgreSQL: Mέθοδοι για Data Replication
PostgreSQL: Mέθοδοι για Data Replication
 
Using PostgreSQL with Bibliographic Data
Using PostgreSQL with Bibliographic DataUsing PostgreSQL with Bibliographic Data
Using PostgreSQL with Bibliographic Data
 
Ace 10 high efficiency plumbing fixtures are flush with savings
Ace 10 high efficiency plumbing fixtures are flush with savingsAce 10 high efficiency plumbing fixtures are flush with savings
Ace 10 high efficiency plumbing fixtures are flush with savings
 
Watersmart innovations 2010 cost of capacity
Watersmart innovations 2010   cost of capacityWatersmart innovations 2010   cost of capacity
Watersmart innovations 2010 cost of capacity
 
Ace 10 conference green plumbersusa - 2 tips from each of the 5 core courses
Ace 10 conference green plumbersusa - 2 tips from each of the 5 core coursesAce 10 conference green plumbersusa - 2 tips from each of the 5 core courses
Ace 10 conference green plumbersusa - 2 tips from each of the 5 core courses
 
Eισαγωγή στην PostgreSQL - Χρήση σε επιχειρησιακό περιβάλλον
Eισαγωγή στην PostgreSQL - Χρήση σε επιχειρησιακό περιβάλλονEισαγωγή στην PostgreSQL - Χρήση σε επιχειρησιακό περιβάλλον
Eισαγωγή στην PostgreSQL - Χρήση σε επιχειρησιακό περιβάλλον
 
Pemodelan & simulasi
Pemodelan & simulasiPemodelan & simulasi
Pemodelan & simulasi
 
Presentation gmp
Presentation gmpPresentation gmp
Presentation gmp
 
Presentation gmp
Presentation gmpPresentation gmp
Presentation gmp
 

Similar to asal12 sqliii

lecture05-14f.ppt
lecture05-14f.pptlecture05-14f.ppt
lecture05-14f.ppt
VADAPALLYPRAVEENKUMA1
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
arnold 7490
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
Vignesh Saravanan
 
Presentation.pdf
Presentation.pdfPresentation.pdf
Presentation.pdf
HosniJuarez2
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
Edhole.com
 
SQL
SQLSQL
Lab
LabLab
CS121Lec04.pdf
CS121Lec04.pdfCS121Lec04.pdf
CS121Lec04.pdf
georgejustymirobi1
 
CS 542 Database Index Structures
CS 542 Database Index StructuresCS 542 Database Index Structures
CS 542 Database Index Structures
J Singh
 
lecture9Calc.ppt
lecture9Calc.pptlecture9Calc.ppt
lecture9Calc.ppt
rituah
 
CS 542 Controlling Database Integrity and Performance
CS 542 Controlling Database Integrity and PerformanceCS 542 Controlling Database Integrity and Performance
CS 542 Controlling Database Integrity and Performance
J Singh
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
Krizia Capacio
 
6_SQL.pdf
6_SQL.pdf6_SQL.pdf
6_SQL.pdf
LPhct2
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
Nitesh Singh
 
Database
Database Database
Les10.ppt
Les10.pptLes10.ppt
Les10
Les10Les10
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
Prosanta Ghosh
 
relational algebra and it's implementation
relational algebra and it's implementationrelational algebra and it's implementation
relational algebra and it's implementation
dbmscse61
 
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 testLecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
ssuser9dddf7
 

Similar to asal12 sqliii (20)

lecture05-14f.ppt
lecture05-14f.pptlecture05-14f.ppt
lecture05-14f.ppt
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
 
Presentation.pdf
Presentation.pdfPresentation.pdf
Presentation.pdf
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
SQL
SQLSQL
SQL
 
Lab
LabLab
Lab
 
CS121Lec04.pdf
CS121Lec04.pdfCS121Lec04.pdf
CS121Lec04.pdf
 
CS 542 Database Index Structures
CS 542 Database Index StructuresCS 542 Database Index Structures
CS 542 Database Index Structures
 
lecture9Calc.ppt
lecture9Calc.pptlecture9Calc.ppt
lecture9Calc.ppt
 
CS 542 Controlling Database Integrity and Performance
CS 542 Controlling Database Integrity and PerformanceCS 542 Controlling Database Integrity and Performance
CS 542 Controlling Database Integrity and Performance
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
6_SQL.pdf
6_SQL.pdf6_SQL.pdf
6_SQL.pdf
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
Database
Database Database
Database
 
Les10.ppt
Les10.pptLes10.ppt
Les10.ppt
 
Les10
Les10Les10
Les10
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
 
relational algebra and it's implementation
relational algebra and it's implementationrelational algebra and it's implementation
relational algebra and it's implementation
 
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 testLecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
 

Recently uploaded

Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

asal12 sqliii

  • 1. SQL: The Query Language Part 3 CS186, Fall 2005 R &G - Chapters 5-6 It is not every question that deserves an answer. Publius Syrus. 42 B. C.
  • 2. Null Values • Field values in a tuple are sometimes unknown (e.g., a rating has not been assigned) or inapplicable (e.g., no spouse’s name). – SQL provides a special value null for such situations. • The presence of null complicates many issues. E.g.: – Special operators needed to check if value is/is not null. – Is rating>8 true or false when rating is equal to null? What about AND, OR and NOT connectives? – We need a 3-valued logic (true, false and unknown). – Meaning of constructs must be defined carefully. (e.g., WHERE clause eliminates rows that don’t evaluate to true.) – New operators (in particular, outer joins) possible/needed.
  • 3. Joins SELECT (column_list) FROM table_name [INNER | {LEFT |RIGHT | FULL } OUTER] JOIN table_name ON qualification_list WHERE … Explicit join semantics needed unless it is an INNER join (INNER is default)
  • 4. Inner Join Only the rows that match the search conditions are returned. SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid Returns only those sailors who have reserved boats SQL-92 also allows: SELECT s.sid, s.name, r.bid FROM Sailors s NATURAL JOIN Reserves r “NATURAL” means equi-join for each pair of attributes with the same name
  • 5. SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid sid 22 31 95 sname rating age Dustin 7 45.0 Lubber 8 55.5 Bob 3 63.5 sid bid day 22 101 10/10/96 95 103 11/12/96 s.sid s.name r.bid 22 Dustin 101 95 Bob 103
  • 6. Left Outer Join Left Outer Join returns all matched rows, plus all unmatched rows from the table on the left of the join clause (use nulls in fields of non-matching tuples) SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid Returns all sailors & information on whether they have reserved boats
  • 7. SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid sid 22 31 95 sname rating age Dustin 7 45.0 Lubber 8 55.5 Bob 3 63.5 s.sid 22 95 31 sid bid day 22 101 10/10/96 95 103 11/12/96 s.name r.bid Dustin 101 Bob 103 Lubber
  • 8. Right Outer Join Right Outer Join returns all matched rows, plus all unmatched rows from the table on the right of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & information on which ones are reserved.
  • 9. SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid sid bid day 22 101 10/10/96 95 103 11/12/96 r.sid bid 101 102 103 104 b.bid 22 95 101 102 103 104 bname Interlake Interlake Clipper Marine b.name Interlake Interlake Clipper Marine color blue red green red
  • 10. Full Outer Join Full Outer Join returns all (matched or unmatched) rows from the tables on both sides of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r FULL OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & all information on reservations
  • 11. SELECT r.sid, b.bid, b.name FROM Reserves r FULL OUTER JOIN Boats b ON r.bid = b.bid sid bid 22 95 day 101 10/10/96 103 11/12/96 r.sid b.bid 22 95 bid 101 102 103 104 101 102 103 104 bname Interlake Interlake Clipper Marine b.name Interlake Interlake Clipper Marine color blue red green red Note: in this case it is the same as the ROJ because bid is a foreign key in reserves, so all reservations must have a corresponding tuple in boats.
  • 12. Conceptual SQL Evaluation SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification Project away columns (just keep those used in SELECT, GBY, HAVING) SELECT [DISTINCT] Eliminate duplicates Apply selections (eliminate rows) WHERE HAVING Eliminate groups Relation cross-product FROM GROUP BY Form groups & aggregate
  • 13. Sorting the Results of a Query • ORDER BY column [ ASC | DESC] [, ...] SELECT S.rating, S.sname, S.age FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ ORDER BY S.rating, S.sname; • Can order by any column in SELECT list, including expressions or aggs: SELECT S.sid, COUNT (*) AS redrescnt FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ GROUP BY S.sid ORDER BY redrescnt DESC;
  • 14. Views: Defining External DB Schemas CREATE VIEW view_name AS select_statement Makes development simpler Often used for security Not instantiated - makes updates tricky CREATE VIEW Reds AS SELECT B.bid, COUNT (*) AS scount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid
  • 15. Views Instead of Relations in Queries CREATE VIEW Reds AS SELECT B.bid, COUNT (*) AS scount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid b.bid scount 102 1 Reds SELECT bname, scount FROM Reds R, Boats B WHERE R.bid=B.bid AND scount < 10
  • 16. Discretionary Access Control GRANT privileges ON object TO users [WITH GRANT OPTION] • Object can be a Table or a View • Privileges can be: • Select • Insert • Delete • References (cols) – allow to create a foreign key that references the specified column(s) • All • Can later be REVOKEd • Users can be single users or groups
  • 17. Two more important topics • Constraints • SQL embedded in other languages
  • 18. Integrity Constraints (Review) • An IC describes conditions that every legal instance of a relation must satisfy. – Inserts/deletes/updates that violate IC’s are disallowed. – Can be used to ensure application semantics (e.g., sid is a key), or prevent inconsistencies (e.g., sname has to be a string, age must be < 200) • Types of IC’s : Domain constraints, primary key constraints, foreign key constraints, general constraints. – Domain constraints: Field values must be of right type. Always enforced. – Primary key and foreign key constraints: you know them.
  • 19. General Constraints • • • • CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, Useful when PRIMARY KEY (sid), more general ICs CHECK ( rating >= 1 than keys are AND rating <= involved. CREATE TABLE Reserves Can use queries ( sname CHAR(10), to express bid INTEGER, constraint. day DATE, Checked on PRIMARY KEY (bid,day), insert or update. CONSTRAINT noInterlakeRes Constraints can CHECK (`Interlake’ <> be named. ( SELECT B.bname FROM Boats B WHERE B.bid=bid))) 10 ))
  • 20. Constraints Over Multiple Relations • • • • • • CREATE TABLE Sailors ( sid INTEGER, Number of boats sname CHAR(10), plus number of rating INTEGER, Awkward and sailors is < 100 wrong! age REAL, Only checks PRIMARY KEY (sid), sailors! Only required to CHECK hold if the ( (SELECT COUNT (S.sid) FROM Sailors S) associated table is + (SELECT COUNT (B.bid) FROM non-empty. ASSERTION is the right solution; not associated with either table. Unfortunately, not supported in many DBMS. Triggers are Boats B) < 100 ) CREATE ASSERTION smallClub CHECK ( (SELECT COUNT (S.sid) FROM Sailors + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) S)
  • 21. Writing Applications with SQL • SQL is not a general purpose programming language. + Tailored for data retrieval and manipulation + Relatively easy to optimize and parallelize - Can’t write entire apps in SQL alone Options: Make the query language “Turing complete” Avoids the “impedance mismatch” but, loses advantages of relational language simplicity Allow SQL to be embedded in regular programming languages. Q: What needs to be solved to make the latter approach work?
  • 22. Embedded SQL • DBMS vendors usually provide “host language bindings” • General pattern • • – – – – E.g. for C or COBOL Allow SQL statements to be called from within a program Typically you preprocess your programs Preprocessor generates calls to a proprietary DB connectivity library – One call to connect to the right database (login, etc.) – SQL statements can refer to host variables from the language Typically vendor-specific – We won’t look at any in detail, we’ll look at standard stuff Problem – SQL relations are (multi-)sets, no a priori bound on the number of records. No such data structure in C. – SQL supports a mechanism called a cursor to handle this.
  • 23. Just to give you a flavor EXEC SQL SELECT S.sname, S.age INTO :c_sname,:c_age FROM Sailors S WHERE S.sid = :c_sid
  • 24. Cursors • • • • • Can declare a cursor on a relation or query Can open a cursor Can repeatedly fetch a tuple (moving the cursor) Special return value when all tuples have been retrieved. ORDER BY allows control over the order in which tuples are returned. • Fields in ORDER BY clause must also appear in SELECT clause. • • Can also modify/delete tuple pointed to by a cursor – A “non-relational” way to get a handle to a particular tuple There’s an Embedded SQL syntax for cursors – DECLARE <cursorname> CURSOR FOR <select stmt> – FETCH FROM <cursorname> INTO <variable names> – But we’ll use JDBC instead
  • 25. Database APIs: Alternative to embedding • Rather than modify compiler, add a library with database calls (API) – special procedures/objects – passes SQL strings from language, presents result sets in a language-friendly way – ODBC a C/C++ standard started on Windows – JDBC a Java equivalent – Most scripting languages have similar things • E.g. For Perl there is DBI, “oraPerl”, other packages • Mostly DBMS-neutral – at least try to hide distinctions across different DBMSs
  • 26. Architecture Application ODBC driver Data Source • A lookup service maps “data source names” (“DSNs”) to drivers – Typically handled by OS • • • • Based on the DSN used, a “driver” is linked into the app at runtime The driver traps calls, translates them into DBMS-specific code Database can be across a network ODBC is standard, so the same program can be used (in principle) to access multiple database systems • Data source may not even be an SQL database!
  • 27. ODBC/JDBC • Various vendors provide drivers – MS bundles a bunch into Windows – Vendors like DataDirect and OpenLink sell drivers for multiple OSes • Drivers for various data sources – Relational DBMSs (Oracle, DB2, SQL Server, Informix, etc.) – “Desktop” DBMSs (Access, Dbase, Paradox, FoxPro, etc.) – Spreadsheets (MS Excel, Lotus 1-2-3, etc.) – Delimited text files (.CSV, .TXT, etc.) • You can use JDBC/ODBC client s over many data sources – E.g. MS Query comes with many versions of MS Office (msqry32.exe) • Can write your own Java or C++ programs against xDBC
  • 28. JDBC • Part of Java, very easy to use • Java comes with a JDBC-to-ODBC bridge – So JDBC code can talk to any ODBC data source – E.g. look in your Windows Control Panel for JDBC/ODBC drivers! • JDBC tutorial online – http://developer.java.sun.com/developer/Books/JDBC Tutorial/
  • 29. JDBC Basics: Connections • A Connection is an object representing a login to a database // GET CONNECTION Connection con; try { con = DriverManager.getConnection( "jdbc:odbc:sailorsDB", userName,password); } catch(Exception e){ System.out.println(e); • Eventually you close the connection // CLOSE CONNECTION try { con.close(); } catch (Exception e) { System.out.println(e); } }
  • 30. JDBC Basics: Statements • You need a Statement object for each SQL statement // CREATE STATEMENT Statement stmt; try { stmt = con.createStatement(); } catch (Exception e){ System.out.println(e); } Soon we’ll say stmt.executeQuery(“select …”);
  • 31. CreateStatement cursor behavior • • • • Two optional args to createStatement: – createStatement(ResultSet.<TYPE>, ResultSet.<CONCUR>) – Corresponds to SQL cursor features <TYPE> is one of – TYPE_FORWARD_ONLY: can’t move cursor backward – TYPE_SCROLL_INSENSITIVE: can move backward, but doesn’t show results of any updates – TYPE_SCROLL_SENSITIVE: can move backward, will show updates made while result set is open <CONCUR> is one of – CONCUR_READ_ONLY: this statement doesn’t allow updates – CONCUR_UPDATABLE: this statement allows updates Defaults: – TYPE_FORWARD_ONLY and CONCUR_READ_ONLY
  • 32. JDBC Basics: ResultSet • • A ResultSet object serves as a cursor for the statement’s results (stmt.executeQuery() ) // EXECUTE QUERY ResultSet results; try { results = stmt.executeQuery( "select * from Sailors") } catch (Exception e){ System.out.println(e); } Obvious handy methods: – results.next() advances cursor to next tuple • Returns “false” when the cursor slides off the table (beginning or end) – “scrollable” cursors: • results.previous(), results.relative(int), results.absolute(int), results.first(), results.last(), results.beforeFirst(), results.afterLast()
  • 33. ResultSet Metadata • Can find out stuff about the ResultSet schema via ResultSetMetaData ResultSetMetaData rsmd = results.getMetaData(); int numCols = rsmd.getColumnCount(); int i, rowcount = 0; • // get column header info for (i=1; i <= numCols; i++){ if (i > 1) buf.append(","); buf.append(rsmd.getColumnLabel(i)); } buf.append("n"); Other ResultSetMetaData methods: – getColumnType(i), isNullable(i), etc.
  • 34. Getting Values in Current of Cursor • getString // break it off at 100 rows max while (results.next() && rowcount < 100){ // Loop through each column, getting the // column data and displaying for (i=1; i <= numCols; i++) { if (i > 1) buf.append(","); buf.append(results.getString(i)); } buf.append("n"); rowcount++; • } Similarly, getFloat, getInt, etc.
  • 35. Updating Current of Cursor • Update fields in current of cursor: result.next(); result.updateInt("Rating", 10); • Also updateString, updateFloat, etc. • Or can always submit a full SQL UPDATE statement – Via executeQuery() • The original statement must have been CONCUR_UPDATABLE in either case!
  • 36. Cleaning up Neatly try { // CLOSE RESULT SET results.close(); // CLOSE STATEMENT stmt.close(); // CLOSE CONNECTION con.close(); } catch (Exception e) { System.out.println(e); }
  • 37. Putting it Together (w/o try/catch) Connection con = DriverManager.getConnection("jdbc:odbc:weblog",userN ame,password); Statement stmt = con.createStatement(); ResultSet results = stmt.executeQuery("select * from Sailors") ResultSetMetaData rsmd = results.getMetaData(); int numCols = rsmd.getColumnCount(), i; StringBuffer buf = new StringBuffer(); while (results.next() && rowcount < 100){ for (i=1; i <= numCols; i++) { if (i > 1) buf.append(","); buf.append(results.getString(i)); } buf.append("n"); } results.close(); stmt.close(); con.close();
  • 38. Similar deal for web scripting languages • Common scenario today is to have a web client – A web form issues a query to the DB – Results formatted as HTML • Many web scripting languages used – jsp, asp, PHP, etc. – most of these are similar, look a lot like JDBC with HTML mixed in
  • 39. E.g. PHP/Postgres <?php $conn = pg_pconnect("dbname=cowbook user=jmh password=secret"); if (!$conn) { echo "An error occured.n"; exit; } $result = pg_query ($conn, "SELECT * FROM Sailors"); if (!$result) { echo "An error occured.n"; exit; } $num = pg_num_rows($result); for ($i=0; $i < $num; $i++) { $r = pg_fetch_row($result, $i); for ($j=0; $j < count($r); $j++) { echo "$r[$j]&nbsp;"; } echo "<BR>"; } ?>
  • 40. API Summary APIs are needed to interface DBMSs to programming languages • Embedded SQL uses “native drivers” and is usually faster but less standard • ODBC (used to be Microsoft-specific) for C/C++ • JDBC the standard for Java • Scripting languages (PHP, Perl, JSP) are becoming the preferred technique for web-based

Editor's Notes

  1. The slides for this text are organized into several modules. Each lecture contains about enough material for a 1.25 hour class period. (The time estimate is very approximate--it will vary with the instructor, and lectures also differ in length; so use this as a rough guideline.) This covers Lectures 3 and 4 (of 6) in Module (3). Module (1): Introduction (DBMS, Relational Model) Module (2): Storage and File Organizations (Disks, Buffering, Indexes) Module (3): Database Concepts (Relational Queries, DDL/ICs, Views and Security) Module (4): Relational Implementation (Query Evaluation, Optimization) Module (5): Database Design (ER Model, Normalization, Physical Design, Tuning) Module (6): Transaction Processing (Concurrency Control, Recovery) Module (7): Advanced Topics