relational databases - northwestern university · 2013-01-30 · • best practices for database...

29
RELATIONAL DATABASES SCRG Lab Group Demo, 23 Jan 2012

Upload: others

Post on 02-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

RELATIONAL DATABASES SCRG Lab Group Demo, 23 Jan 2012

Page 2: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Outline • Why use a relational database • Best practices for database design • Working with a relational database (MySQL and

phpMyAdmin)

Page 3: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Outline • Why use a relational database • Best practices for database design • Working with a relational database (MySQL and

phpMyAdmin)

Page 4: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

A systematic framework for storing and managing data

Page 5: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 6: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 7: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 8: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

One size does not fit all

Page 9: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Outline • Why use a relational database • Best practices for database design • Working with a relational database (MySQL and

phpMyAdmin)

Page 10: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Example Data • Talkers

• Sentences • Acoustic analysis on sentences spoken by talkers

Page 11: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

“Flat Database” design

Page 12: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 13: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 14: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

“Column Heavy” design

Page 15: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Flat vs. Relational

Page 16: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 17: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 18: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 19: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 20: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only
Page 21: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Flat vs. Relational

Page 22: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Outline • Why use a relational database • Best practices for database design • Working with a relational database (MySQL and

phpMyAdmin)

Page 23: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Basic query • SELECT [columns] FROM [table] WHERE [filters]

• SELECT * FROM `talker` WHERE 1 •  (show all talkers)

• SELECT * FROM `talker` WHERE `age`>=20 •  (get all talkers who are 25 or older)

• SELECT `talkerCode` FROM `talker` WHERE `age`>=20 •  (same as above but only retrieves the talkerCode column)

Page 24: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Combining tables • SELECT [columns] FROM [table1],[table2] WHERE [joins]

AND [filters]

• SELECT * FROM `talker`, `sentenceProduction` WHERE `sentenceProduction`.`talker_id` = `talker`.`talker_id` •  Combines the talkers and sentenceProduction table

• SELECT * FROM `sentence`, `sentenceProduction` WHERE `sentenceProduction`.`sentence_id` = `sentence`.`sentence_id` •  (Combine sentence and sentenceProduction table)

Page 25: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Combining tables SELECT * FROM `talker`, `sentenceProduction` WHERE `sentenceProduction`.`talker_id` = `talker`.`talker_id` AND `talker`.`age` >=20

•  Combines the talkers and sentenceProduction table and only get talkers who are 25 or older

Page 26: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Combining tables SELECT * FROM `talker`, `sentence`, `sentenceProduction` WHERE `sentenceProduction`.`talker_id` = `talker`.`talker_id` AND `sentenceProduction`.`sentence_id`= `sentence`.`sentence_id`

•  Combines the talkers, sentence and sentenceProduction table

Page 27: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Combining tables SELECT `talker`.`talkerCode`, `talker`.`age` FROM `talker`, `sentence`, `sentenceProduction` WHERE `sentenceProduction`.`talker_id` = `talker`.`talker_id` AND `sentenceProduction`.`sentence_id`= `sentence`.`sentence_id`

•  Combines the talkers, sentence and sentenceProduction table and only retrieves the talkerCode and age columns from the talker table

Page 28: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Complex example SELECT `talker`.`talkerCode` , AVG( `sentenceProduction`.`totalDuration` ) AS `avg` , COUNT( * ) AS `number of sentences` FROM `talker` , `sentence` , `sentenceProduction` WHERE `sentenceProduction`.`talker_id` = `talker`.`talker_id` AND `sentenceProduction`.`sentence_id` = `sentence`.`sentence_id` GROUP BY `talker`.`talker_id`

Page 29: RELATIONAL DATABASES - Northwestern University · 2013-01-30 · • Best practices for database design ... • Combines the talkers, sentence and sentenceProduction table and only

Extra stuff • MySQL + php allows for more complex queries when

necessary •  To try this on your own computer, download and install

XAMPP (http://www.apachefriends.org/en/xampp.html)