UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF...

40
UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND Databases and SQL Case Study Server Database Example Author: Gareth Williams - H8017251 Reader: Mansha Nawas Submission Date: 15/01/2010

Transcript of UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF...

Page 1: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

UNIVERSITY OF TEESSIDE

SCHOOL OF COMPUTING

HND Databases and SQL

Case Study Server Database Example

Author: Gareth Williams - H8017251

Reader: Mansha Nawas

Submission Date: 15/01/2010

Page 2: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

2

Contents

Title Page......................................................................................................................i

Contents.......................................................................................................................ii

Planning ..................................................................................................................... 4

1. Case Study ............................................................................................................. 5

1.1 Scenario ........................................................................................................... 5

1.2 Additional Considerations & ERDs consulted ................................................... 6

2. Microsoft SQL Server Database ............................................................................. 7

2.1 Database Diagrams .......................................................................................... 7

a) Entity Relationship Diagram - Logical ............................................................. 7

B) Entity Relationship Diagram - Physical ........................................................... 7

Reason for fields ..................................................................................................... 8

a) Tables (physical) with sample data ................................................................. 9

b) Views (logical) with sample data ................................................................... 11

3. Demonstration of SQL Code ................................................................................ 13

3.1 Data Definition Language ............................................................................... 13

a. CREATE TABLE with CONSTRAINT ............................................................ 14

b. CREATE INDEX ............................................................................................ 15

c. DROP TABLE ................................................................................................ 16

d. CREATE VIEW ............................................................................................. 17

e. CREATE TRIGGER ...................................................................................... 18

3.2 Data Manipulation Language .......................................................................... 19

a. SELECT & FROM ......................................................................................... 19

b. WHERE CLAUSE ......................................................................................... 19

c. ORDER BY.................................................................................................... 20

d. DISTINCT...................................................................................................... 20

Page 3: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

3

e. BETWEEN and LIKE ..................................................................................... 21

f. IN and NOT IN Joining Tables ....................................................................... 22

g. Aliases for Tables .......................................................................................... 23

h. Sub-Queries .................................................................................................. 23

i. INSERT .......................................................................................................... 24

j. UPDATE ......................................................................................................... 24

k. DELETE ........................................................................................................ 24

4. Show understanding of Advanced Server - Client Features ............................. 25

a. Microsoft SQL Server Video Installation Guide ............................................. 25

b. Microsoft SQL Attach and Detach ................................................................. 26

c. Microsoft SQL Server (physical tables) to Microsoft Access workstation (logical views).................................................................................................... 28

Evaluation ................................................................................................................ 30

Critical Review ......................................................................................................... 31

References ............................................................................................................... 33

Appendix .................................................................................................................. 34

a) Hyperlinks to the sql database .mdf & .ldf files ................................................. 34

b) All script files for the section 3 and 4 ................................................................ 34

Page 4: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Planning I will be creating a database for an online blog site called SocialBlogs. The focus will be on the tables and test data in the database rather than the web. But I will be demonstrating linking with external applications such as Access 2009.

Above is a screenshot of my plan for this project. The dates are not the dates the task is started; it is the date that section has to be completed by.

Page 5: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

1. Case Study

1.1 Scenario

For my Databases and SQL In-Course Assessment I will be designing and implementing a proposed server database system for a Blog Site. I will use skills I have and build upon them while learning new skills as I progress through the project.

The Database will hold the following information; *A temp_user table to keep the active user table clean of people who don’t use the site. Once a person as fully activated their account their information is copied across to the Users table *The User tables will be for User Registration, Login and Account information. *A Blog table to allow all users to create multiple blogs. *A Category table to allow users to specify a category for their blog entries. *A Comments table which will allow all users to comment on the blog freely.

The focus will be on creating the database rather than the website. But with the idea in mind the concepts behind the database will make more sense when working through the documentation.

This project will be completed using:

* Microsoft SQL Server 2008.

* Microsoft Access 2007.

I will be managing the entire system within Microsoft SQL Server 2008, the other applications will be used to demonstrate my understanding of the links.

All coding will be fully commented and demonstrate my understanding of the knowledge I have gained since the start of this course. Most of the application will be self explanatory and all associated files will be handed in with this ICA.

