+ All Categories
Home > Documents > + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+ SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

Date post: 22-Dec-2015
Category:
Upload: sydney-dawson
View: 215 times
Download: 0 times
Share this document with a friend
29
+ SQL, HTML, PHP Week 10
Transcript
Page 1: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+

SQL, HTML, PHP

Week 10

Page 2: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Review and Questions Topics from last lecture

Terminology

Questions?

Page 3: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Objectives

SQL Review Auto Increment Group By Joins

HTML / PhP Forms Drop Down List Action Scripts – Insert

Page 4: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+

SQL

Page 5: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Auto Increment

Key Word for MS SQL – Identity

Can specify at time of table creation – The easiest CREATE TABLE PlayerDetails(

PlayerID INT Primary Key Identity,

PlayerFirstName Char(30) Not Null,

);

Or alter tables to include Identity

Page 6: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Happy Valley Golf

Page 7: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Altering Player ID to include

-- PlayerID

Alter Table PlayerHandicapdrop constraint PLAYERID_FK;

Alter Table Player_Roundsdrop constraint PLAYERID2_FK;

Alter Table PlayerDetailsdrop constraint PLAYERID_PK;

Alter Table PlayerDetailsDrop column PlayerId;

Alter Table PlayerDetailsAdd PlayerID int identity;

Alter Table PlayerDetailsAdd constraint pk_PlayerIDprimary key(PlayerId);

Alter Table PlayerHandicapAdd Constraint PLAYERID_FKForeign Key (PlayerId)References PlayerDetails(PlayerId);

Alter Table Player_RoundsAdd Constraint PLAYERID2_FKForeign Key (PlayerId)References PlayerDetails(PlayerId);

Page 8: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Group By

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

How many rounds of Golf have been played?

How many rounds of Golf have been played at each golf course? (Just need the id for the course)

Page 9: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Group By

How many rounds of Golf have been played? SELECT Count(RoundId) AS NumberofRounds

FROM GolfRounds;

How many rounds of Golf have been played at each golf course? (just need id for the course)

SELECT CourseID, Count(RoundId) AS NumberofRounds

FROM GolfRounds

GROUP BY CourseID;

Page 10: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Having

Allows for the results to be displayed for only a selection of the results of the function (count, average, etc).

How many multiple rounds of Golf have been played at each golf course (display results for only courses that have more than 1 round)? (just need id for the course)

Page 11: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Having

How many multiple rounds of Golf have been played at each golf course (display results for only courses that have more than 1 round)? (just need id for the course)

SELECT CourseID, Count(RoundId) AS NumberofRounds

FROM GolfRounds

GROUP BY CourseID;

HAVING COUNT(RoundId) > 1;

Page 12: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+One Step Farther

How many rounds of Golf have been played at each golf course and what golf course have they been played at (name of the course)?

SELECT CourseName, Count(RoundId) AS NumberofRounds

FROM GolfRounds AS GR , GolfCourses AS GC

WHERE GR.CourseID = GC.CourseID

GROUP BY GR.CourseID, CourseName;

Page 13: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Join

Needed when the results displayed come from multiple tables. If the results to display come from one table you can use a

subquery

Page 14: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Inner Joins

Returns the rows when there is at least one match in both tables. SELECT CourseName, Count(RoundId)

FROM GolfRounds AS GR , GolfCourses AS GC

WHERE GR.CourseID = GC.CourseID

GROUP BY GR.CourseID, CourseName;

Page 15: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Join … On

Will give the same results as the join query on the previous slide

SELECT CourseName, Count(RoundId)

FROM GolfRounds AS GR JOIN GolfCourses AS GC

ON GR.CourseID = GC.CourseID

GROUP BY GR.CourseID, CourseName;

Page 16: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Outer Join

Returns all rows from both the participating tables which satisfy the join condition along with rows which do not satisfy the join condition.

How many rounds of Golf have been played at each golf course and what golf course have they been played at (name of the course)? Display all golf courses even if there has been no rounds played.

SELECT CourseName, Count(RoundID)

FROM GolfRounds AS GR RIGHT JOIN GolfCourses AS GC

ON GR.CourseID = GC.CourseID

GROUP BY GR.CourseID, CourseName;

Page 17: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Outer Join

Can use left Join as well. SELECT CourseName, Count(RoundID)

FROM GolfCourses AS GC LEFT JOIN GolfRounds AS GR

ON GR.CourseID = GC.CourseID

GROUP BY GR.CourseID, CourseName;

Page 18: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Multiple Joins

You can use multiple tables together, not just two.

Show player’s names along with their average hole score and their handicap, for all players that have a handicap and have recorded round scores.

SELECT PlayerFirstName, PlayerLastName, Avg(HoleScore) AS AvgHoleScore, Avg(HandicapScore) AS AvgHandicapScore

FROM PlayerDetails AS PD, Player_Rounds AS PR, PlayerHandicap AS PH

WHERE PD.PlayerID = PR.PlayerIDAND PR.PlayerID = PH.PlayerIDGROUP BY PlayerLastName, PlayerFirstName

Page 19: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Multiple Joins .. Using JOIN…. ON

Show all player’s names along with their average hole score and their handicap

SELECT PlayerFirstName, PlayerLastName, Avg(HoleScore) AS AvgHoleScore, Avg(HandicapScore) AS AvgHandicapScore

