Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you...
Transcript of Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you...
![Page 1: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/1.jpg)
1
Principles of Information and Database Management
198:336Week 5 – Feb 21
Matthew Stone
Today
Joins and other advanced features of SQL– Recap– Loose ends– Joins– Optimization and relational algebra– Summary statistics in SQL
![Page 2: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/2.jpg)
2
Recap
Relational model
sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8
In the relational model, IM stores set of relations or tables
TELLINFORMATION
MANAGERASK
DEFINE
![Page 3: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/3.jpg)
3
SQL Language
TELLINFORMATION
MANAGERASK
DEFINE
SQL: insert valuesSQL: select queries
SQL: create table
Relational Model: Example
Definition statement
create table books( isbn char(13) not null primary key,author char(80),title char(100),price float(4,2)
);
![Page 4: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/4.jpg)
4
Relational model example
Tell statement
insert into books values('0-672-31697-8','Michael Morgan','Java 2 for Professional Developers',34.99);
Relational model example
Tell statement– Adds a row to the specified table in the information
manager to include the specified entity or relationship.
![Page 5: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/5.jpg)
5
Relational model example
Query example
SELECT author,titleFROM booksWHERE price > 30;
Relational model example
This returns a new table
author title Michael Morgan
Java 2 for Professional Developers
... …
![Page 6: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/6.jpg)
6
Options
not nullprimary keyreferences table(field)
Interaction
use database;show tables;describe table;
![Page 7: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/7.jpg)
7
Loose ends
Null values and primary keys
create table nullness (id integer primary key, stuff text
);
insert into nullness values (null, "you lose");
Loose ends
Updating an existing row
update tableset column=expressionwhere restriction
![Page 8: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/8.jpg)
8
Update
changes columns in existing table rows– Set clause indicates which columns to modify
and the values they should be given.– Where clause specifies which rows should be
updated.
Example
Book gets “new edition”
Current row of books:– isbn='0-672-31697-8',
– author='Michael Morgan',
– title='Java 2 for Professional Developers',
– price=34.99
![Page 9: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/9.jpg)
9
Example
Command:Update books
Set title = 'Java 3 for Professional Developers',price = 39.99where isbn='0-672-31697-8‘
Result: row changed to:– isbn='0-672-31697-8',– author='Michael Morgan',
– title='Java 3 for Professional Developers',
– price=39.99
Loose ends
Removing an existing row
delete from tablewhere restriction
![Page 10: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/10.jpg)
10
Example
Book goes out of print
delete from bookswhere isbn='0-672-31697-8‘
Loose ends
Adding or deleting columns: alter table
Add a column, give all rows null value:
alter table booksadd column publisher char(40)
![Page 11: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/11.jpg)
11
Loose ends
Adding or deleting columns: alter table
Get rid of a column:
alter table booksdrop column publisher
Loose ends
Adding or deleting columns: alter table
Lots of other ways to use this command.
![Page 12: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/12.jpg)
12
Adding or deleting columns
Why should you not have to do this?
Loose ends
Discarding whole tables from the database
drop table books
![Page 13: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/13.jpg)
13
Demo break
Any questions?
Joins - Motivation
How do you combine information from multiple tables?
Example, from book domain:who ordered what titles?
![Page 14: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/14.jpg)
14
Recap
Not useful:select C.name, O.isbn from customers C, order_items O
– performs cross product on tables– no connections between rows
Recap
Need to establish relationshipsselect C.name, I.isbn from customers C, orders O, order_items I
where C.customerid = O.customerid and O.ordernumber = I.orderid
![Page 15: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/15.jpg)
15
Joins
How you evaluate these queries is very important.– Database designers describe algorithms
using idea of a join – an operation that combines two tables together to give a new table.
Relational algebra
Describes operations to build relations– Used in DB to represent query– Can find equivalent expressions– Can estimate how long evaluation will take
![Page 16: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/16.jpg)
16
Selection
Extract rows from a relation
extract all the rows from relation R that satisfy condition
( )condition R�
Example
Get the rows from S where rating > 8
Corresponds toselect * from S where rating > 8
8( )rating S�
�
![Page 17: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/17.jpg)
17
Projection
Extract columns from a relation
make a smaller table from R with just the specified columns
( )columns R�
Example
Extract sailor names and ratings
corresponds toselect sname, rating from S
, ( )sname rating S�
![Page 18: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/18.jpg)
18
Set operations
UnionIntersectionDifferenceCross-product
R S�
R S�
R S�
R S�
General Joins
Accepts:– join condition– two relations
Returns – new relation
( )c cR S R S�� �Ä
![Page 19: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/19.jpg)
19
Equijoins
Join conditions contain only equalitiesDuplicated fields are dropped
Natural join: special case– all fields in common are equated
. .R i S iR S�
Ä
Equivalences
Cascading of selections
Commutativity of selections
( ) ( ( ))c d c dS S� � ��
�
( ( )) ( ( ))c d d cS S� � � ��
![Page 20: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/20.jpg)
20
Equivalences
Cascading projections
( ( )) ( )c d cS S� � ��
Equivalences
Commutativity of joins
Associativity of joins
R S S R� � �
( ) ( )R S T R S T� � � � �
![Page 21: Principles of Information and Database Management Week 5 ...mdstone/class/336/l5.pdf · How you evaluate these queries is very important. – Database designers describe algorithms](https://reader033.fdocuments.us/reader033/viewer/2022060412/5f10f869e719c70f50236c33/html5/thumbnails/21.jpg)
21
Equivalences
Depending on the conditions at playSelections and projections commute
Selection and join commute
c c( ) ( )R S R S� �� ��
c c c c( ( )) ( ( ))R R� � � ��
What this means in practice
The DB implementation can search for a good way to evaluate a query!