Page 6: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

1.2 Additional Considerations & ERDs consulted

To get an initial idea for my chosen I looked at a few premade ERDs from the database of free data models.

Image taken from the library of free database models when researchinghttp://www.databaseanswers.org/data_models/blogs/index.htm

I felt this diagram was too basic and I could create a more in depth database From the idea, I created my own ERD that I will be using while implementing.

Image taken from the library of free database models when researching

http://www.databaseanswers.org/data_models/social_networking/index.htm

This one only had only one element of the system I intent to make and I didn’t want to create a full blows Social Network because a lot of the elements would just be repetitive and not give anything new towards the documentation

Page 7: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

2. Microsoft SQL Server Database

2.1 Database Diagrams

There are two key types of Modelling when it comes to database design. These who are Logical and Physical, below I have produced a diagram representing these who types.

a) Entity Relationship Diagram - Logical

The Logical ERD is a more simplistic design that is more readable than the Physical. If I had a client this would be the quick notes I wrote down when talking it through with my client to make sure I had everything they wanted. One they’ve accepted this as what they want I move onto creating the Physical ERD, since I’m my own client I will accept this as my requirements

B) Entity Relationship Diagram - Physical

This Physical ERD shows the design of the database from the requirements established in the Logical ERD above. This holds more in depth information like Data Types, appropriate linking, etc. I feel this is essential before you even think of starting to create the Database you’re creating for your client.

Page 8: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

Below is how the database looks now that I have created the tables and linked them as I planned.

The image was too wide to get the picture any better quality but there is a better image of this both on the disk and in the database. As you can see it is almost identical to the design above in every way. I find the best way to create a project it to focus on the planning of it before touching the tools you’re going to use to complete the task.

Reason for fields

I just thought I should justify one or two things in the beginning of the documentation. I have set the date fields to VARCHAR because I have had issues in the past where date fields only accept the date is it’s using the right dividers or format and I’ve it’s slowly grown on me.

The reason for the random characters and numbers in the password fields is to demonstrate an encrypted password because I always put security first when programming and I would never put a password straight in.

The userId and the confirmCode in the temp_user table is a fixed lenth integer generated by the proposed web side of the project.

Page 9: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

2.2 Management Studio

a) Tables (physical) with sample data

The images below show the Physical tables with the data type clearly visible.

I feel these are quite self explanatory so I havnt added any additional information

a.1 The temp_Users Table (physical)

a.2 The Users Table (physical)

Page 10: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

a.3 The Blog Table (physical)

a.4 The Category Table (physical)

a.5 The Comments Table (physical)

a.6 The user_blog Table (physical)

a.7 The blog_cat Table (physical)

a.8 The blog_com Table (physical)

Page 11: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

b) Views (logical) with sample data

These images show the Logical view of the tables with the sample test data already entered. I feel these are quite self explanatory so I havnt added any additional information

b.1 The temp_Users Table view (logical)

b.2 The Users Table view (logical)

b.3 The Blog Table view (logical)

b.4 The Category Table view (logical)

b.5 The Comments Table view (logical)

Page 12: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

b.6 The user_blog Table view (logical)

b.7 The blog_cat Table view (logical)

b.8 The blog_com Table view (logical)

Page 13: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

3. Demonstration of SQL Code

3.1 Data Definition Language

The first thing to do before coding any SQL code is to open a new query window by pressing the button in the top left corner of the screen. This needs to be done before each sub-section I talk the reader through.

Page 14: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

a. CREATE TABLE with CONSTRAINT

All my create table pieces of code have constraints in them I feel this helps to keep the database from getting mismatch or link issues.

I will be demonstrating the “users” table in this section of the documentation. This is the required code below: --Create users table CREATE TABLE users( userId INT NOT NULL CONSTRAINT userIdPK PRIMARY KEY, userName VARCHAR(20) NOT NULL, firstNamet VARCHAR(30) NOT NULL, lastName VARCHAR(30) NOT NULL, email VARCHAR(50) NOT NULL, [password] VARCHAR(60) NOT NULL, gender VARCHAR(15) NULL, dob VARCHAR(10) NULL, userImage VARCHAR(50) NULL, regDate VARCHAR(10) NOT NULL, aboutMe VARCHAR(200) NULL, )

