Appendix A Database Design CS380 1. Database design principles  database design: the act of deciding the schema for a database  database schema: a description.

Slides:



Advertisements
Similar presentations
Table (TABLE) Contains TABLE ROWS (TR) Contains TABLE DATA (TD) Data can contain anything Text Lists Other tables Pictures …
Advertisements

Cos 125 Day 22. Agenda Assignment 6 Not corrected Waiting for tune-ins Assignment 7 is posted Assignment 7 Due April 2PM Left to do 1 Assignments.
Cos 125 Day 24. Agenda All students meeting 7 Pm Monday, April 19 UMS Strategic Plan Assignment #7 Posted Due April 20 Two (one?) more to go Quiz Four.
Introduction to Databases CIS 5.2. Where would you find info about yourself stored in a computer? College Physician’s office Library Grocery Store Dentist’s.
Page 1 ISMT E-120 Introduction to Microsoft Access & Relational Databases The Influence of Software and Hardware Technologies on Business Productivity.
Tutorial #8 – Creating Data Tables. Tutorial #6 Review – Layouts Two Column Fixed Width, Three Column Fixed Width Class VS. ID Container Universal Selector.
CIS 1310 – HTML & CSS 8 Tables. CIS 1310 – HTML & CSS Learning Outcomes  Create a Table  Apply Attributes to Format Tables  Increase the Accessibility.
Lecture 16: SQL and HTML tables
Database Basics CS Why use a database?  powerful: can search it, filter data, combine data from multiple sources  fast: can search/filter a database.
1 The Structure of a Web Table beginning of the table structure first row of three in the table end of the table structure table cells You do not need.
1 Relational Databases. 2 Find Databases here… 3 And here…
CHAPTER 11 Tables. How Are Tables Used Data Display  Very tidy and very useful Better Text Alignment  Putting text in tables allows you to format indents.
INTRODUCTORY Tutorial 7 Creating Tables. XP New Perspectives on Blended HTML, XHTML, and CSS2 Objectives Discern the difference between data tables and.
Tutorial 5 Working with Web Tables. XP Objectives Explore the structure of a Web table Create headings and cells in a table Create cells that span multiple.
Principles of Web Design 6 th Edition Chapter 10 – Data Tables.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
CHAPTER 10 Formatting Tables and Forms. Using Tables the Right Way  HTML and XHTML have a LOT of tags dedicated to building a table  If you have only.
PHP meets MySQL.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Example simpsons database
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
Chapter 9 Table Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Tutorial 5 Working with Tables and Columns
Tutorial 5 Working with Tables and Columns
CSS Table Properties.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
Access Manual 2 By Dhawala Kovuri Elham S.Khorasani Ismail Guneydas.
CSE 154 LECTURE 14: MULTI-TABLE SQL QUERIES (JOINS)
Tutorial 5: Tables Session OBJECTIVES Marking row groups Marking column groups Setting the table frame Specifying the table’s internal gridlines.
Tutorial 5 Working with Web Tables. XP Objectives Explore the structure of a Web table Create headings and cells in a table Create cells that span multiple.
Lesson 7. Tables Table Tags and Attributes Tables HTML tables are used in two main ways: 1.To organize tabular data in a familiar spreadsheet-like way.
McGraw-Hill/Irwin The O’Leary Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Access 2002 Lab 3 Analyzing Tables and Creating.
Access Review. Access Access is a database application A database is a collection of records and files organized for a particular purpose Access supports.
Chapter 5 Creating Page Templates. Principles of Web Design 2nd Ed. Chapter 5 2 Principles of Web Design Chapter 5 Objectives Understand table basics.
HTML Tables The HTML table model allows authors to arrange data - text, preformatted text, images, links, forms, form fields, other tables, etc. - into.
26 HTML Tables … surround table … surround each row … surround each cell … like, but bold and centered by default (for table headings) … table title No.
Chapter 5 Working with Tables Principles of Web Design, 4 th Edition.
Updated on: September 4, 2010 CIS67 Foundations for Creating Web Pages Professor Al Fichera.
How To Create HTML Tables. Table Structure General HTML code for a Web Table: table cells table cells.
Assistant Professor,UCER Naini,Allahabad
Chapter 9 Table Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Copyright © 2012 Certification Partners, LLC -- All Rights Reserved Lesson 5: HTML Tables.
Tables creating a table within a web page. What makes up a table? Columns Rows.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
Chapter 1. Getting Started IST 210: Organization of Data IST2101.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Tutorial 5 Working with Web Tables. New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 2 Objectives Learn and Apply the structure of.
CSS for Styling By Jinzhu Gao.
CSE 154 Lecture 17: HTML tables.
Organizing Content with Lists and Tables
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
>> HTML: Tables.
Loops BIS1523 – Lecture 10.
HTML Tables CS 1150 Spring 2017.
Chapter 7 Tables.
Lecture 25: SQL and HTML tables
Lecture 22: Relational Databases and SQL
Implementing Tables to Hold Data in HTML
Lecture 23: Multi-table SQL Queries (Joins)
Lecture 24: Creating a Database and More joins
Using tables in HTML Goes in order with Examples at my site.
Principles of Web Design 5th Edition
Lecture 21: Multi-table SQL Queries (Joins)
Lecture 22: Creating a Database and More joins
Presentation transcript:

Appendix A Database Design CS380 1

Database design principles  database design: the act of deciding the schema for a database  database schema: a description of what tables a database should have, what columns each table should contain, which columns' values must be unique, etc. CS380 2

Database design principles  some database design principles:  keep it simple, stupid (KISS)  provide an identifier by which any row can be uniquely fetched  eliminate redundancy, especially of lengthy data (strings)  integers are smaller than strings and better to repeat  integers can be compared/searched more quickly than strings, real numbers CS380 3

First database design  what's good and bad about this design?  good: simple (one table), can see all data in one place  bad: redundancy (name, , course repeated frequently)  bad: most searches (e.g. find a student's courses) will have to rely on string comparisons  bad: there is no single column whose value will be unique in each row 4

Second database design  splitting data into multiple tables avoids redundancy  normalizing: splitting tables to improve structure and remove redundancy / anomalies  normalized tables are often linked by unique integer IDs 5

Second database design  primary key: a table column guaranteed to be unique for each record  record in Student table with id of 888 is Lisa Simpson's student info 6 CS380

Second database design  records of one table may be associated with record(s) in another table  foreign key: a column in table A that stores a value of a primary key from another table B  records in Grade table with student_id of 888 are Lisa Simpson's course grades 7

Design question  suppose we want to keep track of the teachers who teach each course  e.g. Ms. Krabappel always teaches CSE 142 and INFO 100  e.g. Ms. Hoover always teaches CSE 143  e.g. Mr. Stepp always teaches CSE 190M  what tables and/or columns should we add to the database? 8

Design answer  add a teachers table containing information about instructors  link this to courses by teacher IDs  why not just skip the teachers table and put the teacher's name as a column in courses?  repeated teacher names are redundant and large in size 9

Multi-table Queries 10 CS380

Example simpsons database CS380 11

Querying multi-table databases When we have larger datasets spread across multiple tables, we need queries that can answer high-level questions such as:  What courses has Bart taken and gotten a B- or better?  What courses have been taken by both Bart and Lisa?  Who are all the teachers Bart has had?  How many total students has Ms. Krabappel taught, and what are their names? To do this, we'll have to join data from several tables in our SQL queries. CS380 12

Cross product with JOIN  cross product or Cartesian product: combines each row of first table with each row of second  produces M * N rows, where table 1 has M rows and table 2 has N  problem: produces too much irrelevant/meaningless data 13 SELECT column(s) FROM table1 JOIN table2; SQL SELECT * FROM students JOIN grades; SQL

Joining with ON clauses  join : a relational database operation that combines records from two or more tables if they satisfy certain conditions  the ON clause specifies which records from each table are matched  often the rows are linked by their key columns 14 SELECT column(s) FROM table1 JOIN table2 ON condition(s)... JOIN tableN ON condition(s); SQL SELECT * FROM students JOIN grades ON id = student_id; SQL

Join example  table.column can be used to disambiguate column names: 15 SELECT * FROM students JOIN grades ON id = student_id; SQL SELECT * FROM students JOIN grades ON students.id = grades.student_id; SQL CS380

Filtering columns in a join  if a column exists in multiple tables, it may be written as table.column 16 SELECT name, course_id, grade FROM students JOIN grades ON students.id = student_id; SQL CS380

Giving names to tables  can give names to tables, like a variable name in Java  to specify all columns from a table, write table.* 17 SELECT name, g.* FROM students s JOIN grades g ON s.id = g.student_id; SQL CS380

