Web Database Programming Week 9
-
Upload
ivana-calhoun -
Category
Documents
-
view
36 -
download
2
description
Transcript of Web Database Programming Week 9
![Page 1: Web Database Programming Week 9](https://reader036.fdocuments.us/reader036/viewer/2022082505/56812eb2550346895d9458b7/html5/thumbnails/1.jpg)
Web Database Programming Week 9
DB Administration –
Multiple Tables
![Page 2: Web Database Programming Week 9](https://reader036.fdocuments.us/reader036/viewer/2022082505/56812eb2550346895d9458b7/html5/thumbnails/2.jpg)
Table Relationships
• One to many– Use foreign key at the Many side of the
relationship– E.g. pets & species
• Many to many– Use an extra relational table– This table contains foreign keys for Both
sides of relationship– E.g. pets ownership table captures
relationship between pets & owners
![Page 3: Web Database Programming Week 9](https://reader036.fdocuments.us/reader036/viewer/2022082505/56812eb2550346895d9458b7/html5/thumbnails/3.jpg)
Display Table w/ Foreign Keys (FKs)
• Need to join the table(s) containing FKs with the table(s) providing FKs
• Must specify key relationships in joinSELECT ownership.id, owners.lastname, pets.nameFROM ownership, owners, petsWHERE ownership.ownerId = owners.id AND ownership.petId = pets.id
![Page 4: Web Database Programming Week 9](https://reader036.fdocuments.us/reader036/viewer/2022082505/56812eb2550346895d9458b7/html5/thumbnails/4.jpg)
Rows Aggregation
• GROUP BY clause– Rows with the same value will be grouped
together
• Enable the use of aggregation functions
SELECT city from customer
GROUP BY city;
![Page 5: Web Database Programming Week 9](https://reader036.fdocuments.us/reader036/viewer/2022082505/56812eb2550346895d9458b7/html5/thumbnails/5.jpg)
Aggregation Functions
• Count()
• Sum()
• Min()
• Max()
• Avg()
SELECT city, COUNT(*) from customer
GROUP BY city;
![Page 6: Web Database Programming Week 9](https://reader036.fdocuments.us/reader036/viewer/2022082505/56812eb2550346895d9458b7/html5/thumbnails/6.jpg)
Example
• Pet Ownership Administration:– Create an ownership– Update an ownership– Delete an ownership– Summary report
• E.g. a table listing the number of pets owned by each owner
![Page 7: Web Database Programming Week 9](https://reader036.fdocuments.us/reader036/viewer/2022082505/56812eb2550346895d9458b7/html5/thumbnails/7.jpg)
Additional SQL Criteria for WHERE Clause
• Direct comparison: =, <>, <=, <, >, >=• Existence: IS NULL, IS NOT NULL• Between WHERE aNumber BETWEEN 234 AND 999
• Combining: AND, ORWHERE (LastName = ‘Smith’ AND FirstName = ‘Jack’) OR (LastName = ‘Jones’ AND FirstName = ‘Kim’)
• Negation: WHERE NOT( LastName >= ‘Jones’) • Set membership:
WHERE LastName In (‘Jones’, ‘Smith’,‘$formValue’)
• Like: WHERE LastName LIKE ‘Johns[oe]n’WHERE LastName LIKE ‘Ben%’