Once this code is entered and the query has been run the following confirmation is displayed to inform you that it was successful.

Page 15: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

b. CREATE INDEX

If I’m not mistaken an index in SQL Server works just like the index in Windows it allows the queries to run faster and be overall more efficient in searching, finding and loading.

Below is the code I will be using in my new query to create the index.

--Create an index for a specified column CREATE INDEX I_Users_Userid on Users (Userid) The code completed successfully

The Users table should be quicker when searching for information. But because there isn’t very much in this table we won’t really see a different We’d have to have a much larger database to really get the benefits of this.

Page 16: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

c. DROP TABLE

This script is similar to the create script except it simply removes the table from the database in one short and sweet click. The table we’ll be removing is the one we just created. The Users table. This is the script needed to remove a table from the database.

--Removes the users table DROP TABLE users

Once the script has been run your screen should now look like the image below. When the tables are refreshed there will no longer be a Users table.

Page 17: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

d. CREATE VIEW

A view allows you to say what field you’re looking for just like a query except it remembers it only needs to show the fields you selected. This means less processing and quicker results. Below is the code to make the view query --Create View for Users table CREATE VIEW V_Users AS SELECT username, firstName, lastName, email FROM Users This screenshot shows the Create View code completed successfully

Here is the view I created in action only showing the fields I chose.

Page 18: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

e. CREATE TRIGGER

The trigger was something I hadn’t really come across and although I was a little daunted at first I thought it would probably be a really good feature. Below is the script used to activate the trigger: IF OBJECT_ID ('users.reminder', 'TR') IS NOT NULL DROP TRIGGER users.reminder; GO -- This trigger raises a message whenever a row is inserted or modified in Sales.Customer CREATE TRIGGER reminder ON users AFTER INSERT, UPDATE, DELETE AS EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Administrator', @recipients = '[email protected]', @body = 'Don''t forget the ICA is due in on the 15th', @subject = 'Reminder'; GO Below is a screenshot of the script having completed successfully

This feature is quite handy and could make for an excellent snippet of code. Sadly I cannot supply a screenshot of this features final stage because the mail client on my computer will not run with SQL.

Page 19: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

3.2 Data Manipulation Language

a. SELECT & FROM

This feature is used to select specific items to bring up in a query. This is the script I will be using in this example. --SELECT all fields FROM the Users table SELECT * FROM Users

Here is a screenshot of the run query with a long list of results for every user.

b. WHERE CLAUSE

This feature does the same thing as the SELECT statement except it allows you to specify a parameter all results have to conform to. --SELECT all fields that meet the requirements FROM the Users table SELECT * FROM Users WHERE gender = 'male'

Below is a screenshot of the completed query with a much neater result.

Unless you requires every record in the database you’re use this more often.

Page 20: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

c. ORDER BY

Order by takes your query result and sorts it into an order of your choice For this example I will be sorting the results ascending Code to be used in this example:

--ORDER BY returns sorts the users ascending SELECT * FROM Users WHERE gender = 'male' ORDER BY userName ASC; The completed query similar to above but sorted Asendingly.

d. DISTINCT

For this example I will need to run two queries to demonstrate the capabilities of this feature.

First script --Normal select statement SELECT blogId FROM blog_com; Screenshot of resulting query

Second Script --Distinct ruturns the unique Ids SELECT DISTINCT blogId FROM blog_com; Screenshot of resulting query

It’s a very powerful feature that makes sorting through a range of items must easier than ever before.

Page 21: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

e. BETWEEN and LIKE

The between feature allows you to return result that falls between two values, whereas like allows you to put the beginning letters and a wildcard in the search to bring back a result. Belows are the two scrips followed by the resulting screenshot of the completed queries.

--Between shows results between two values SELECT * FROM Users WHERE userId BETWEEN 2 AND 4;

--Like shows results with a wildcard SELECT * FROM Users WHERE gender LIKE 'fe%';

I especially like using the like parameter when I’ve forgotten the name of something in my table but I remember part of it. It’s really helpful if you don’t want to go searching through record after record after record.

