MATTHEW COLLINS H

Similar documents
MARIA AMPARO FUENTES-OREA (F ) HSQ-DATABASES & SQL MANSHA NAWAZ. SERVER DATABASE EXAMPLE Generic Sales Order Processing System

SCHOOL OF COMPUTING HND YEAR 2 SOFTWARE DEVELOPMENT. Databases and SQL ICA. Paul Nicks (f )

Also please note there are a number of documents outlining more detailed League Manager processes at support.tennis.com.au

Server Side Scripting Report

HSQ Databases and SQL

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

Querying Data with Transact SQL

MTA Database Administrator Fundamentals Course

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Table Joins and Indexes in SQL

Creating the Data Layer

Data Manipulation (DML) and Data Definition (DDL)

Microsoft Access - Using Relational Database Data Queries (Stored Procedures) Paul A. Harris, Ph.D. Director, GCRC Informatics.

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Get Table Schema In Sql Server 2008 To Add Column If Not Exists >>>CLICK HERE<<<

DB2 SQL Class Outline

DSC 201: Data Analysis & Visualization

T-SQL Training: T-SQL for SQL Server for Developers

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

NCSS: Databases and SQL

Introduction to Databases and SQL

Web-based IDE to create Model and Controller Components for MVC-based Web Applications on CakePHP

CGS 3066: Spring 2017 SQL Reference

CSC Web Programming. Introduction to SQL

DB2 MOCK TEST DB2 MOCK TEST I

CS 186 Midterm, Spring 2003 Page 1

Creating databases using SQL Server Management Studio Express

CST272 SQL Server, SQL and the SqlDataSource Page 1

What is SQL? Toolkit for this guide. Learning SQL Using phpmyadmin

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

MySQL for Beginners Ed 3

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Manual Triggers Sql Server 2008 Examples

SQL Server 2008 Tutorial 3: Database Creation

Chapter 1 SQL and Data

ISYS2421ExamRevisionQuestions! Week 1!

Customizing GDPR in netforum Enterprise

Table of contents. Report absent and borrowed players... 13

SQL Interview Questions

FACETs. Technical Report 05/19/2010

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

Oracle - MySQL Fundamentals Ed 1

Introduction to Computer Science Midterm 3 Fall, Points

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Database Systems. phpmyadmin Tutorial

Exact Numeric Data Types

Sql Server Syllabus. Overview

Chapter 8: Working With Databases & Tables

MySQL for Developers with Developer Techniques Accelerated

Data Manipulation Language (DML)

Unit Assessment Guide

Locate your Advanced Tools and Applications

RESTRICTING AND SORTING DATA

II (The Sequel) We will use the following database as an example throughout this lab, found in students.db.

CSCU9Q5 Introduction to MySQL. Data Definition & Manipulation (Over ~two Lectures)

Follow these steps to get started: o Launch MS Access from your start menu. The MS Access startup panel is displayed:

Introduction to PeopleSoft Query. The University of British Columbia

2017/11/04 04:02 1/12 Coding Conventions

NUS USER GUIDE EHS360 TRAINING MODULE. Copyright National University of Singapore. All Rights Reserved.

static String usersname; public static int numberofplayers; private static double velocity, time;

Consistency The DBMS must ensure the database will always be in a consistent state. Whenever data is modified, the database will change from one

Database Systems Fundamentals

The 2018/2019 Prawn Sandwich Administration Site is now live and ready for configuration.

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Outline. Introduction to SQL. What happens when you run an SQL query? There are 6 possible clauses in a select statement. Tara Murphy and James Curran

STIDistrict Query (Basic)

Introduction to SQL. Tara Murphy and James Curran. 15th April, 2009

Workbooks (File) and Worksheet Handling

Mobile MOUSe MTA DATABASE ADMINISTRATOR FUNDAMENTALS ONLINE COURSE OUTLINE

CSCI 1100L: Topics in Computing Lab Lab 07: Microsoft Access (Databases) Part I: Movie review database.

"Charting the Course... Intermediate PHP & MySQL Course Summary

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object.

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

INFO 1103 Homework Project 2


DR B.R.AMBEDKAR UNIVERSITY B.Sc.(Computer Science): III Year THEORY PAPER IV (Elective 4) PHP, MySQL and Apache

