creating databases

35
Creating Databases SELECT. UPDATE. Demonstrate projects. Classwork / Homework: Prepare to choose teams & projects.

Upload: clint

Post on 14-Jan-2016

37 views

Category:

Documents


1 download

DESCRIPTION

Creating Databases. SELECT. UPDATE. Demonstrate projects. Classwork / Homework: Prepare for review for midterm. SELECT. … what if you want only DISTINCT values? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Creating Databases

Creating Databases

SELECT. UPDATE.

Demonstrate projects.

Classwork / Homework: Prepare to choose teams & projects.

Page 2: Creating Databases

Warning

• Changes in php from last time.– THIS IS INEVITABLE and is mainly a good

thing.

• I think I made required changes, but only did modest testing.– AND I did all the testing. Always involve other

people to do testing!

Page 3: Creating Databases

SELECT

• … what if you want only DISTINCT values?

• For example, the trivia quiz starts with a form to choose the category. People (site administrators) adding questions can put in any category.

• http://socialsoftware.purchase.edu/jeanine.meyer/quiz/choosecategory.php

Page 4: Creating Databases

from php code choosecategory.php

<h1> Welcome to the Quiz </h1> <br><h3> Sign in and select a category for your question </h3><form action="askquestion.php" method=post>

<p>Name <input type=text name='player' size=30<?php…

$query="SELECT DISTINCT category FROM questions";$categories = mysqli_query($link,$query);while ($row=mysqli_fetch_array($categories)){ $cat=$row['category']; print ("<option value='$cat'>$cat</option><br>\n");}

Page 5: Creating Databases

SQL join

• SELECT statement against a table made up by JOINing tables together on identical fields.

• Different types of JOIN:– JOIN (same as INNER JOIN)– LEFT JOIN– RIGHT JOIN– FULL JOINhttp://www.w3schools.com/sql/sql_join.asp

Page 6: Creating Databases

LEFT, RIGHT, FULL JOINs

• These provide ways to pick up missing records.

• CHECK OUT THE w3schools and other tutorials!

• Will show example from the quiz show– Task: find questions that player has NOT

answered correctly and has NOT been asked that day.

Page 7: Creating Databases

Two steps

• Create a temporary table of all the questions asked a particular player and answered correctly OR asked today.– need to specify the contents of the table. In

this case, one field of INT datatype

• If there have been past questions, do a SELECT using LEFT JOIN to extract any question NOT present in the past array. Otherwise, do a simple SELECT

Page 8: Creating Databases

$query="CREATE temporary TABLE past (item_id INT)";

$query.= " SELECT question_id FROM history WHERE (player_id='".$player_id;

$query.= "' AND (whenplayed='".$today."' OR correct))";

$result=mysqli_query($link,$query);$query="SELECT * FROM past";$result = mysqli_query($link,$query);$Num_past = mysqli_num_rows($result);

1st step: create temporary table, past

Page 9: Creating Databases

2nd step