Page 22: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

f. IN and NOT IN Joining Tables This feature is good because it allows you to set multiple criteria to search for.

Example of IN --The IN script looks for both criteria when searching SELECT * FROM Users WHERE gender IN ('male', 'female');

All results with the words male or female in the gender were returned..

. Example of NOT IN Example code: --The NOT IN script ignores anything with the criteria below SELECT * FROM Users WHERE gender NOT IN ('male', 'female');

As you may have already guessed this is the exact opposite from the first query it ignores the results that have male and female entered. All results without the words male or female in the gender were returned.

Page 23: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

g. Aliases for Tables

This allows you to change the name of the column being displayed. I thought I would make this one fun by giving password the alias of gobbledygook. Like with all the other examples it’s a straight forward piece of code to demonstrate the task at hand without confusing with any extra bits.

--The Alias allows you to change the name of a field for that query SELECT password AS gobbledygook FROM Users

The successful query returned looks like this

h. Sub-Queries

This is a demonstration of a sub-query, which is the action of running a query inside of another query. The result is dependent on the second queries result. Example Script --Sub-Query allows multiple quires in one (they are dependant) SELECT catName FROM category WHERE catId IN (SELECT catId FROM blog_cat WHERE catId = '1')

Page 24: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

The next three are the very first pieces of SQL script I ever learnt. They’re quite basic and self explanatory so I wont go too indepth

i. INSERT

What this does is allow you to insert a new record into a table We’ll be adding a new record to the temp_users table to demonstrate

--Insert a new record into the table INSERT INTO temp_users VALUES ('11','Jen','Jenny','Williams','[email protected]', 'dwi3r4r53jth4r8h4w3r2gtge34ruhfs','female','','','01-01-2010','25432823'); Script ran correctly and the screenshot shows the new record

j. UPDATE

We will be changing the UserName Jen to Jen2010 and dob to undisclosed.

--Update a new record from the table UPDATE temp_users SET userName='Jenny', dob='26/08/1963' WHERE userName='Jen'

k. DELETE

Here we will be removing a whole record where the username is Jen2010

--Delete a record from the table DELETE * FROM Users WHERE userName='Jenny'

Notice the record is no longer visible at the bottom of the list of records

Page 25: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

4. Show understanding of Advanced Server - Client Features

a. Microsoft SQL Server Video Installation Guide

To accompany this document and associated database files I have also created an video installation guide that takes you through the installation process from start to finish. It is a small enough size so that it can be easily taken anywhere and even uploaded to YouTube to share with other students.

This can be found under videos on the top level of the disk

Page 26: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

b. Microsoft SQL Attach and Detach

For this section I’ll be supplying screenshot evidence of having attached and detached the SQL Database.

Detach Database

To detach the database you need to do is simply right click on it, go to Tasks and the click Detach

Once you’ve done this you will be taken to the screen below informing you of which database is selected to be detached. If any users are currently running the database you will not be able to detach the database. If this is the case like in the below image simply clip Drop Connections. This will make sure everyones disconnected before the database is detached.

Once you’ve done this click ok, it will process then complete. You have just detached your first database.

Page 27: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

Attach Database

Attaching or in this case reattaching is just as painless and stress free as detaching. Simply right click on Databases and click Attach like demonstrated

in the below image.

You will then be taken to a new screen, Press “Add...” and select your database once selected your screen will look very similar to the below image. This is telling you which database is being attached and what files. One Database file and one Data log file.

These are dependent on each other because all changed renames or anything you do in the database is written to the log to make sure it hasn’t been tampered with in any way. The detach-attach Database went very well.

Page 28: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

c. Microsoft SQL Server (physical tables) to Microsoft Access workstation (logical views)

For this section I will be demonstrating how to link to an SQL database through Microsoft Access. Step1 – Open a new Access databae Step 2 – Click the External Data tab and press More. Here you’ll see a list of options. The one you want it ODBCdatabase. Once selected this you’ll be given a new window as demonstrated below

You want to Link to the database. If you’re unsure I’d recommend Linking all the time because this way you know the data is only being changed in the linked database and on a group network there’s will be less chance of losing work. Now on the new screen select New and scroll down to SQL Server.