Manual Trigger Sql Server 2008 Insert Update Delete

Chapter 7 Equijoins. Review. Why do we store data for articles and writers in two separate tables? How can we link an article to its writer?

Full-Time. Team Administrators. Version 5.1

Designing dashboards for performance. Reference deck

TRACKER DASHBOARD USER GUIDE. Version 1.0

SQA Advanced Unit Specification: general information. Relational Database Management Systems

FIT 100 More Microsoft Access and Relational Databases Creating Views with SQL

Learning Objectives. Description. Your AU Expert(s) Trent Earley Behlen Mfg. Co. Shane Wemhoff Behlen Mfg. Co.

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Deccansoft softwareservices-microsoft Silver Learing Partner. SQL Server Syllabus

Querying Data with Transact-SQL

I Wish I Knew How To. Program Access 2013 with Real Studio Desktop Apps in Windows. By Eugene Dakin. June 2013 Edition (1.7)

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

Polaris SQL Introduction. Michael Fields Central Library Consortium

Jarek Szlichta

SQL - Tables. SQL - Create a SQL Table. SQL Create Table Query:

How To Change Existing Table Schema In Sql Server 2008

Assignment: 1. (Unit-1 Flowchart and Algorithm)

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

Higher National Unit specification: general information. Relational Database Management Systems

DESIGNING, BUILDING, AND USING DATABASES (BEGINNING MICROSOFT ACCESS, X405.4)

Design document. Table of content. Introduction. System Architecture. Parser. Predictions GUI. Data storage. Updated content GUI.

Transcription:

TEESSIDE UNIVERSITY MODULE TITLE DATABASES AND SQL ASSIGNMENT TITLE CASE STUDY: SERVER DATABASE EXAMPLE MATTHEW COLLINS H8851143

Contents 1 CASE STUDY... 4 1.1 SCENARIO... 4 1.2 ADDITIONAL CONSIDERATIONS... 4 1.3 deliverables... 4 2 study plan... 5 3 SQL server database... 6 3.1 database diagram... 6 3.1.1 Full ERD diagram... 6 3.1.2 Partial ERD... 7 4 database design... 9 4.1 table design... 9 4.2 logical views... 10 4.2.1 Example... 10 4.2.2 using aliases in views... 11 4.3 functions... 13 4.4 stored procedures... 16 4.4.1 getfixtures... 16 4.4.2 squadlist... 17 4.4.3 showleague... 18 5 data definition language... 20 5.1 data definition language... 20 5.1.1 create table... 20 5.1.2 drop table... 20 5.1.3 creating constraints... 20 5.2 data manipulation language... 20 5.2.1 SELECT... 20 5.2.2 WHERE... 21 5.2.3 ORDER BY... 21 5.2.4 BETWEEN... 21 5.2.5 LIKE... 22 5.2.6 'IN' AND 'NOT IN'... 22 5.2.7 INSERT... 23 5.2.8 UPDATE... 23

5.2.9 DELETE... 23 6 Advanced Features... 24 7 CRITICAL REVIEW... 25 7.1 CONCLUSION... 25 7.2 CRITICAL REVIEW OF ICA... 25 7.3 PERSONAL DEVELOPMENT... 25 8 REFERENCES... 26 9 APPIX... 27 9.1 APPIX 1... 27

1 CASE STUDY 1.1 SCENARIO This database has been created to help facilitate the running and organisation of a local 5 a side football league compromising any number of teams. A league table will be updated based on fixture results and fixture details will show who is playing, who the referee is and which colour strip each team should wear. Statistics such as leading goalscorers will also be available. 1.2 ADDITIONAL CONSIDERATIONS It may be necessary to include a mechanism for controlling user access rights should this database be used in a publicly accessible way; such as a web interface. There is no personal or confidential data being stored in the database. 1.3 DELIVERABLES On completion I hope to have shown my proficiency in the following areas: Building a complex ERD and a partial, at minimum, implementation of the corresponding SQL Have a functional and error free database Knowledge of SQL stored procedures and functions to aid in data manipulation and presentation Knowledge of methods of connectivity to best use this database, such as via a website

