1 / 33

Creating Databases

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

clint
Download Presentation

Creating Databases

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating Databases SELECT. UPDATE. Demonstrate projects. Classwork / Homework: Prepare for review for midterm

  2. 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

  3. 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 = mysql_query($query); while ($row=mysql_fetch_array($categories)) { $cat=$row['category']; print ("<option value='$cat'>$cat</option><br>\n"); }

  4. 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 JOIN http://www.w3schools.com/sql/sql_join.asp

  5. 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.

  6. 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

  7. 1st step: create temporary table, past $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=mysql_query($query); $query="SELECT * FROM past"; $result = mysql_db_query($DBname,$query,$link); $Num_past = mysql_num_rows($result);

  8. 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. "'"; }

  9. 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

  10. 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

  11. Conditions Can select using set of values SELECT * FROM questions WHERE category IN (‘trivia’,’misc’,’silly’) See also BETWEEN

  12. In most cases, people have only 1 role. Affleck is an exception. In most cases, awards are for 1 role. Producing is an exception. Some roles are not nominated for anything, hence the 0. Recall • 4 tables movies mid mname mdate people pid pname … nominations aid rid category win … roles rid mid pid role (director,actor,etc.) 0

  13. 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'

  14. 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

  15. 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'

  16. 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

  17. 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

  18. 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'

  19. 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)

  20. 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

  21. 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

  22. 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

  23. Projects • [Geolocation / Google maps email.Google Maps portal]http://socialsoftware.purchase.edu/jeanine.meyer/emailing/geolocationkmemail.html andhttp://faculty.purchase.edu/jeanine.meyer/html5/mapvideos.html • add database?

  24. 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

  25. Projects • Student departmentshttp://socialsoftware.purchase.edu/jeanine.meyer/studentexample/showstudents.phphttp://socialsoftware.purchase.edu/jeanine.meyer/studentexample/showdepartments.phphttp://socialsoftware.purchase.edu/jeanine.meyer/studentexample/enterstudent.html

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

  27. Projects • Origami storehttp://socialsoftware.purchase.edu/jeanine.meyer/orders/orderproduct.phphttp://socialsoftware.purchase.edu/jeanine.meyer/orders/inputproducts.phpneed file upload to upload pictures • general improvement • scale up

  28. Projects • Store names and find similar songshttp://socialsoftware.purchase.edu/jeanine.meyer/example/findothers1.phphttp://socialsoftware.purchase.edu/jeanine.meyer/example/addsong.htmlhttp://socialsoftware.purchase.edu/jeanine.meyer/example/addfeature.htmlhttp://socialsoftware.purchase.edu/jeanine.meyer/example/addfeaturestosong1.php

  29. 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

  30. Assignment • Team project to present and • Enhance one of these projects • There will be an assignment to build database project totally on your own

  31. 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 ???

  32. Preview (after midterm) • I will assign teams (4-5) • Teams will pick projects • resolve conflicts

  33. Homework • Study SQL SELECT examples • Study charts, notes, guide for midterm • Prepare for review day

More Related