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

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Keys, Referential Integrity and PHP One to Many on the Web.
1Key – Report Creation with DB2. DB2 Databases Create Domain for DB2 Test Demo.
Introduction to Structured Query Language (SQL)
Objectives Connect to MySQL from PHP
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management Sixth Edition
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
INTERNET APPLICATION DEVELOPMENT For More visit:
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Programming using C# Joins SQL Injection Stored Procedures
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
ISM 4212 Lab Creating DB Tables 02 copyright Lars Paul Linden 2007.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Creating Databases for web applications [Complete presentations] More SQL Class time: discuss final projects. Do posting if you have not done it.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Creating databases for web applications Library. New example: student database. Homework: Complete class example. Catch up on source postings. Do creation.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Module Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in.
PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
+ SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?
Advanced Adhoc Reporting 2010 Visions Conference July 28, 2010.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
(SQL - Structured Query Language)
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
+ Back to html / php / sql. + HTML Hello World Page Title Hello World!!
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Lab week 10 Aggregates and sub-queries And assignment details.
 CONACT UC:  Magnific training   
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
For more course tutorials visit CIS 336 All iLabs Week 1 to Week 7 Devry University CIS 336: All iLabs Week 1 to Week 7: Devry University.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
SQL - Training Rajesh Charles. Agenda (Complete Course) Introduction Testing Methodologies Manual Testing Practical Workshop Automation Testing Practical.
SQL in Oracle.
Introduction to Web programming
ISC440: Web Programming 2 Server-side Scripting PHP 3
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Data Management Innovations 2017 High level overview of DB
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
Introduction to Web programming
Presentation transcript:

+ 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

+ SQL

+ Auto Increment Key Word for MS SQL – Identity Can specify at time of table creation – The easiest CREATE TABLE PlayerDetails( PlayerIDINTPrimary Key Identity, PlayerFirstNameChar(30)Not Null, … ); Or alter tables to include Identity

+ Happy Valley Golf

+ Altering Player ID to include -- PlayerID Alter Table PlayerHandicap drop constraint PLAYERID_FK; Alter Table Player_Rounds drop constraint PLAYERID2_FK; Alter Table PlayerDetails drop constraint PLAYERID_PK; Alter Table PlayerDetails Drop column PlayerId; Alter Table PlayerDetails Add PlayerID int identity; Alter Table PlayerDetails Add constraint pk_PlayerID primary key(PlayerId); Alter Table PlayerHandicap Add Constraint PLAYERID_FK Foreign Key (PlayerId) References PlayerDetails(PlayerId); Alter Table Player_Rounds Add Constraint PLAYERID2_FK Foreign Key (PlayerId) References PlayerDetails(PlayerId);

+ 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)

+ 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;

+ 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)

+ 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;

+ 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;

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

+ 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;

+ 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;

+ 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;

+ 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;

+ 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.PlayerID AND PR.PlayerID = PH.PlayerID GROUP BY PlayerLastName, PlayerFirstName

+ 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

+ 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

+ 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)

+ 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

+ 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 ParForHole FROM (GolfCourses AS GC LEFT JOIN GolfRounds AS GR ON GR.CourseID = GC.CourseID) RIGHT JOIN CourseHoleDetails AS CHD ON GC.CourseID = CHD.CourseID GROUP BY GR.CourseID, CourseName;

+ 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.CourseID GROUP BY GR.CourseID, CourseName;

+ HTML / PHP

+ Forms My Lab 10 Form Page Add A Golf Handicap: Handicap Date: (format: ) Handicap Score: (format: 1 or -1)

+ Drop Down List My Lab 10 Form Page Add A Golf Handicap: //add Stuff Here for selection box Handicap Date: (format: ) Handicap Score: (format: 1 or -1)

+ Drop Down List Select a player: <?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 " ". $row["PlayerFirstName"]. $row["PlayerLastName"]. " ”;} //close the connection ?>

+ Action Script - Insert Lab 10 Action Page My Lab 10 Action Page (add a Handicap) <?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 "; echo "You posted a handicap of ". $HandicapScore. " on ". $HandicapDate. " for player "; echo " ". $PlayerID." "; //close the connection ?>