Introduction to Databases 6.830 Lecture 1 Sam Madden [email protected] [email protected] .
-
Upload
axel-simon -
Category
Documents
-
view
244 -
download
1
Transcript of Introduction to Databases 6.830 Lecture 1 Sam Madden [email protected] [email protected] .
![Page 1: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/1.jpg)
Introduction to Databases
6.830 Lecture 1Sam Madden
http://db.csail.mit.edu/6.830
![Page 2: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/2.jpg)
Administrivia
http://db.csail.mit.edu/6.830Email: [email protected] questions on Piazza!
Lecturers:Sam MaddenAaron Elmore
TAs:Rebecca TaftHongyu Yang
Office hours: TBD – G9 Lounge
![Page 3: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/3.jpg)
Textbooks
• Readings in Database Systems– Available on Books24x7.com
• Database Management Systems, 3rd ed. by Ramakrishnan and Gehrke
![Page 4: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/4.jpg)
Zoo Data ModelEntity Relationship Diagram
Animal Cage
Keeper
keeps
Name
1
name
Time1
feedTime
Age
Name
Species
1
1
1
name
age
species
Animals have names, ages, speciesKeepers have namesCages have cleaning times, buildingsAnimals are in 1 cage; cages have multiple animalsKeepers keep multiple cages, cages kept by multiple keepers
1
11
Building1
bldg
entity entity
entity
contains 1
relationship
![Page 5: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/5.jpg)
Our Zoo
LankySlimy
Sally the StudentSam the Salamander Mike the Giraffe
![Page 6: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/6.jpg)
SQL – Structured Query Language
SELECT field1, …, fieldM FROM table1, … WHERE condition1, …
INSERT INTO table VALUES (field1, …)
UPDATE table SET field1 = X, …WHERE condition1,…
![Page 7: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/7.jpg)
Names of Giraffes
• Imperativefor each row r in animals
if r.species = ‘giraffe’output r.name
• DeclarativeSELECT r.name FROM animalsWHERE r.species = ‘giraffe’
![Page 8: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/8.jpg)
Cages in Building 32
• Imperativefor each row a in animals
for each row c in cagesif a.cageno = c.no and
c.bldg = 32output a
• DeclarativeSELECT a.name FROM animals AS a, cages AS c WHERE a.cageno = c.no AND c.bldg = 32JOIN
NESTED
LOOPS
![Page 9: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/9.jpg)
Average Age of Bears
• DeclarativeSELECT AVG(age) FROM animalsWHERE species = ‘bear’
![Page 10: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/10.jpg)
Complex QueriesFind pairs of animals of the same species and different genders older than 1 year:
SELECT a1.name,a2.nameFROM animals as a1, animals as a2WHERE a1.gender = M and a2.gender = FAND a1.species = a2.speciesAND a1.age > 1 and a2.age > 1
Find cages with salamanders fed later than the average feedtime of any cage:SELECT cages.cageid FROM cages, animalsWHERE animals.species = ’salamander'AND animals.cageid = cages.cageidAND cages.feedtime >
(SELECT AVG(feedtime) FROM cages )
“self join”
“nested queries”
![Page 11: Introduction to Databases 6.830 Lecture 1 Sam Madden madden@csail.mit.edu madden@csail.mit.edu .](https://reader036.fdocuments.us/reader036/viewer/2022081508/56649c7c5503460f94930618/html5/thumbnails/11.jpg)
Complex Queries 2Find keepers who keep both students and salamanders:
SELECT keeper.name FROM keeper, cages as c1, cages as c2,
keeps as k1, keeps as k2, animals as a1, animals as a2WHERE c1.cageid = k1.cageid AND keeper.keeperid = k1.keeperid AND c2.cageid = k2.cageid AND keeper.keeperid = k2.keeperidAND a1.species = ’student' AND a2.species = ’salamander' AND c1.cageid = a1.cageid AND c2.cageid = a2.cageid
a1 c1 k1
a2 c2 k2
keeper
keeper.keeperid = k2.keeperid
keeper.keeperid = k1.keeperid
species = ‘shrew’
species = ‘student’