Giving names to tables  FROM / JOIN glue the proper tables together, and WHERE filters the results  what goes in the ON clause, and what goes in WHERE ?  ON directly links columns of the joined tables  WHERE sets additional constraints such as particular values (123, 'Bart') 18 SELECT name, course_id, grade FROM students s JOIN grades g ON s.id = g.student_id WHERE s.id = 123; SQL CS380

More simple join practice  Show the names of the classes that Mr. Krabappel is teaching  Show the classes that Milhouse is taking  Print the results on an html table CS380 19

PHP MySQL functions CS380 20

HTML tables:,,  table defines the overall table, tr each row, and td each cell's data  tables are useful for displaying large row/column data sets  NOTE: tables are sometimes used by novices for web page layout, but this is not proper semantic HTML and should be avoided CS ,1 1,2 okay 2,1 real wide 2,2 HTML

Table headers, captions:,  th cells in a row are considered headers; by default, they appear bold  a caption at the start of the table labels its meaning CS My important data Column 1 Column 2 1,1 1,2 okay 2,1 real wide 2,2 HTML

Styling tables  all standard CSS styles can be applied to a table, row, or cell  table specific CSS properties:  border-collapse, border-spacing, caption-side, empty-cells, table-layout CS <table { border: 2px solid black; caption-side: bottom; } tr { font-style: italic; } td { background-color: yellow; text-align: center; width: 30%; } CSS

The border-collapse property  by default, the overall table has a separate border from each cell inside  the border-collapse property merges these borders into one CS table, td, th { border: 2px solid black; } table { border-collapse: collapse; } CSS

Table headers, captions:,  colspan makes a cell occupy multiple columns; rowspan multiple rows  text-align and vertical-align control where the text appears within a cell CS Column 1 Column 2 Column 3 1,1-1,2 1,3-3,3 2,1 2,2 3,1 3,2 HTML

Table headers, captions:,  col tag can be used to define styles that apply to an entire column (self-closing)  colgroup tag applies a style to a group of columns (NOT self-closing) CS Column 1 Column 2 Column 3 1,1 1,2 1,3 2,1 2,2 2,3 HTML

Other MySQL PHP functions CS380 27

Multi-way join  grade column sorts alphabetically, so grades better than B- are ones <= it 28 SELECT c.name FROM courses c JOIN grades g ON g.course_id = c.id JOIN students bart ON g.student_id = bart.id WHERE bart.name = 'Bart' AND g.grade <= 'B-'; SQL CS380

A suboptimal query  problem: requires us to know Bart/Lisa's Student IDs, and only spits back course IDs, not names.  Write a version of this query that gets us the course names, and only requires us to know Bart/Lisa's names, not their IDs. 29 SELECT bart.course_id FROM grades bart JOIN grades lisa ON lisa.course_id = bart.course_id WHERE bart.student_id = 123 AND lisa.student_id = 888; SQL CS380  What courses have been taken by both Bart and Lisa?

Improved query 30 SELECT DISTINCT c.name FROM courses c JOIN grades g1 ON g1.course_id = c.id JOIN students bart ON g1.student_id = bart.id JOIN grades g2 ON g2.course_id = c.id JOIN students lisa ON g2.student_id = lisa.id WHERE bart.name = 'Bart' AND lisa.name = 'Lisa'; SQL CS380  What courses have been taken by both Bart and Lisa?

Practice queries 31 SELECT DISTINCT t.name FROM teachers t JOIN courses c ON c.teacher_id = t.id JOIN grades g ON g.course_id = c.id JOIN students s ON s.id = g.student_id WHERE s.name = 'Bart'; SQL CS380  What are the names of all teachers Bart has had?  How many total students has Ms. Krabappel taught, and what are their names? SELECT DISTINCT s.name FROM students s JOIN grades g ON s.id = g.student_id JOIN courses c ON g.course_id = c.id JOIN teachers t ON t.id = c.teacher_id WHERE t.name = 'Krabappel'; SQL

Designing a query  Figure out the proper SQL queries in the following way:  Which table(s) contain the critical data? (FROM)  Which columns do I need in the result set? (SELECT)  How are tables connected (JOIN) and values filtered (WHERE)?  Test on a small data set (imdb_small).  Confirm on the real data set (imdb).  Try out the queries first in the MySQL console.  Write the PHP code to run those same queries.  Make sure to check for SQL errors at every step!! 32

Example imdb database  other tables:  directors (id, first_name, last_name)  movies_directors (director_id, movie_id)  movies_genres (movie_id, genre) CS380 33

IMDb query example 34 select * from actors where first_name like '%mick%'; SQL CS380

IMDb table relationships / ids CS380 35