Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Databases More SQL. Design. Original Project Assignment Homework: Post proposal. Continue with planning.

Similar presentations


Presentation on theme: "Creating Databases More SQL. Design. Original Project Assignment Homework: Post proposal. Continue with planning."— Presentation transcript:

1 Creating Databases More SQL. Design. Original Project Assignment Homework: Post proposal. Continue with planning.

2 MySQL or most DBMS products can specify fields in records as FOREIGN KEY and do the work of checking. My examples do not use this but instead do the checking in the php.

3 LOCK and UNLOCK Most database applications are for use by multiple users. You don't want two or more users updating the same record –WHY NOT? Most DBMS products have ways to support lengthy transactions. POSTING OPPORTUNITY

4 Performance considerations The CREATE table statement can specify one or more fields to be INDEX. This forces the DBMS to build an index (!) for improved speed. Trade off: more space and slightly more time when records added but shorter times during SELECT operations. OPTIMIZE table. Assumption that tables become less efficient over time. Similar to disk defrag EXPLAIN SELECT gives information on how this SELECT works. May lead you to revising the statement OR defining an INDEX (use ALTER table) Other techniques…. Some DBMSs have stored procedures…

5 Recall songs, features, featuresinsongs

6 Multiple tables conditions SELECT from multiple tables (use commas) List the name of the song with the name of the tag feature. One row for each tagging… SELECT s.sname as a, f.fname as b FROM songs as s, featuresinsongs as t, features as f WHERE s.sid=t.sid AND t.fid=f.fid

7 Alternate Use INNER JOIN and ON Note: the, between tables is equivalent to INNER JOIN BUT The way the operation is done is different and probably more efficient: this way doesn't create as many records. The WHERE way creates records for each row in one table with ALL rows of other table and then removes the ones not matching the WHERE conditions!

8 INNER JOIN ON SELECT s.sname as a, f.fname as b FROM songs as s INNER JOIN featuresinsongs as t ON s.sid=t.sid INNER JOIN features as f ON t.fid=f.fid NOTE: can say simply JOIN and not INNER JOIN SELECT s.sname as a, f.fname as b FROM songs as s JOIN featuresinsongs as t ON s.sid=t.sid JOIN features as f ON t.fid=f.fid

9 Results in all cases This land is your land Folk This land is your landPolitical I told you soPolitical somethingPop something elseClassical

10 More SQL Assuming the simple student database (id, sname, department, gpa) Produce a list of departments, with number of students, average, maximum, and minimum gpa Produce a list of departments, number of students with gpa at least 2 (not on academic probation?) Produce a list of departments, number of students on academic probation, limit to the 5 with the most on academic probation

11 Students in clubs Assume the student table (sid, sname, dept, gpa) plus 2 more tables –clubs: clubid, clubname, clubdesc –clubmemberships: mid, sid, clubid Usual technique, similar to tags, ordered items.

12 Generate list of students in club named Hiking SELECT s.sname FROM students as s, clubs as c, clubmemberships as m WHERE c.clubname='Hiking' AND m.clubid=c.clubid AND s.sid=m.sid ALTERNATIVE (may need parentheses) SELECT s.sname FROM students as s JOIN clubmemberships as m ON m.sid = s.sid JOIN clubs as c WHERE clubname='Hiking' ON m.clubid=c.clubid

13 Left Join Matches rows of two tables using the ON condition, but if something on the LEFT does not have a match, generate a row with null values. Used in the quiz show to find questions that haven't been answered or asked recently. NOTE: can have SELECT within a SELECT

14 Students not in any club SELECT sname FROM SELECT * FROM students as s LEFT JOIN clubmemberships as m ON s.sid=m.sid WHERE m.sid=null

15 php to php One way to acquire and pass information from one php script to another is to use the query string. In the store application, html is produced that has tags with href=makeorder.php?3 for example, where 3 represents a product id.

16 Select product: <?php $query="Select * from catalog"; $result=mysql_db_query($DBname, $query, $link); while ($row=mysql_fetch_array($result)) { print (" <a href=makeorder.php"); print ("?p_id="); print($row['id']); print(">"); print($row['p_name']); print(" "); print(" <img src=\""); $picture=$row['picture']; print("$picture"); print("\" width='200'> "); } print (" ");

17 General principles for SQL You need to build the Select statement –pencil in hand, work in steps Decide the tables needed Decide on logical conditions for records contributing to the result –WHERE Decide on conditions connecting tables –JOIN ON

18 General principles, cont. The GROUP BY command combines / aggregates records based on common values, using aggregate operations such as COUNT, AVG, SUM,etc. If you want to maintain individual records but bunch them together, use ORDER Remember order high to low requires DESC

19 General principles, cont. For condition on which individual records to use: WHERE For condition on aggregated (GROUP BY) records: HAVING DISTINCT will extract [just] one from a table for the specified field