FROM (PlayerDetails AS PD JOIN Player_Rounds AS PR ON PD.PlayerID = PR.PlayerID) JOIN

PlayerHandicap AS PH ON PR.PlayerID = PH.PlayerID

GROUP BY PlayerLastName, PlayerFirstName

Page 20: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Multiple Joins – Outer Join

Show all player’s names along with their average hole score and their handicap

SELECT PlayerFirstName, PlayerLastName, Avg(HoleScore) AS AvgHoleScore, Avg(HandicapScore) AS AvgHandicapScore

FROM (PlayerDetails AS PD LEFT JOIN Player_Rounds AS PR ON PD.PlayerID = PR.PlayerID)

LEFT JOIN PlayerHandicap AS PH ON PR.PlayerID = PH.PlayerID

GROUP BY PlayerLastName, PlayerFirstName

Page 21: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Practice

1.) Display each golf courses name, the average yards and par for holes at each course.

2.) Display each golf courses name, the average yards and par for holes at each course, and the number of holes played at the course. (Include all courses)

3.) Display each golf courses name, the average yards and par for holes at each course, and the number of rounds played at the course. (Include all courses)

Page 22: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Question 1

Display each golf courses name, the average yards and par for holes at each course.

SELECT CourseName, Avg(YardsForHole) AS AvgYardsForHole, Avg(ParForHole) AS ParForHole

FROM GolfCourses AS GC LEFT JOIN CourseHoleDetails AS CHD

ON GC.CourseID = CHD.CourseID

GROUP BY GC.CourseID, CourseName

Page 23: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Question 2

Display each golf courses name, the average yards and par for holes at each course, and the number of holes played at the course. (Include all courses) SELECT CourseName, Count(RoundId) AS NumberofHolesPlayer,

Avg(YardsForHole) AS AvgYardsForHole, Avg(ParForHole) AS ParForHoleFROM (GolfCourses AS GC LEFT JOIN GolfRounds AS GR ON GR.CourseID = GC.CourseID) RIGHT JOIN CourseHoleDetails AS CHD ON GC.CourseID = CHD.CourseIDGROUP BY GR.CourseID, CourseName;

Page 24: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Question 3

Display each golf courses name, the average yards and par for holes at each course, and the number of rounds played at the course. (Include all courses) SELECT CourseName, Count(DISTINCT RoundId) AS

NumberofHolesPlayer, Avg(YardsForHole) AS AvgYardsForHole, Avg(ParForHole) AS ParForHole

FROM (GolfCourses AS GC LEFT JOIN GolfRounds AS GR

ON GR.CourseID = GC.CourseID) RIGHT JOIN CourseHoleDetails AS CHD ON GC.CourseID = CHD.CourseIDGROUP BY GR.CourseID, CourseName;

Page 25: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+

HTML / PHP

Page 26: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Forms

<body> <h1>My Lab 10 Form Page</h1>

<h2>Add A Golf Handicap:</h2> <form action="lab10_action.php" method="post">

Handicap Date: <input name="HandicapDate"

type="text" /> (format: 2013-1-31)<br />

Handicap Score: <input name="HandicapScore" type="text" /> (format: 1 or -1)<br />

<input type="submit" />

</form></body>

Page 27: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Drop Down List

<body> <h1>My Lab 10 Form Page</h1>

<h2>Add A Golf Handicap:</h2> <form action="lab10_action.php" method="post">

//add Stuff Here for selection box

Handicap Date: <input name="HandicapDate" type="text" /> (format: 2013-1-31)<br />

Handicap Score: <input name="HandicapScore" type="text" /> (format: 1 or -1)<br />

<input type="submit" />

</form></body>

Page 28: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Drop Down List

<p>Select a player: <select name="PlayerID"> <?php//Set Server, User name, Password, DB Variables//connection to the database//select a database to work with

//declare the SQL statement that will query the database$query = "SELECT * FROM PlayerDetails ";

//execute the SQL query and return records$result = mssql_query($query);

$numRows = mssql_num_rows($result); echo "(" . $numRows . " Player" . ($numRows == 1 ? "" : "s") . " Found )";

//display the results while($row = mssql_fetch_array($result)){ echo "<option value='" . $row["PlayerID"] ."'>" . $row["PlayerFirstName"] . $row["PlayerLastName"]. "</option>”;}

//close the connection?>

Page 29: + SQL, HTML, PHP Week 10. + Review and Questions Topics from last lecture Terminology Questions?

+Action Script - Insert

<html> <head><title>Lab 10 Action Page</title> </head>

<body><h1>My Lab 10 Action Page (add a Handicap)</h1>

<p><?php//All the beginning Connection Stuff.

//declare the SQL statement that will query the database$query = "INSERT INTO PlayerHandicap";$query .= "(PlayerID, HandicapDate, HandicapScore)";$query .= "VALUES ($PlayerID, '$HandicapDate', '$HandicapScore' )";

//execute the SQL query and return records$result = mssql_query($query);

echo "1 record added <br />";echo "You posted a handicap of ". $HandicapScore . " on " . $HandicapDate . " for player ";echo " " . $PlayerID ."<br /><hr />";

//close the connection?></p> </body></html>


Recommended