Proceed by clicking next and give the new link connection a file name and destination by pressing browse naming the file. I tent to call it “DBlink” as demonstrated below.

Page 29: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

Click next when you are done and you will get a screen asking what you want to describe the connection as and which server you want to connect to. I will be connecting with the setting shown below. Proceed by clicking next.

Once on this screen enter your login credentials as needed. I will be connecting with NT authentication rather than with the SQL Server authentication. And proceed by pressing next.

Click next on the following two screens until you reach the end and finalise the connection. You can test the connection from this point. Finish by pressing ok in the last screen, selecting your new file from the page and click open. Once on this screen select the tables you want and you are now fully connected to your SQL Server through Microsoft Access. Well Done.

Page 30: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

Evaluation I feel that the whole project has gone exceptionally well. The screenshots and drawings all game together to make what I feel is an excellent piece of work. I have learnt a lot in the short duration of this

I could have done better on some aspects if I had put the same amount of effort into each section of the project but like with most things there are things you enjoy doing and things you don’t. I still did the best I could and I think it’s paid off. My knowledge of databases is much more focused that it used to be and I think I will continue with SQL Server beyond this course and implement it in my future job one day as a web developer.

The sound file in the movie clip became corrupt which was a real shame but the video is still safe to use. There’s just no audio to go with the picture I figured it’s still clear what’s going and I feel it could still be an aid to anyone trying to install SQL Server on their home machine.

This has been a really enjoyable ICA to work on.

Page 31: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

Critical Review

Development of Written Communication Skills Understanding task requirements

Did you:

Read the assessment guidelines and criteria? Follow guidelines? Seek clarification?

But in hindsight I could have……. Asked more about the layout and format of the documentation. Planning your time effectively

Did you allocate enough time to: Gather resources? Write draft? Make amendments? Proof-read? Check references/sources are reliable?

But in hindsight I could have……. Started earlier than I did to produce a better report

Getting down to actually producing the work

Were you able to: Get started on time? Keep to your planned time schedule? Document the sources of your information as you were reading? Keep focused on the task? Refer back to the assessment guidelines and criteria? Find appropriate help if required?

But in hindsight I could have……. I would have allocated more time to researching more in-depth capabilities of the scripts I already know Handing in the assessment

Were you able to: Present the assessment in the appropriate format? Submit on time? Complete the assignment front sheet (and keep your copy as evidence of submission)?

Provide the appropriate information (e.g. student number, Module name, Tutor name, etc)?

But in hindsight I could have……. Completed the assignment sooner before the deadline date

Page 32: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

What parts of the task do I feel I did well in: I feel the best part of the task that I completed was creating the installation video tutorial for users to follow when installing SQL Server at home. What parts of the task do I feel I could improve on: I could probably have completed more additional extra tasks. e.g. making the link with the web site. What would I do differently next time: I would most probably put more time into research before planning.

What skills have I acquired during this assessment that I could use in the workplace: I have learnt how to create an instructional video. I have learnt a lot of new types of coding standards. I have learnt how to better plan my time with working.

Page 33: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

References All sources of information are documented here, including URL links to the sites that I got the information from

1. Mansha Nawaz Intranet Staff Page

http://outranet.scm.tees.ac.uk:8002/users/u0000706/

2. Library of free Database Models

http://www.databaseanswers.org/data_models/

3. Create Triggers http://msdn.microsoft.com/en-us/library/ms189799.aspx

4. Create Index http://www.1keydata.com/sql/sql-create-index.html

...

Page 34: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

Appendix

a) Hyperlinks to the sql database .mdf & .ldf files

Below are the two hyper links to the SQL database files on my intranet site as labelled; Alternatively you can obtain these files from the disk included with this document in “Database Files and Link”. Database .mdf file

Database .ldf file

b) All script files for the section 3 and 4

These can be located in their separate .sql files on the cd provided. Under “Database Files and Link” > “SQL”