20 Operational definition … is what you call the definition of something that is used in a process. For example: –freshmen, sophomores, juniors, seniors –Dean's list, academic probation –???

21 Aside Last week, we had meeting for rising seniors I/we gave up on sending invitation to just that group and invited everyone. –We used email. –We believe some rising seniors did not attend!

22 Generate a list of names of clubs Just need one table SELECT clubname FROM clubs But what if I wanted clubs with members? need the clubs table and the clubmemberships table Join on cid

23 List of names of clubs with actual members SELECT DISTINCT clubname FROM clubs as c JOIN clubmemberships as m ON c.cid=m.cid could write c.clubname, but not needed since there is no ambiguity. Don't worry about this.

24 List names of clubs with at least 5 members SELECT c.cid, c.clubname, COUNT(*) as n FROM clubs as c JOIN clubmemberships as m ON c.cid=m.cid GROUP BY c.cid HAVING n>=5 This produces more information than requested. Could probably leave out the c.cid and instead GROUP by clubname. This would be better if there is a chance of ambiguity.

25 List names of any club with at least one freshmen SELECT DISTINCT clubname FROM clubs as c JOIN clubmemberships as m ON c.cid=m.cid JOIN students as s ON m.sid=s.sid WHERE s.credits <30 Creating a table using the clubmembership table with information added from other tables. Records only go into the table if the student's record has credits<30. Use DISTINCT to get the distinct club names. Don't need other information.

26 List all clubs by name with number of sophomores Need to use all 3 tables Need to use WHERE clause to get sophomores –Operational definition: sophomore means between 30 and 60 credits. Use LEFT JOIN to pick up clubs with no matches since for this example, I want clubs with no sophomores to show up! –Use COUNT(m.id) to NOT count any NULL fields!

27 [should check use of ( ) ] SELECT clubname, COUNT(m.mid) as num from CLUBS as c LEFT JOIN (clubmemberships as m JOIN students as s ON m.sid = s.sid WHERE s.credits BETWEEN 30 AND 60) ON c.cid=m.cid GROUP BY clubname

28 Possible result Young Dems40 Young Repubs 3 Origami10 Hiking 0

29 Generate list of students with the clubs each one belongs to… CurleyCircus Hiking PTV LarryCircus Origami MoeHiking

30 Planning Will need SQL and then php Need 3 tables (need club names and student names) Need to place all records for each student together, but not aggregated. Use ORDER BY

31 SQL SELECT s.sid, s.sname as a,c.clubname as b FROM students as s JOIN clubmemberships as m ON s.sid=m.sid JOIN clubs as c ON m.cid=c.cid ORDER BY s.sid s.sname Note: information just in the clubmembership table isn't part of the resultset BUT those records are the only ones represented in the resultset.

32 php … // assume $result is the result of query $curstudent=""; print(" Student Club "); while ($row=mysql_fetch_array($result)) { print(" "); if ($curstudent!=$row['a']) { $curstudent=$row['a']; print("$curstudent "); } else { print (" "); } print(" ".$row['b']." "); } print(" ");

33 Misc. Can order using multiple fields ….. ORDER BY lname, fname WHERE or HAVING condition can use –IN …. WHERE clubname IN ('Hiking', 'Origami') –BETWEEN … WHERE gpa BETWEEN 3 AND 4 ALSO can use BETWEEN for dates and times NOTE: other ways to do these. EXTRA CREDIT: look up & report on ROLLUP

34 Planning Planning (system design where design has broad meaning, not specifically the look) –comes first! –can change Document the structure of database using ER diagram and the potential tasks (processes) of the application using the DFD –ER: entities and relationships –recall DFD: agents, processes, data stores

35 Database table design Don't have same information in more than one table. For example: –The records in an orders table points to a customer table. A customer's address is not in each order.

36 Data flow diagram place to specify Processes: actions/tasks/operations of your application. Agents: Who is doing what with/to information. Data stores –This is an attempt to not be specific about how data kept. Don't get bogged down in details of the information right now.

37 Presentation What is the application? Show the information structure using an ER diagram. Show the processes/operations/tasks using a Data flow diagram. No storyboard (not yet) May show a sketch of one or more screens. Time for constructive comments from audience

38 Team work …. is not always smooth. That is part of the reason for the assignments. Do post names of everyone on the team. Everyone should understand everything! People do have different talents but EVERYONE can participate!

39 Comment Large[r] organizations have elaborate tools for project proposals. –proposals –communication –testing –documentation –deployment. Most work is incremental: fixing bugs and making enhancements.

40 Classwork/homework/classwork Start/continue: –ideas, teams, planning Next class: work session on planning Following class: 4/20: Present plans using diagrams


Download ppt "Creating Databases More SQL. Design. Original Project Assignment Homework: Post proposal. Continue with planning."

Similar presentations


Ads by Google