Example simpsons database

Slides:



Advertisements
Similar presentations
HTML TABLES EXPLAINED. What is a TABLE? The HTML table allows web designers to arrange & organize data -- text, images, hyperlinks, forms, form fields,
Advertisements

INTRODUCTORY Tutorial 8 Creating Data Tables. XP Objectives Contrast data tables with layout tables Create a table to display and organize data Provide.
Table (TABLE) Contains TABLE ROWS (TR) Contains TABLE DATA (TD) Data can contain anything Text Lists Other tables Pictures …
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.
Introducing Web Tables
HTML. Creating a Table Attributes: border: indicates the border type of the table Value: 0 (no border), 1, 2, etc. cols: indicates the number of columns.
1 CS428 Web Engineering Lecture 10 Images, Tables, Forms, Border-Radius (CSS – V)
INTRODUCTION TO WEB DEVELOPMENT AND HTML Lecture 06: Tables - Spring 2011.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 8 Key Concepts 1 Copyright © Terry Felke-Morris.
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.
CIS 1315 – Web Development for Educators CIS 1315 HTML Tutorial 5: Working with Tables.
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.
Appendix A Database Design CS Database design principles  database design: the act of deciding the schema for a database  database schema: a description.
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.
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 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.
Jozef Goetz, © Pearson Education Copyright (c) 2006 Prentice-Hall. All rights reserved.
Chapter 9 Table Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Basics of Web Design 1 Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Tutorial 5 Working with Tables and Columns
Web Development & Design Foundations with XHTML Chapter 8 Key Concepts.
Tutorial 5 Working with Tables and Columns
CSS Table Properties.
Html Tables Basic Table Markup. How Tables are Used For Data Display Tables were originally designed to display and organize tabular data (charts, statistics,
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.
1 TECH1001 Lecture 6 Electronic Publishing and Production 1 More About Tables.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 8 Key Concepts 1 Copyright © Terry Felke-Morris.
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.
Tables with XHTML Please use speaker notes for additional information!
CIS234A Lecture 8 Instructor Greg D’Andrea. Review Text Table contains only text, evenly spaced on the Web page in rows and columns uses only standard.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 8 Key Concepts 1 Copyright © Terry Felke-Morris.
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.
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.
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
Applying CSS to Tables Stylish Tables.
>> 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
H T M L A B E S X P I N D.
Using tables in HTML Goes in order with Examples at my site.
Principles of Web Design 5th Edition
Lesson 5: HTML Tables.
Lecture 20: Relational Databases and SQL
Lecture 21: Multi-table SQL Queries (Joins)
Lecture 22: Creating a Database and More joins
Presentation transcript:

Example simpsons database CS380

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

Cross product with JOIN SELECT column(s) FROM table1 JOIN table2; SQL SELECT * FROM students JOIN grades; SQL 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

Joining with ON clauses 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: 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

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

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

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

Giving names to tables SELECT name, course_id, grade FROM students s JOIN grades g ON s.id = g.student_id WHERE s.id = 123; SQL 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') 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

PHP MySQL functions name description mysql_connect connects to a database server mysql_select_db chooses which database on server to use (similar to SQL USE database; command) mysql_query performs a SQL query on the database mysql_real_escape_string encodes a value to make it safe for use in a query mysql_fetch_array, ... returns the query's next result row as an associative array mysql_close closes a connection to a database CS380

HTML tables: <table>, <tr>, <td> <tr><td>1,1</td><td>1,2 okay</td></tr> <tr><td>2,1 real wide</td><td>2,2</td></tr> </table> HTML 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 CS380

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

Styling tables <table { border: 2px solid black; caption-side: bottom; } tr { font-style: italic; } td { background-color: yellow; text-align: center; width: 30%; } CSS 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 CS380

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

Table headers, captions: <th>, <caption> <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr> <tr><td colspan="2">1,1-1,2</td> <td rowspan="3">1,3-3,3</td></tr> <tr><td>2,1</td><td>2,2</td></tr> <tr><td>3,1</td><td>3,2</td></tr> </table> HTML colspan makes a cell occupy multiple columns; rowspan multiple rows text-align and vertical-align control where the text appears within a cell CS380

Table headers, captions: <th>, <caption> <col class="urgent" /> <colgroup class="highlight" span="2"></colgroup> <tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr> <tr><td>1,1</td><td>1,2</td><td>1,3</td></tr> <tr><td>2,1</td><td>2,2</td><td>2,3</td></tr> </table> HTML 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) CS380

Other MySQL PHP functions name description mysql_num_rows returns number of rows matched by the query mysql_num_fields returns number of columns per result in the query mysql_list_dbs returns a list of databases on this server mysql_list_tables returns a list of tables in current database mysql_list_fields returns a list of fields in the current data complete list CS380

Multi-way join 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 Course from courses and grades tables have to be the same Student id from grades and students tables have to be the same Name from students table has to be Bart, grade from grades table has to be B- grade column sorts alphabetically, so grades better than B- are ones <= it CS380

A suboptimal query What courses have been taken by both Bart and Lisa? 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 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. CS380

Improved query What courses have been taken by both Bart and Lisa? 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

Practice queries What are the names of all teachers Bart has had? How many total students has Ms. Krabappel taught, and what are their names? CS380

Practice queries What are the names of all teachers Bart has had? 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 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 CS380

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!!

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

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

IMDb table relationships / ids CS380