2 STUDY PLAN STUDY PLAN 1. DESIGN Case Study & Deliverables x ERD Full & Partial x x Tables x x 2 IMPLEMENTATION TABLES x x x CONSTRAINTS x x INSERTS x x x DROPS x x x 3 FUNCTIONALITY SQL functions x x x SQL stored procedures x x x SQL Import Export Database x x SQL Web integration x x x 4 REPORT x x x x x Dates 26.11 3.12 10.12 17.12 24.12 31.12 7.01 14.01 Week 1 2 3 4 5 6 7 8

3 SQL SERVER DATABASE 3.1 DATABASE DIAGRAM 3.1.1 FULL ERD DIAGRAM

3.1.2 PARTIAL ERD A. This shows the relationship between the tables 'players', 'teams' and 'managers'. These tables are used to display the team name, the players that belong to this team and the team manager. B. This shows the relationship between the tables 'teams' and 'leaguetable'. These tables are used to display the team name in the league table along with all corresponding information such as games played and points scored. C. This shows the relationships between the tables 'teams' and 'strips'. These tables are used to show the strips assigned to each team. D. This shows the relationship between the tables 'teams', 'fixtures' and 'referees'. These tables are used to generate fixture lists and show which referee is in charge of which game.

E. This shows the relationship between the tables 'players', 'goals' and 'fixtures'. This can show us who scored in which game and how many goals an individual player has scored. Because of the relationship between the tables 'players' and 'teams' (as shown in Diagram A) it is also possible to show which team has scored the most goals.

4 DATABASE DESIGN 4.1 TABLE DESIGN Below are the physical views of all the tables in my database. I have added a prefix to every field name in a table to help avoid ambiguous field names in queries. If this field is used as a foreign key in another table I will also include this prefix there. For example, teams.tm_id is the primary key for the table 'teams' and is linked to the foreign key players.pl_tm_id. This is particularly helpful when joining tables that have similar field names, such as the 'players' and 'teams' tables. The SQL that demonstrates this is as below: fixtures goals leaguetable

managers players referees strips teams 4.2 LOGICAL VIEWS 4.2.1 EXAMPLE Below is a view that shows all teams in the league table, ordered by points gained.

The above shows the relationship between the 'leaguetable' and 'teams' tables along with the required fields and any sorting options. It also shows the SQL code required to display this view and the results that are output once the view is executed. The view is saved as 'table'. The SQL to manually execute this view is: SELECT * FROM [table] 4.2.2 USING ALIASES IN VIEWS Below is an example of a problem I encountered when using views in Microsoft SQL Management Studio. As shown in the Partial ERD below, there are three required tables when generating fixture lists.

This ERD shows that the 'fixtures' table has two separate relationships with the 'teams' table, however both of these relationships refer to the primary key of the 'teams' table TM_id. When using SQL Server Management Studio to create this view, this is the SQL that it generated for me: SELECT dbo.teams.tm_name FROM dbo.fixtures INNER JOIN dbo.teams ON dbo.fixtures.fxt_home = dbo.teams.tm_id AND dbo.fixtures.fxt_away = dbo.teams.tm_id INNER JOIN dbo.referees ON dbo.fixtures.fxt_ref_id = dbo.referees.ref_id The above SQL requires that both the home team ID (FXT_home) and the away team ID (FXT_away) are the same as the team ID (TM_id) which would mean that only fixtures where the same team was playing home and away would be selected. This would result in no results returned. To get around this problem I would use an alias when joining the 'teams' table, as shown in the SQL below: SELECT home_team.tm_name FROM dbo.fixtures INNER JOIN dbo.teams AS home_team ON dbo.fixtures.fxt_home = home_team.tm_id INNER JOIN dbo.teams AS away_team ON dbo.fixtures.fxt_away = away_team.tm_id INNER JOIN dbo.referees ON dbo.fixtures.fxt_ref_id = dbo.referees.ref_id The image below shows how this changes the tables in the view; it now includes the 'teams' table twice, each with a different alias (home_team and away_team). It also shows the fields selected and the results output when this view is executed. This view is saved as 'vfixtures'.

The SQL to execute this view is: SELECT * FROM [vfixtures] 4.3 FUNCTIONS I have created a function used when updating any given league table column for any given team in the database. The SQL used in this function is shown below. CREATE FUNCTION updatetable (@team int, @col char) RETURNS int AS DECLARE @points int DECLARE @temp int DECLARE @played int DECLARE @won int

