CS 362 Phase 5 Individual Project
-
Upload
mark-murphy -
Category
Documents
-
view
754 -
download
2
description
Transcript of CS 362 Phase 5 Individual Project
CTU
Phase 5 Individual Project
CS 362
Mark Murphy
9/19/2012
1
TABLE OF CONTENTS
ABSTRACT................................................................................................................................................2
Part 1: Function...........................................................................................................................................2
Part 2: Stored Procedure.............................................................................................................................3
Part 3: Indexes.............................................................................................................................................3
PART 1: FUNCTION......................................................................................................................................4
PART 2: STORED PROCEDURE......................................................................................................................8
PART 3: INDEXES........................................................................................................................................13
CONCLUSION........................................................................................................................................21
REFERENCES........................................................................................................................................22
2
ABSTRACT
There were several steps to this assignment. I had to add proper indexes, a
function, and a stored procedure to my database. The changes will provide functionality
used by the teachers for screens such as a grade book. The parts of this assignment
are broken down into parts and described in further detail below.
Part 1: Function
Write a user-defined function (UDF) that calculates a student's GPA for a given
time frame. Inputs are StudentId int, ClassStartDateStart datetime, and
ClassStartDateEnd datetime. The output should be the student's GPA for all classes
that were taken between ClassStartDateStart and ClassStartDateEnd. Also, supply the
script to call this new function.
Part 2: Stored Procedure
Write the script to make a stored procedure that returns data needed to display a
grade book screen for a professor. The only input for the stored procedure is a ClassId.
Outputs need to include student names and grades for all assignments as well as a
calculated overall grade for the class for each student. Provide an example calling this
new stored procedure, passing it parameter values of your choice. Include a screenshot
of the output.
3
Part 3: Indexes
Provide a list of suggested indexes and the script to create them. Include an
explanation of the purpose of indexes and how you made your decision for the fields to
include in your list of suggested indexes.
4
PART 1: FUNCTION
This part of the assignment requires me to calculate a student’s GPA. To
determine the students GPA, I had to use the inputs StudentId int, ClassStartDateStart
datetime, and ClassStartDateEnd datetime. This is how I added the inputs to my
database to create the Function.
From the Object Explorer, I selected the database I was working with. I expanded
the database and selected the Functions folder.
I right clicked on the Functions folder and selected New Inline Table Valued
Function.
5
This is a screenshot of the template that appeared.
From here I could edit the Function and create the function I needed for the
project. This is how I did that.
6
I entered USE and [CS 362 Phase 1 Individual Project] then on the next line I
entered the word GO. That bit of scripting tells the server that I am using the CS 362
Phase 1 Individual Project database and GO tells the server to go to that database.
Next, I had to indicate that I was creating an object that was going to be used in a
database that was a User Defined Function. I wanted to save the function as
StudentsGPA and I saved the date when I created the script.
For the actual script part, I entered CREATE FUNCTION [dbo].
[sp_StudentsGPA]. The inputs I used were StudentID, ClassStartDateStart and
ClassStartDateEnd. The output I wanted was going to be the students GPA so I added
up the grade from the students four assignments and divided that grade by 4 (since
there are a total of 4 assignments). After I entered all that I ran it to see if it was correct.
And this is the output from my function after it was compiled and verified.
7
This is the script for my User Defined Function.
USE [CS 362 Phase 1 Individual Project]GO
/******Object: UserDefinedFunction [dbo].[StudentsGPA] Script Date: 9/23/2012SET ANSI_NULLS ONGO
SET QUOTED INDETIFIER ONGO
CREATE FUNCTION [dbo].[sp_StudentsGPA](StudentID int, ClassStartDateStart datetime, ClassStartDateEnd datetime)ASBEGIN
RETURN (@Assignment1+@Assignment2+@Assignment3+@Assignment4)/4 ENDGO*/
And this is the execute script for my User Defined Function to show that it was
completed successfully.
8
PART 2: STORED PROCEDURE
For this section, I had to create a script to make a stored procedure work. The
only input for this procedure is ClassId and the output should include the student names
and grades for all the assignments as well as a calculated overall grade for each
student in the class.
This is how I created the new stored procedure. From the Object Explorer, I
opened the Programmability section.
I selected the Stored Procedures and right clicked on that.
9
I selected New Stored Procedure and this template appeared.
From here, I could create my stored procedure and this is how I did that.
10
For this assignment, I had to make a stored procedure that returns data needed
to display a grade book for a professor. To do that, in the query window I typed use
[CTU CS 362 Phase 1 Individual Project] and then I typed the word go on another line. I
selected CS 362 Phase 1 Individual Project because it is the database I am working
with and the word GO tells the server to go to that database. Here is a screenshot.
I entered that I wanted to create an object that was a Stored Procedure. I wanted
to use the Stored Procedure in my database and I wanted the Stored Procedure to be
saved as sp_GetStudentsGrades. I wanted the results of the procedure to be displayed
as a decimal with one digit followed by a decimal and two other digits. I choose to
display the results this way because it could display a student’s grade in the class as a
numeric output. Also, I wanted the output to display the student’s first name, last name
and their grade, in that order. After I entered all the data into the query window, I verified
11
the information. This is screenshot of the procedure I created and that it was compiled
successfully.
Now that the information was compiled correctly, I wanted to save the script.
12
This is the SQL text for my Stored Procedure.
use [CS 362 Phase 1 Individual Project]go
/******Object: StoredProcedure [dbo].[sp_GetStudentsGrades] Script Date: 9/23/2012SET ANSI_NULLS ONGO
SET QUOTED_IDENTIFIER ONGO CREATE FUNCTION [sp_GetStudentsGrades]StudentsGrades decimal 1,2asselect FirstName, LastName, Gradesgo*/
13
PART 3: INDEXES
In this section I had to provide a list of suggested indexes and the SQL text
scripts that I used to create them. Indexes are created to help search data and are
especially useful when a large amount of data must be searched. Indexes can convert
text characters and convert them to a numeric value. This is done because it is easier
and faster to search through a large amount of number data than it is to search text.
For this assignment, I had to create one index. I choose to create an index for the
student’s last name and this is how I created the index. From the Object Explorer, I
opened the database I was working with, like this.
I opened the Tables section and, since I am making an index for the Students
last name, I selected the Students table.
14
From here, I right clicked Students and selected Design. This is the next screen
that appeared.
15
From here, I right clicked on “LastName” and selected Indexes/ Keys. This is what that
process looked like.
My next step was to setup the index. This is how I did that.
16
I selected the Columns options in the General section and an Index Columns table
appeared. I selected Last Name since that is what I am using as my index. And I opted
for the results to be sorted in ascending order. This is a screenshot of what that looked
like.
17
I clicked OK and saved the index. Then I wanted to make sure that the index was
saved properly, so I went back to the Object Explorer and refreshed the tables to the
database. I expanded the database tables and here is a screenshot.
18
The index I just created is saved under the Indexes folder. Next I will explain how
to script this index and include the SQL text script.
I right clicked on the index and selected Script Index As then CREATE To and
New Query Editor Window. Here is a screenshot of that.
19
Here is a screenshot showing that the Index was compiled corrected and verified.
20
And here is the script.
USE [CS 362 Phase 1 Individual Project]GO
/****** Object: Index [PK_Students] Script Date: 9/21/2012 5:04:28 PM ******/ALTER TABLE [dbo].[Students] ADD CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED (
[StudentID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]GO
21
CONCLUSION
This is my phase 5 individual project. This has been a difficult class for me, but I
think I learned a lot. I have worked hard and gained a deep appreciation for databases.
Although I was not able to watch any of the classes live, I think I learned a lot from
watching the archived chat sessions.
For this project, I had to create a function, stored procedure and an index. This
was a challenging and time consuming project. I referred to the live chats several times
for guidance. I hope the hard work and time I put into this project is reflected in my
submitted work.
22
REFERENCES
ONeill, T. (2012). SQL Server Stored Procedures for Beginners. Retrieved September
24, 2012, from http://www.sql-server-performance.com: http://www.sql-server-
performance.com/2003/stored-procedures-basics/8/
pinaldave. (2008, January 15). SQL SERVER – What is – DML, DDL, DCL and TCL –
Introduction and Examples. Retrieved September 2, 2012, from
http://blog.sqlauthority.com: http://blog.sqlauthority.com/2008/01/15/sql-server-
what-is-dml-ddl-dcl-and-tcl-introduction-and-examples/
QiSQL. (n.d.). Learning SQL - SQLite Basics. Retrieved August 25, 2012, from
http://qisql.com: http://qisql.com/qisql_sqlite_basicsl.html
w3schools. (2012). SQL Data Types. Retrieved September 22, 2012, from
http://www.w3schools.com: http://www.w3schools.com/sql/sql_datatypes.asp