b.1 CREATE Table Code --Create temp_users table CREATE TABLE temp_users( userId INT NOT NULL CONSTRAINT userIdPK PRIMARY KEY, userName VARCHAR(20) NOT NULL, firstNamet VARCHAR(30) NOT NULL, lastName VARCHAR(30) NOT NULL, email VARCHAR(50) NOT NULL, [password] VARCHAR(60) NOT NULL, gender VARCHAR(15) NULL, dob VARCHAR(10) NULL, userImage VARCHAR(50) NULL, regDate VARCHAR(10) NOT NULL, confirmCode INT NOT NULL, )

--Create users table CREATE TABLE users( userId INT NOT NULL CONSTRAINT userIdPK PRIMARY KEY, userName VARCHAR(20) NOT NULL, firstNamet VARCHAR(30) NOT NULL, lastName VARCHAR(30) NOT NULL, email VARCHAR(50) NOT NULL, [password] VARCHAR(60) NOT NULL, gender VARCHAR(15) NULL, dob VARCHAR(10) NULL, userImage VARCHAR(50) NULL, regDate VARCHAR(10) NOT NULL, aboutMe VARCHAR(200) NULL, )

--Create blog table CREATE TABLE blog( blogId INT NOT NULL CONSTRAINT blogIdPK PRIMARY KEY, title VARCHAR(35) NOT NULL, post VARCHAR(1000) NOT NULL, postDate VARCHAR(10) NOT NULL )

Page 35: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

--Create category table CREATE TABLE category( catId INT NOT NULL CONSTRAINT catIdPK PRIMARY KEY, catName VARCHAR(35) NOT NULL, catDescription VARCHAR(100) NULL ) --Create comments table CREATE TABLE comments( commentId INT NOT NULL CONSTRAINT commentIdPK PRIMARY KEY, comment VARCHAR(200) NOT NULL, commentDate VARCHAR(10) NOT NULL ) --Create user_blog table CREATE TABLE user_blog( userId INT NOT NULL CONSTRAINT userIdPK PRIMARY KEY, blogId INT NOT NULL CONSTRAINT blogIdPK PRIMARY KEY ) --Create blog_cat table CREATE TABLE user_blog( blogId INT NOT NULL CONSTRAINT blogIdPK PRIMARY KEY, catId INT NOT NULL CONSTRAINT catIdPK PRIMARY KEY ) --Create blog_com table CREATE TABLE blog_com( blogId INT NOT NULL CONSTRAINT blogIdPK PRIMARY KEY, commentId INT NOT NULL CONSTRAINT commentIdPK PRIMARY KEY )

b.2 DROP Table Code --Delete all tables DROP TABLE temp_users DROP TABLE users DROP TABLE blog DROP TABLE category DROP TABLE comments DROP TABLE user_blog DROP TABLE blog_cat DROP TABLE blog_com

Page 36: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