if ($Num_past>0) { $sel = "SELECT questions.question_id, question,

answerpattern, value from questions"; $sel =$sel . " LEFT JOIN past ON questions.question_id =

past.question_id WHERE "; $sel = $sel . " category='" . $pickedcategory . "' AND

past.question_id IS NULL"; }else { $sel="SELECT question_id, question, answerpattern,

value from questions "; $sel= $sel . " WHERE category= '" . $pickedcategory. "'"; }

Page 10: Creating Databases

SELECT Conditions

• WHERE – Sets condition on individual records

• with a JOIN – the ON specifying what field to do the JOIN

on, generally a foreign key equal to a primary key

• After aggregating using GROUP– HAVING sets a condition on grouped data

Page 11: Creating Databases

Conditions

• Remember: the single equal sign is the operator for equality!

• Other comparisons: >, <, >=, <=

• LOGIC: AND, OR, NOT

• REGEX for regular expressions

• LIKE: another way to specify a pattern

Page 12: Creating Databases

Conditions

• Can select using set of values

• SELECT * FROM questions WHERE category IN (‘trivia’,’misc’,’silly’)

• See also BETWEEN

Page 13: Creating Databases

Recall

• 4 tables

movies

mid

mname

mdate

people

pid

pname

roles

rid

mid

pid

role (director,actor,etc.)

nominations

aid

rid

category

win

In most cases, peoplehave only 1 role. Affleck is an exception.In most cases, awards are for 1 role. Producingis an exception. Some roles are notnominated for anything,hence the 0.

0

Page 14: Creating Databases

Tasks• List all movies by name, ordered by date

– SELECT mname, mdate FROM movies ORDER BY mdate

• List all people by name with roles in a given movie, named $moviename (this is mixture of php and straight SQL)– SELECT p.pname,m.mname,r.role FROM

movies as m JOIN roles as r ON m.mid = r.mid JOIN people as p ON p.pid = r.ridWHERE m.mname='$moviename'

Page 15: Creating Databases

next task: reuse JOIN clauses

• List all directors (by name), with movie (by name) ordered by movie name– SELECT p.pname,m.mname FROM movies

as m JOIN roles as r ON m.mid = r.mid JOIN people as p ON p.pid = r.ridWHERE r.role='Director' ORDER BY m.mname

Page 16: Creating Databases

• List all movies by name in which someone was nominated for Best Lead Actor– SELECT m.mname FROM movies as m

JOIN roles as r ON m.mid = r.mid JOIN people as p ON p.pid = r.ridJOIN nominations as n ON n.rid=r.ridWHERE n.award='Best Lead Actor'

Page 17: Creating Databases

next task, again reuse JOIN clauses plus start of WHERE

• List all movies by name in which someone was nominated for an acting category. Count number.– Best Lead Actor, Best Lead Actress, Best Supporting

Actor, Best Supporting Actress– SELECT m.mname, count(*) FROM movies as m

JOIN roles as r ON m.mid = r.mid JOIN people as p ON p.pid = r.ridJOIN nominations as n ON n.rid=r.ridWHERE n.award IN ('Best Lead Actor, Best Lead Actress, Best Supporting Actor, Best Supporting Actress) ORDER BY m.mname GROUP BY m.mname

Page 18: Creating Databases

More

• List movie name, person name, nominated award ordered by movie name– SELECT m.mname, p.pname, n.award

FROM movies as m JOIN roles as r ON m.mid = r.mid JOIN people as p ON p.pid = r.ridJOIN nominations as n ON n.rid=r.rid ORDER BY m.mname

Page 19: Creating Databases

Next

• List winners: movie name, person name, award– SELECT m.mname, p.pname, n.award

FROM movies as m JOIN roles as r ON m.mid = r.mid JOIN people as p ON p.pid = r.ridJOIN nominations as n ON n.rid=r.rid ORDER BY m.mname WHERE n.win='true'

Page 20: Creating Databases

• List movie name, number of people nominated, ordered from high to low– SELECT m.mname, count(*) FROM movies as m

JOIN roles as r ON m.mid = r.mid JOIN people as p ON p.pid = r.ridJOIN nominations as n ON n.rid=r.rid ORDER BY m.mname GROUP BY m.mname

– EXTRA CREDIT: figure out how not to count multiples for awards that have multiples (such as producer, technical awards)

Page 21: Creating Databases

Research

• Some questions require consideration of absent records– What movies were nominated (had people

nominated) for Best Movie but not Best Director

– ?

• Look up and study examples of LEFT JOINS

Page 22: Creating Databases

UPDATE

• Can update a single record or a set of records.

• UPDATE questions SET text = ‘$ntext’ WHERE ques_id=‘$qid’– Assumes table names questions with fields

text and ques_id and php variables $ntext and $qid set previously

Page 23: Creating Databases

UPDATE

• Raise all the prices 10%– UPDATE products SET price=1.10*price

• Raise the prices that are over 100 by 5%– UPDATE products SET price=1.05*price

WHERE price > 100

Page 24: Creating Databases

Projects• Recall Geolocation / Google maps email.

http://socialsoftware.purchase.edu/jeanine.meyer/emailing/geolocationkmemail.html

• Quizhttp://faculty.purchase.edu/jeanine.meyer/html5/mapmediaquiz.html

• Does not use database. Enhancement is to add database for locations

Page 25: Creating Databases

Projects

• Bookmarks, with password system for finders

– http://socialsoftware.purchase.edu/jeanine.meyer/research/addsite.html

– http://socialsoftware.purchase.edu/jeanine.meyer/research/showsitesbycategory1.php

Page 27: Creating Databases

Projects

• Trivia quizhttp://socialsoftware.purchase.edu/jeanine.meyer/quiz/choosecategory.php http://socialsoftware.purchase.edu/jeanine.meyer/quiz/inputquestions.php – edit feature?– change scoring?– improve interface?

Page 28: Creating Databases

Projects

• Origami storehttp://socialsoftware.purchase.edu/jeanine.meyer/orders/orderproduct.php

http://socialsoftware.purchase.edu/jeanine.meyer/orders/inputproducts.php need file upload to upload pictures

– general improvement– scale up

Page 30: Creating Databases

Stories

• http://socialsoftware.purchase.edu/jeanine.meyer/stories/tellStory.php

• http://socialsoftware.purchase.edu/jeanine.meyer/stories/enterscene.html

• http://socialsoftware.purchase.edu/jeanine.meyer/stories/addscene.php

• http://socialsoftware.purchase.edu/jeanine.meyer/stories/showAllScenesforediting.php

Page 31: Creating Databases

Citations

• http://socialsoftware.purchase.edu/jeanine.meyer/citations/drawlinksAsArrows.php

• http://socialsoftware.purchase.edu/jeanine.meyer/citations/inputjournals.php

• http://socialsoftware.purchase.edu/jeanine.meyer/citations/inputarticles.php

• http://socialsoftware.purchase.edu/jeanine.meyer/citations/addlink1.php

Page 32: Creating Databases

Assignment

• Team project to present and

• Enhance one of these projects

• There will be an assignment to build database project totally on your own

Page 33: Creating Databases

Projects• student database• trivia quiz• book marks• songs• origami store• Stories

• Google maps media portal (or quiz)– Note: this does NOT have a database at all! So obvious

enhancement is to make use of database, possibly with table for player or ???

• Late addition: citations (needs updating)

Page 34: Creating Databases

Preview (after midterm)

• Self assign or I assign teams (4-5)

• Teams will pick projects– resolve conflicts

Page 35: Creating Databases

Homework

• Prepare to form teams AND choose application.

• Working with existing code is…always a challenge but more common than starting from nothing.