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

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

Transcript of Creating Databases More SQL. Design. Original Project Assignment Homework: Post proposal. Continue...

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

Creating Databases

More SQL. Design.Original Project Assignment

Homework: Post proposal. Continue with planning.

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

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.

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

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

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

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…

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

Recall

• songs, features, featuresinsongs

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

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

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

Alternate

• Use INNER JOIN and ON• Note: the , between tables is equivalent to

INNER JOINBUT• 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!

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

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

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

Results in all cases

This land is your land Folk

This land is your land Political

I told you so Political

something Pop

something else Classical

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

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

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

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.

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

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

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

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

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

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

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

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 <a> tags with href=makeorder.php?3

for example, where 3 represents a product id.

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

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

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

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

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

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

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

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

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

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– ???

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

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!

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

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

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

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.

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

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.

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

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.

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

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!

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

[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

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

Possible result

• Young Dems 40

• Young Repubs 3

• Origami 10

• Hiking 0

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

Generate list of students with the clubs each one belongs to…

Curley Circus

Hiking

PTV

Larry Circus

Origami

Moe Hiking

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

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

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

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.

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

php… // assume $result is the result of query$curstudent="";print("<table

border='1'><tr><th>Student</th><th>Club</th></tr>");while ($row=mysql_fetch_array($result)) {

print("<tr><td>"); if ($curstudent!=$row['a']) {

$curstudent=$row['a'];print("$curstudent</td>"); }

else {print ("&nbsp;</td>");}

print("<td>".$row['b']."</td></tr>");}print("</table>");

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

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

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

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

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

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.

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

Data flow diagramplace 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.

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

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

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

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!

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

Comment

• Large[r] organizations have elaborate tools for project proposals.– proposals– communication– testing– documentation– deployment.

• Most work is incremental: fixing bugs and making enhancements.

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

Classwork/homework/classwork

• Start/continue:– ideas, teams, planning

• Next class: work session on planning

• Following class: 4/20: Present plans using diagrams