b.3 INSERT Code --Insert temp_users data INSERT INTO temp_users VALUES ('1','GarethW','Gareth','Williams','[email protected]', 'j4h53g45hg34hygr4g3uc4gr347yr3x','male','','','01-01-2010','654324567'); INSERT INTO temp_users VALUES ('2','SMN','Marie','Neil','[email protected]', 'ejhurg48rh834htrf3u4egburfg3w4r','female','','','01-01-2010','876312322'); INSERT INTO temp_users VALUES ('3','HM','Heinrich','Maure','[email protected]', 'u323bh4r23hr8u23vr2f3r89324brh34','','','','04-01-2010','342597422'); INSERT INTO temp_users VALUES ('4','Daniel21','Dan','Williams','[email protected]', '38947g5h43btrfi8fywgdwrui34hr893w','','','','05-01-2010','287424387'); INSERT INTO temp_users VALUES ('5','Alz','Allen','Lawsons','[email protected]', 'i3hnebu23grt789yh8ir2w343','male','','','05-01-2010','433234902'); INSERT INTO temp_users VALUES ('6','Becky','Becky','Proter','[email protected]', 'hu3hguieh2oiuh3r2382y98y3irhuiohi','','','','05-01-2010','348723862'); INSERT INTO temp_users VALUES ('7','JamesD','James','D','[email protected]', '3jirhj8rhfih4r8y34890rfjw','male','','','09-01-2010','908786521'); INSERT INTO temp_users VALUES ('8','PeterPiper','Peter','Piper','[email protected]', 'wdijw3r82rinhruyh34h84hr8f83whyfr8hn','','','','14-01-2010','620465692'); INSERT INTO temp_users VALUES ('9','StoryTeller','Perry','Kerr-Clark','[email protected]', 'hheu2r928uyr2wygru3y89ry289uhu','','','','15-01-2010','893744321'); --Insert users data INSERT INTO users VALUES ('1','GarethW','Gareth','Williams','[email protected]', 'j4h53g45hg34hygr4g3uc4gr347yr3x','male','','','01-01-2010',''); INSERT INTO users VALUES ('2','SMN','Marie','Neil','[email protected]', 'ejhurg48rh834htrf3u4egburfg3w4r','female','','','01-01-2010',''); INSERT INTO users VALUES ('3','HM','Heinrich','Maure','[email protected]', 'u323bh4r23hr8u23vr2f3r89324brh34','','','','04-01-2010','Its always sunnier in other parts of the world'); INSERT INTO users VALUES ('4','Daniel21','Dan','Williams','[email protected]', '38947g5h43btrfi8fywgdwrui34hr893w','','','','05-01-2010','Why do I need a degree when I have poker?'); INSERT INTO users VALUES ('5','Alz','Allen','Lawsons','[email protected]', 'i3hnebu23grt789yh8ir2w343','male','','','05-01-2010','I created a site for maplin');

Page 37: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

INSERT INTO users VALUES ('6','Becky','Becky','Proter','[email protected]', 'hu3hguieh2oiuh3r2382y98y3irhuiohi','','','','05-01-2010','Click Radio for the WIN!'); INSERT INTO users VALUES ('7','JamesD','James','D','[email protected]', '3jirhj8rhfih4r8y34890rfjw','male','','','09-01-2010','Somethings fishy around here'); INSERT INTO users VALUES ('8','PeterPiper','Peter','Piper','[email protected]', 'wdijw3r82rinhruyh34h84hr8f83whyfr8hn','','','','14-01-2010','Pickled Peppers'); INSERT INTO users VALUES ('9','StoryTeller','Perry','Kerr-Clark','[email protected]', 'hheu2r928uyr2wygru3y89ry289uhu','','','','15-01-2010',''); --Insert blog data INSERT INTO blog VALUES ('1','The World Around us','Yes its round and round and round and round and round and round and round and round and round and round and round and round and round and round and round and round and round','11-01-2010'); INSERT INTO blog VALUES ('2','The Boy who cried','WOLFWOLFWOLFWOLFWOLFWOLFWOLFWOLFWOLFWOLFWOLFWOLF','10-01-2010'); INSERT INTO blog VALUES ('3','A cute litte story','There one was a boy from Durham who fell in a buddle and was in such a muddle he never went back there again','13-01-2010'); INSERT INTO blog VALUES ('4','BLOGGER','I just wanted to see if this actually worked like it promised','11-01-2010'); INSERT INTO blog VALUES ('5','Testing','1...2..3.','08-01-2010'); --Insert category data INSERT INTO category VALUES ('1','Random','For anything short, sweet and to the point.'); INSERT INTO category VALUES ('2','True Story','A story that really happened to you or someone you know.'); INSERT INTO category VALUES ('3','Public Information','For something you feel everyone should know for their own good'); --Insert comments data INSERT INTO comments VALUES ('1','The world is a vampire','11-01-2010'); INSERT INTO comments VALUES ('2','What are you on about?','11-01-2010'); INSERT INTO comments VALUES ('3','I could ask you the same thing','11-01-2010'); INSERT INTO comments VALUES ('4','Youre both mad!','12-01-2010'); INSERT INTO comments VALUES ('5','That story always scared me','10-01-2010'); INSERT INTO comments VALUES ('6','Agreed Haha','12-01-2010');

Page 38: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

