Web Database Programming Week 9

7
Web Database Programming Week 9 DB Administration – Multiple Tables

description

Web Database Programming Week 9. DB Administration – Multiple Tables. 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 - PowerPoint PPT Presentation

Transcript of Web Database Programming Week 9

Page 1: Web Database Programming Week 9

Web Database Programming Week 9

DB Administration –

Multiple Tables

Page 2: Web Database Programming Week 9

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

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

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

Aggregation Functions

• Count()

• Sum()

• Min()

• Max()

• Avg()

SELECT city, COUNT(*) from customer

GROUP BY city;

Page 6: Web Database Programming Week 9

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

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%’