DECLARE @draw int DECLARE @lost int DECLARE @output int SET @points = 0 SET @temp = 0 SET @played = 0 SET @won = 0 SET @draw = 0 SET @lost = 0 SET @output = 0 DECLARE @week int SET @week = 1 WHILE (@week <= 10) SET @temp = (SELECT CASE WHEN FXT_homescore > FXT_awayscore THEN 3 WHEN FXT_homescore = FXT_awayscore THEN 1 ELSE 0 AS points FROM fixtures WHERE FXT_home = @team AND FXT_week = @week AND (FXT_homescore IS NOT NULL AND FXT_awayscore IS NOT NULL) ) IF (@temp >= 0) SET @points = @points + @temp SET @played = @played + 1 IF (@temp = 3) SET @won = @won+1 IF (@temp = 1) SET @draw = @draw+1 IF (@temp = 0) SET @lost = @lost+1 SET @temp = (SELECT CASE WHEN FXT_awayscore > FXT_homescore THEN 3 WHEN FXT_awayscore = FXT_homescore THEN 1 ELSE 0 AS points FROM fixtures

WHERE ) FXT_away = @team AND FXT_week = @week AND (FXT_homescore IS NOT NULL AND FXT_awayscore IS NOT NULL) IF (@temp >= 0) SET @points = @points + @temp SET @played = @played + 1 IF (@temp = 3) SET @won = @won+1 IF (@temp = 1) SET @draw = @draw+1 IF (@temp = 0) SET @lost = @lost+1 SET @week = @week + 1 IF (@week > 10) BREAK ELSE CONTINUE IF (@col = 'W') SET @output = @won IF (@col = 'D') SET @output = @draw IF (@col = 'L') SET @output = @lost IF (@col = 'P') SET @output = @played IF (@col = 'S') SET @output = @points RETURN(@output)

The function is called with the following parameters @team this is the unique ID (TM_id) of the team you wish to update @col this is the column within the league table you wish to update The values and their meanings for @col are as follows: 'P' Played 'W' Won 'D' Drawn 'L' Lost 'S' Scored (updates the Points column) When two correct values are passed to this function, it will return the correct value to SQL. This can be used to update all of these columns for all of these teams with a simple SQL statement, as shown below: UPDATE leaguetable SET TBL_played=dbo.updateTable(TBL_TM_id, 'P'), TBL_won=dbo.updateTable(TBL_TM_id, 'W'), TBL_draw=dbo.updateTable(TBL_TM_id, 'D'), TBL_loss=dbo.updateTable(TBL_TM_id, 'L'), TBL_points=dbo.updateTable(TBL_TM_id, 'S') 4.4 STORED PROCEDURES I have created a number of stored procedures to help display data in a useful and relevant format. I have used stored procedures instead of logical views as a stored procedure gives me more flexibility to manipulate the data returned via parameters. 4.4.1 GETFIXTURES This stored procedure has one parameter (@week, integer) and returns a list of fixtures for a particular game week, as defined by this parameter. The stored procedure will also check the shirt colour of each team (STP_shirt) and if both teams have matching shirts, the away team will use an alternate option as defined in the 'strips' table (STP_alt_shirt). The SQL used to create this stored procedure is shown below.

And the SQL used to generate results for game week 3 is shown below. 4.4.2 SQUADLIST This stored procedure has one parameter (@team, integer) and returns a squad list, team name and manager name, as defined by this parameter. The SQL used to create this stored procedure is shown below:

The SQL used to generate results for team 3 (teams.tm_id = 3) is shown below: 4.4.3 SHOWLEAGUE This stored procedure contains the SQL shown above in the functions chapter along with some extra SQL to update the league table and then display the results.

It takes no parameters and calls the updatetable function five separate times, one for each column that needs to be updated, after updating the table it then displays the league table.