INSERT INTO comments VALUES ('7','Do you feel better for having done that','12-01-2010'); INSERT INTO comments VALUES ('8','KABOOM!!!','13-01-2010'); INSERT INTO comments VALUES ('9','I do :) It actually worked','14-01-2010'); INSERT INTO comments VALUES ('10','The ICA deadlies today! Dont panic!!!','15-01-2010'); --Insert user_blog data INSERT INTO user_blog VALUES ('3','4'); INSERT INTO user_blog VALUES ('8','3'); INSERT INTO user_blog VALUES ('9','2'); INSERT INTO user_blog VALUES ('6','1'); INSERT INTO user_blog VALUES ('5','5'); --Insert blog_cat data INSERT INTO blog_cat VALUES ('1','3'); INSERT INTO blog_cat VALUES ('2','1'); INSERT INTO blog_cat VALUES ('3','2'); INSERT INTO blog_cat VALUES ('4','3'); INSERT INTO blog_cat VALUES ('5','1'); --Insert blog_com data INSERT INTO blog_com VALUES ('1','1'); INSERT INTO blog_com VALUES ('1','2'); INSERT INTO blog_com VALUES ('1','3'); INSERT INTO blog_com VALUES ('1','4'); INSERT INTO blog_com VALUES ('2','5'); INSERT INTO blog_com VALUES ('1','6'); INSERT INTO blog_com VALUES ('4','7'); INSERT INTO blog_com VALUES ('5','8'); INSERT INTO blog_com VALUES ('4','9'); INSERT INTO blog_com VALUES ('2','10');

Page 39: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

b.4 INDEX Code --Create an index for a specified column CREATE INDEX I_Users_Userid on Users (Userid)

b.5 VIEW Code --Create View for Users table CREATE VIEW V_Users AS SELECT username, firstName, lastName, email FROM Users

b.6 TRIGGER Code

IF OBJECT_ID ('users.reminder', 'TR') IS NOT NULL DROP TRIGGER users.reminder; GO -- This trigger raises a message whenever a row is inserted or modified in Sales.Customer CREATE TRIGGER reminder ON users AFTER INSERT, UPDATE, DELETE AS EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Administrator', @recipients = '[email protected]', @body = 'Don''t forget the ICA is due in on the 15th of January', @subject = 'Reminder'; GO

b.7 SELECT & FROM Code

--SELECT all fields FROM the Users table SELECT * FROM Users

b.8 WHERE CLAUSE Code --SELECT all fields that meet the requirements FROM the Users table SELECT * FROM Users WHERE gender = 'male'

b.9 ORDER BY Code --ORDER BY returns sorts the users ascending SELECT * FROM Users WHERE gender = 'male' ORDER BY userName ASC;

b.10 DISTINCT Code --Distinct ruturns the unique Ids SELECT DISTINCT blogId FROM blog_com;

b.11 BETWEEN and LIKE Code --Between shows results between two values SELECT * FROM Users WHERE userId BETWEEN 2 AND 4; --Like shows results with a wildcard SELECT * FROM Users WHERE gender LIKE 'fe%';

Page 40: UNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING · PDF fileUNIVERSITY OF TEESSIDE SCHOOL OF COMPUTING HND ... idea in mind the concepts behind the database will make ... of Modelling when

Gareth Williams - H8017251

b.12 IN and NOT IN Code --The IN script looks for both criteria when searching SELECT * FROM Users WHERE gender IN ('male', 'female'); --The NOT IN script ignores anything with the criteria below SELECT * FROM Users WHERE gender NOT IN ('male', 'female');

b.13 Alias Code --The Alias allows you to change the name of a field for that query SELECT password AS gobbledygook FROM Users

b.14 Sub-Query Code --Sub-Query allows multiple quires in one (they are dependant) SELECT catName FROM category WHERE catId IN (SELECT catId FROM blog_cat WHERE catId = '1')

b.15 INSERT Code --Insert a new record into the table INSERT INTO temp_users VALUES ('11','Jen','Jenny','Williams','[email protected]', 'dwi3r4r53jth4r8h4w3r2gtge34ruhfs','female','','','01-01-2010','25432823');

b.16 UPDATE Code --Update a new record from the table UPDATE temp_users SET userName='Jenny', dob='26/08/1963' WHERE userName='Jen'

b.17 DELETE Code --Delete a record from the table DELETE * FROM Users WHERE userName='Jenny'