5 DATA DEFINITION LANGUAGE 5.1 DATA DEFINITION LANGUAGE 5.1.1 CREATE TABLE The following code is an example of how to create a table using SQL: CREATE TABLE [dbo].[teams]( [TM_id] [int] IDENTITY(1,1) NOT NULL, [TM_name] [nchar](20) NULL, [TM_STP_id] [int] NULL, CONSTRAINT [PK_teams] PRIMARY KEY CLUSTERED The table is called 'teams' There are three different columns: TM_id, TM_name, TM_STP_id There are two different column types: o int integer value o nchar numbers and other characters, in this instance with a limit of 20 characters The TM_id field is an IDENTITY field, this field will increment by 1 for every new row, starting with the value 1. 5.1.2 DROP TABLE The following code shows how to drop a table from the database using SQL: DROP TABLE fixtures 5.1.3 CREATING CONSTRAINTS The underlined code shows how a constraint is added to a table: CREATE TABLE [dbo].[teams]( [TM_id] [int] IDENTITY(1,1) NOT NULL, [TM_name] [nchar](20) NULL, [TM_STP_id] [int] NULL, CONSTRAINT [PK_teams] PRIMARY KEY CLUSTERED 5.2 DATA MANIPULATION LANGUAGE 5.2.1 SELECT The SELECT statement is used to define the fields we select and from which table(s). The query below shows how to select all fields from the 'teams' table.

5.2.2 WHERE The WHERE statement allows conditions to be added to the SELECT statement, allowing certain data to be selected. The query below shows how to select 'Stockton Town' from the 'teams' table. 5.2.3 ORDER BY The ORDER BY statement is used to order output from a query in a particular way. It is possible to order by any field, ascending or descending. The following query shows all teams, ordered by their team name in ascending order. 5.2.4 BETWEEN The BETWEEN statement is used to compare either numerical values or dates. The condition is inclusive, meaning that the following query would show all fixtures in weeks 1, 2 and 3.

5.2.5 LIKE The LIKE statement is used to find a partial text match when searching on a particular field. This is used in conjunction with the WHERE statement. The wildcard character (%) can be used to search for the beginning or end of strings. The following query will search for all player names beginning with A. 5.2.6 'IN' AND 'NOT IN' The IN and NOT IN statements can be used to search for values compared to a list of predefined values. The query below will search for all players who are in team 3 or 4:

Whereas the following query will do the opposite; showing all players that are NOT IN teams 3 or 4: 5.2.7 INSERT The following query will add a player to team 1. INSERT INTO players (PL_TM_id, PL_name) VALUES (1, 'Mark Wildon') 5.2.8 UPDATE The following query will update the team name of team 1. UPDATE teams SET TM_name = 'Redcar Town' WHERE TM_id = 1 5.2.9 DELETE The following query will delete a player from the 'players' table. DELETE FROM players WHERE PL_name = 'Mark Wildon'

6 ADVANCED FEATURES It is possible to link SQL Server to other applications, one example of this is to a website application using a scripting language such as.net or PHP. The following code shows how to connect to the database and perform a simple query using PHP.

7 CRITICAL REVIEW 7.1 CONCLUSION The original aim was to create a fully functioning database system, driven by a web front end to help organise and run a local 5 a side league. Due to time restraints only the database system was designed and created. If more time was allowed then a complete system could have been presented. The database is fully functional and can be used in its current state to manage a 5 a side league but it is not very user friendly and a web front end would improve the user experience massively. 7.2 CRITICAL REVIEW OF ICA There were many other possible features that could have been added to this system and I feel that this would have led to more tables being added to the model, and as such a more complex ERD with more complex queries. I do believe that my current database and table structure, as shown in the ERD, is of an acceptable standard to demonstrate proper usage of SQL Server. 7.3 PERSONAL DEVELOPMENT I have had previous knowledge of using mysql and I felt that this put me in a good position to create a solid database. I used this ICA as an opportunity to use advanced features such as Stored Procedures and User Defined Function to help improve the functionality of my database. I had very limited knowledge of this beforehand and had to refer to online tutorials to help me with this. I feel confident that I could now use these features in future projects with minimal help.

8 REFERENCES 1. Mansha Nawaz Sample Reports and Examples posted on his website http://scm intranet/users/u0000706/ 2. MSDN Online documentation from Microsoft MSDN http://msdn.microsoft.com/en us/library/bb545450.aspx 3. PHP.net Online Documentation from PHP.net http://www.php.net/manual/en/function.mssql query.php

9 APPIX 9.1 APPIX 1 The following are links to the create script files for the tables used in the database. Teams Strips Referees Players Managers Leaguetable Goals Fixtures The following is a link to the create script files for the functions and stored procedures I have created in the database. Functions and Stored Procedures