School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’...

91
Teesside University School of Computing Databases & SQL Case Study: Server Database for the ‘My Drift’ System Author: Kirsty Amos (D4142375) Module Leader: Mansha Nawaz Module Code: HSQ Date of Submission: 15th January 2010

Transcript of School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’...

Page 1: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Teesside University School of Computing

Databases & SQL

Case Study: Server Database for the ‘My Drift’ System

Author: Kirsty Amos (D4142375) Module Leader: Mansha Nawaz

Module Code: HSQ

Date of Submission: 15th January 2010

Page 2: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 2 of 91

ACKNOWLEDGMENTS I would like to thank Mansha Nawaz and Paul Massey for their guidance throughout this module. I am also grateful to ‘Team Sunset’ for allowing me to model the ‘My Drift’ system.

Page 3: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 3 of 91

CONTENTS 1 CASE STUDY............................................................................................. 5

1.1 Case Study Scenario........................................................................... 5 1.2 Additional Considerations & ERDs.................................................... 5 1.3 Deliverables.......................................................................................... 6

2 MS SQL SERVER DATABASE.................................................................. 7

2.1 Database Diagrams............................................................................. 7 2.1.1 Full ERD....................................................................................... 7 2.1.2 Partial ERD’s............................................................................... 8

2.1.2.1 Applications................................................................... 8 2.1.2.2 Users.............................................................................. 8 2.1.2.3 Logins............................................................................ 9 2.1.2.4 Teams............................................................................ 9 2.1.2.5 Events............................................................................ 10 2.1.2.6 Quotes............................................................................ 10 2.1.2.7 Suppliers........................................................................ 11 2.1.2.8 Products......................................................................... 11 2.1.2.9 Customers...................................................................... 12 2.1.2.10 Orders........................................................................... 12 2.1.2.11 Returns......................................................................... 13

2.2 Management Studio............................................................................. 14 2.2.1 Tables with sample data............................................................. 14

2.2.1.1 Applications................................................................... 14 2.2.1.2 Users............................................................................... 15 2.2.1.3 Logins............................................................................. 16 2.2.1.4 Teams............................................................................. 17 2.2.1.5 Events............................................................................. 18 2.2.1.6 Quotes............................................................................. 19 2.2.1.7 Suppliers......................................................................... 20 2.2.1.8 Products.......................................................................... 21 2.2.1.9 Customers....................................................................... 22 2.2.1.10 Orders............................................................................ 23 2.2.1.11 Returns.......................................................................... 24

2.2.2 Views with sample data............................................................... 25 2.2.2.1 Users Vs Teams.............................................................. 25 2.2.2.2 Team Vs Event................................................................ 26 2.2.2.3 Orders Vs Returns.......................................................... 27 2.2.2.4 Orders Vs Suppliers....................................................... 28

3 DEMONSTRATION OF SQL CODE............................................................. 29

3.1 Data Definition Language (DDL).......................................................... 29 3.1.1 Create Table.................................................................................. 29 3.1.2 Alter Table..................................................................................... 30 3.1.3 Create Index.................................................................................. 31 3.1.4 Drop Table..................................................................................... 32 3.1.5 Constraint...................................................................................... 33 3.1.6 Create View................................................................................... 34 3.1.7 Create Trigger............................................................................... 37

Page 4: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 4 of 91

3.2 Data Manipulation Language (DML).................................................... 38

3.2.1 SELECT and FROM...................................................................... 38 3.2.2 WHERE Clause............................................................................. 40 3.2.3 ORDER BY.................................................................................... 41 3.2.4 DISTINCT...................................................................................... 42 3.2.5 BETWEEN and LIKE.................................................................... 43 3.2.6 IN and NOT IN.............................................................................. 45 3.2.7 Joining Tables............................................................................. 47 3.2.8 Aliases for Tables........................................................................ 48 3.2.9 Sub-Queries.................................................................................. 49 3.2.10 INSERT........................................................................................ 50 3.2.11 DELETE....................................................................................... 50 3.2.12 UPDATE...................................................................................... 51

4 SQL: SHOW UNDERSTANDING OF ADVANCE SERVER – CLIENT

FEATURES................................................................................................... 53 4.1 Connecting to Microsoft Access database......................................... 53 4.2 Import and Export of data between MS Access & MS SQL Server.. 58 4.3 Stored Procedure.................................................................................. 63 4.4 Connecting to MS Visual Web Developer........................................... 64 4.5 Attach and Detach SQL Server database............................................ 71

5 CRITICAL REVIEW...................................................................................... 76

5.1 Conclusion............................................................................................. 76 5.2 Review of ICA Assessment Criteria & Deliverables........................... 76 5.3 Review of Database............................................................................... 76 5.4 Review of Report................................................................................... 77 5.5 Review of Personal Development........................................................ 77

6 REFERENCES.............................................................................................. 78

7 APPENDICES............................................................................................... 79

7.1 Appendix A - All script’s for sections 3.............................................. 79 7.2 Appendix B - ‘My Drift’ Context Diagrams from Systems Analysis &

Design Report........................................................................................ 82 7.2.1 Users............................................................................................. 82 7.2.2 Admin............................................................................................ 82 7.2.3 Suppliers....................................................................................... 83 7.2.4 Customers..................................................................................... 83

7.3 Appendix C - Creation of the ‘My Drift’ SQL database...................... 84 7.4 Appendix D - Save changes not permitted......................................... 86 7.5 Appendix E - Incorrect SQL Database................................................. 87 7.6 Appendix F - Ascent ERD..................................................................... 88 7.7 Appendix G – Script Table As.............................................................. 89 7.8 Appendix H – Hyperlinks to .mdf and .ldl files................................... 91

Page 5: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 5 of 91

1 CASE STUDY

1.1 Case Study Scenario I have chosen to investigate and develop a server database for the ‘My Drift’ system. ‘My Drift’ is a system designed to enable those people involved with or interested in the motorsport of ‘Drifting’ to communicate with each other using the ‘World Wide Web’. Users can register themselves as Individuals, Teams or Business’s on the website. This includes Drivers, Mechanics, Officials, Judges and Spectators. Members have the facility to upload Photo’s/Videos, Advertise Events/Competitions, Sell Tickets for Events/Competitions, Run Mini Leagues, Update Points Tables, Buy/Sell Items and Interact with each other using the Forum. This text was taken from my own Systems Analysis & Design (SAD) Case Study which was submitted to the School of Computing on 12th May 2009. 

1.2 Additional Considerations & ERDs

The ‘My Drift’ system holds a vast amount of information including:

Members personal details Sales/Order processing Discussions & private messages posted via the Forum

Due to much of the data being private and confidential, a user name and password has been set up on the server database which only allows authorised personnel to access the data.

Page 6: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 6 of 91

1.3 Deliverables

Below are the skill areas I have developed during the implementation of the ‘My Drift’ system in MS SQL Server 2008 and throughout the duration of this module. Successful installation of ‘MS SQL Server 2008’ and troubleshooting skills

gained during the installation. A good working knowledge of standard SQL coding and some advanced

skills. Implementation of a well planned, large working database and

accompanying report. Full and Partial Entity Relationship Diagrams (ERD’s), showing tables and

their relationships. Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’ database and ‘MS

Access’ database. Successful installation and learning of ‘MS Visual Web Developer’ in order

to connect my SQL database to a website. Use of Ascent to create an ERD. Successfully Detach and Attach my database.

The screen shots throughout this report show my successes and failures when developing the above areas.

Page 7: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 7 of 91

2 MS SQL SERVER DATABSE

2.1 Database Diagrams 2.1.1 Full ERD of the ‘My Drift’ system

The above Entity Relationship Diagram (ERD) shows the main tables I have chosen to pursue for the ‘My Drift’ system. These are:

Applications Users Login Teams Events Quotes Suppliers Products Customers Orders Returns

Due to time constraints I chose not to create the below tables at this stage. This leaves room for future development of the system:

Themes Profiles Videos/Images Forum Banned Suppliers Banned Users Company Profile Team Secretaries

Page 8: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 8 of 91

2.1.2 Partial ERD’s of the ‘My Drift’ system The following Partial ERD’s are derived from the full ERD. They show the standard view of each table, their relationships and their Primary Keys. The Primary Keys are identified by the ‘key’ symbol to the left of the field name:

2.1.2.1 Applications ERD This ERD shows the relationship between ‘Applications’, which may then become ‘Users’.

2.1.2.2 Users ERD This ERD shows the relationship between ‘Users’ from the ‘Application’ stage, through to ‘Login’ and their ‘Team’ and ‘Events’ where applicable.

Page 9: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 9 of 91

2.1.2.3 Logins ERD This ERD shows the relationship between ‘Login’ and ‘Users’. ‘Logins’ are specific to ‘Users’ and do not share a relationship with any other tables.

2.1.2.4 Teams ERD This ERD shows the relationship between ‘Teams’, ‘Events’ and ‘Users’.

Page 10: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 10 of 91

2.1.2.5 Events ERD This ERD shows the relationship between ‘Events’, ‘Teams’ and ‘Users’. This diagram uses the same tables as the Teams ERD.

2.1.2.6 Quotes ERD This ERD shows the relationship between ‘Quotes’, ‘Customers’, ‘Products’ and ‘Suppliers’.

Page 11: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 11 of 91

2.1.2.7 Suppliers ERD This ERD shows the relationship between ‘Suppliers’, ‘Customers’, ‘Quotes’, ‘Products’ and ‘Orders’.

2.1.2.8 Products ERD This ERD shows the relationship between ‘Products’, ‘Supplier’s and ‘Quotes’.

Page 12: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 12 of 91

2.1.2.9 Customers ERD This ERD shows the relationship between ‘Customers’, ‘Orders’, ‘Suppliers’ and ‘Returns’.

2.1.2.10 Orders ERD This ERD shows the relationship between ‘Orders’, ‘Customers’, ‘Products’, ‘Suppliers’ and ‘Returns’.

Page 13: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 13 of 91

2.1.2.11 Returns ERD This ERD shows the relationship between ‘Returns’, ‘Orders’, ‘Customers’ and ‘Suppliers’.

Page 14: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 14 of 91

2.2 Management Studio 2.2.1 Tables In this section are the tables used in the ‘My Drift’ system. 2.2.1.1 Applications Table

Below is a screen shot of the data held in the Applications table, along with the ‘Insert’ script used to populate the data (on a row by row basis):

Page 15: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 15 of 91

2.2.1.2 Users Table

Below is a screen shot of the data held in the Users table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 16: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 16 of 91

2.2.1.3 Logins Table

Below is a screen shot of the data held in the Login table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 17: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 17 of 91

2.2.1.4 Teams Table

Below is a screen shot of the data held in the Teams table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 18: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 18 of 91

2.2.1.5 Events Table

Below is a screen shot of the data held in the Events table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 19: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 19 of 91

2.2.1.6 Quotes Table

Below is a screen shot of the data held in the Quotes table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 20: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 20 of 91

2.2.1.7 Suppliers Table

Below is a screen shot of the data held in the Suppliers table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 21: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 21 of 91

2.2.1.8 Products Table

Below is a screen shot of the data held in the Products table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 22: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 22 of 91

2.2.1.9 Customers Table

Below is a screen shot of the data held in the Customers table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 23: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 23 of 91

2.2.1.10 Orders Table

Below is a screen shot of the data held in the Orders table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 24: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 24 of 91

2.2.1.11 Returns Table

Below is a screen shot of the data held in the Returns table, along with the ‘Insert’ script used to populate this data (on a row by row basis):

Page 25: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 25 of 91

2.2.2 Views In this section are a number of queries I created to view specific data held in the database. These queries may be useful for reporting purposes. 2.2.2.1 Users Vs Teams Displays which Users belong to which Teams. Query saved in the database as ‘Users_V_Teams’. SQL Code: SELECT Teams.TeamName, Teams.SecretaryUserID, Users.UserID, Users.Forename, Users.Surname FROM Teams INNER JOIN Users ON Teams.SecretaryUserID = Users.UserID Results:

Page 26: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 26 of 91

2.2.2.2 Team Vs Event Displays which Team are organising which event. Query saved in the database as ‘Team_V_Event’. SQL Code: SELECT Events.Description, Events.Location, Events.Date, Teams.TeamName FROM Events INNER JOIN Teams ON Events.TeamID = Teams.TeamID Results:

Page 27: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 27 of 91

2.2.2.3 Orders Vs Returns Compares the Orders that have been placed against the items which have been returned – in this case all of the orders have been returned. Query saved in the database as ‘Orders_V_Returns’. SQL Code: SELECT Customers.CustomerID, Orders.OrderID, Orders.ProductDescription, Orders.OrderDate, [Returns].ReturnDate, [Returns].Reason FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN [Returns] ON Customers.CustomerID = [Returns].CustomerID AND Orders.OrderID = [Returns].OrderID Results:

Page 28: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 28 of 91

2.2.2.4 Orders Vs Suppliers Displays which Suppliers have had Orders placed with them, sorted by lowest price to highest. Query saved in the database at ‘Orders_V_Suppliers’. SQL Code:

SELECT Orders.OrderID, Suppliers.SupplierName, Products.ProductDescription, Products.Cost FROM Orders INNER JOIN Products ON Orders.ProductID = Products.ProductID AND Orders.ProductID = Products.ProductID INNER JOIN Suppliers ON Orders.SupplierID = Suppliers.SupplierID AND Products.SupplierID = Suppliers.SupplierID ORDER BY Products.Cost Results:

Page 29: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 29 of 91

3 DEMONSTRATION OF SQL CODE In this section I will demonstrate my understanding of writing SQL code to perform certain functions. 3.1 Data Definition Language (DDL) DDL statements are used to build and modify the structure of tables and other objects in the database. 3.1.1 Create Table Below is the code I wrote to create the ‘AllUsers’ table. This table was later ‘Dropped’.

Page 30: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 30 of 91

3.1.2 Alter Table Below is the code I wrote to add the column ‘CurrentCar’ to the ‘AllUsers’ table after it had been created.

Page 31: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 31 of 91

3.1.3 Create Index Below is the code I wrote to create an Indexed field on the Suppliers table. This ensures the uniqueness of the SupplierID field.

Page 32: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 32 of 91

3.1.4 Drop Table Below is the code I wrote to drop/remove the ‘AllUsers’ table.

.

Page 33: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 33 of 91

3.1.5 Constraint Below is the code I wrote to specify the ‘UserID’ field as the Primary Key when creating this table.

In the above screen shot ‘AllUsers’ in unlined in red. This is because the database I was connected to at that time was ‘Master’. In order for this query to work I had to change ‘Master’ to ‘MyDrift’ in the drop down box to the left of the ‘Execute’ button.

Page 34: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 34 of 91

3.1.6 Create View Below is the code I wrote to create a view.

Page 35: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 35 of 91

The view is stored in my database:

When I access the view and execute the query, it displays the Product Name for all items which cost £150 (see screen shot on next page).

Page 36: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 36 of 91

Page 37: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 37 of 91

3.1.7 Create Trigger Below is the code I wrote to create a ‘Trigger’. This trigger sends an email to ‘[email protected]’ when a new order is received.

Page 38: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 38 of 91

3.2 Data manipulation Language (DML) DML statements are used to work with the data in tables. 3.2.1 SELECT and FROM Below is a query I wrote to select all records from the ‘Applications’ table.

Page 39: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 39 of 91

Here is another example of using SELECT & FROM. In this query I have requested the top 5 rows from the Applications table and chosen only display the ApplicantID, Forename, Surname and Email.

Page 40: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 40 of 91

3.2.2 WHERE Clause Below is a query I wrote to find out the Email address of all Suppliers who’s County is ‘Essex’.

Page 41: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 41 of 91

3.2.3 ORDER BY Below is a query I wrote to display all Quotes, Ordered by Cost, from Lowest to Highest.

Page 42: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 42 of 91

3.2.4 DISTINCT Below is a query I wrote to display how you would retrieve unique data entries from the database. The data entries in my database are unique anyway so a similar query with the word ‘DISTINCT’ removed, would display the same results. However I find this is a useful tool in larger databases.

Page 43: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 43 of 91

3.2.5 BETWEEN and LIKE BETWEEN Below is a query I wrote to display all Events with a date between 1st April 2010 and 1st June 2010.

Page 44: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 44 of 91

LIKE Below is a query I wrote to display all Products with the word ‘NISSAN’ in the ProductDescription.

Page 45: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 45 of 91

3.2.6 IN and NOT IN IN Below is a query I wrote to display all Returns where the ‘Reason’ is ‘Does not fit’.

Page 46: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 46 of 91

NOT IN Below is a query I wrote to display all Returns where the ‘Reason’ is anything other than ‘Does not fit’.

Page 47: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 47 of 91

3.2.7 Joining Tables Below is a query I wrote which joins multiple tables. This is a very useful tool for retrieving data.

Page 48: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 48 of 91

3.2.8 Aliases for Tables Below is a query I wrote using the Alias function to display Customers who do not live in the same ‘County’ as CustomerID: ‘0000000004’ (Stockton on Tees). CustomerID: 0000000005 and 0000000008 also live in Stockton On Tees so are not displayed in the results of this query.

Page 49: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 49 of 91

3.2.9 Sub-Queries Below is a query I wrote to find the basic information of a Product for all those Suppliers who had 12 items of a product in stock.

Page 50: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 50 of 91

3.2.10 INSERT Please see section 2.2.1 for examples of Insert scripts used to populate data into my tables. Hyperlink below: #Hyp1 3.2.11 DELETE Below is a query I wrote to delete all Events with a ‘Location’ of ‘Knock Hill’.

Page 51: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 51 of 91

3.2.12 UPDATE Below is a query I wrote to correct a typing error I made in the ‘Reason’ field of ReturnID: 0000000007.

Page 52: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 52 of 91

Before Update Script Executed:

After Update Script Executed:

Page 53: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 53 of 91

4 SQL: SHOW UNDERSTANDING OF ADVANCE SERVER – CLIENT FEATURES

4.1 Connecting to Microsoft Access database The below screen shots show how I created a Microsoft (MS) Access database and connected it to my MS SQL Server database. I created a new ‘MS Office Access Project’, ensuring it was saved with the

file extension ‘.adp’

Page 54: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 54 of 91

I selected ‘Yes’ to connect to an existing SQL Server database.

I input the relevant details into the ‘Data Link Properties’ box and tested

the connection.

Page 55: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 55 of 91

The tables I set up in my SQL Server database now appeared in my

Access database.

With the link between the two databases set up successfully I am able to

edit/add data in either platform and see the results in both. For example, when populating data for the ‘Customers’ table in

SQL Server, I input the same email address for two customers by mistake.

Page 56: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 56 of 91

I corrected the data in the field using MS Access and saved the

changes

I refreshed my SQL Server database and the changes had filtered through.

Page 57: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 57 of 91

MS Access 2007 is not fully compatible with MS SQL Server 2008 so not all actions I performed were saved. To solve this issue I would need to convert my Access database to a 2002-03 version. Due to time constraints I did not pursue this any further.

For example, when creating a Stored Procedure using Access I was

advised that my changes would not be saved

Page 58: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 58 of 91

4.2 Import and Export of data between MS Access & MS SQL Server In this section I look at Importing and Exporting my data between MS Access and MS SQL Server. The screen shots below show the process I followed. In MS SQL Server, I right clicked over the ‘MyDrift’ database and navigated

to the ‘Import Data...’ option.

I then followed the steps in the ‘Import & Export’ wizard.

Page 59: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 59 of 91

I selected ‘Microsoft Access’ from the drop down menu for the ‘Data

source’. I then ‘Browsed’ to locate my Access database file name, I input my ‘User name’ and ‘Password’.

I clicked on the ‘Advanced’ button in order to test the connection before

proceeding any further.

Page 60: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 60 of 91

The test connection failed. I performed a ‘Google’ search for the error message I received.

‘Test connection failed because of an error initializing provider. Cannot start your application. The workgroup information file is missing or opened exclusively by another user.’

After investigating a few options I found a feasible solution so set about implementing the fix. I was to update the ‘Property Value’ of the OLEDB Database Password.

http://forums.devx.com/archive/index.php/t-45626.html [10th January 2010]

Page 61: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 61 of 91

With the Property Value updated I tested the connection again, only to find another issue, this time with the file extension of the Access database.

I re-saved my Access database in both .dbo and .mdb formats in attempt to rectify this error but neither of these solved the issue.

I also attempted to ‘Export’ my data from SQL Server into Access but encountered the same problems. I decided not to spend any more time on this issue and moved onto the next section.

Page 62: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 62 of 91

4.3 Stored Procedure The below screen shots show how I set up a Stored Procedure. Utilising the script from document ‘15.sql.QA.ProcedureToScriptYourData’ on Mansha Nawaz staff pages. In SQL Server, create ‘New Query’, copy and paste the script from page 7

onwards in the above mentioned document and ‘Execute’ the query.

Once the Stored Procedure was in place I selected the ‘Events’ table and

opened a ‘New Query’ window. I typed in the code: EXEC sp_generate_inserts 'Events'

Page 63: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 63 of 91

Upon ‘Executing’ this query, insert scripts were created and displayed in

the results window.

By right clicking and saving these results to my computer, I can re-create the Events table at a later date if necessary. Repeating this process or all the tables means I would be able to re-create the whole database if and when required.

Page 64: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 64 of 91

4.4 Connecting to MS Visual Web Developer In order to create a website to connect to my SQL Server database, first I had to install ‘MS Visual Web Developer’. Before this, I also had to install ‘Web Platform Installer 2.0’. I downloaded both programs free from Microsft.com.

The Programs took less than 30 minutes to install.

Page 65: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 65 of 91

Once the programs were installed, I was able to start work on my website. I only wanted a basic web page to attach to the server database. I chose to model the ‘Events’ table. I created a ‘New Web Site’ called ‘MyDrift’:

I created a ‘Master Page’:

Page 66: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 66 of 91

I then created a ‘Style Sheet’:

I created a basic page for collecting ‘Events’ data:

Page 67: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 67 of 91

Once I was happy with the look of the page, I created an ‘Events’ table which is a mirror image of the one in my SQL Server database:

I then created an Admin. Page and dragged the ‘Events’ table across into this. This page can be used by the admin team to view/update information. It doesn’t look very pretty but a few minutes spent tweaking this could improve it drastically.

The ‘Connection String’ to my SQL server database was automatically created, I located this in the web.config file. Visual Basic (VB) code below: <connectionStrings> <add name="MyDriftConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDrift.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/> </connectionStrings>

Page 68: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 68 of 91

Next I input the ‘VB’ code (below) for the ‘Submit’ button. This code connects to the SQL database, transfers the data from the Web Page to the SQL database and displays either a confirmation or problem page to the user. Partial Class MyDriftMaster Inherits System.Web.UI.MasterPage Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim MyDriftDataSource As New SqlDataSource() MyDriftDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("MyDriftConnectionString1").ToString() MyDriftDataSource.InsertCommandType = SqlDataSourceCommandType.Text MyDriftDataSource.InsertCommand = "INSERT INTO Events (EventID, Date, Description, UserID, TicketPrice, Location, TeamID) VALUES (@EventID, @Date, @Description, @UserID, @TicketPrice, @Location, @TeamID)" MyDriftDataSource.InsertParameters.Add("EventID", EventIDTB.Text) MyDriftDataSource.InsertParameters.Add("Date", DateTB.Text) MyDriftDataSource.InsertParameters.Add("Description", DescriptionTB.Text) MyDriftDataSource.InsertParameters.Add("UserID", UserIDTB.Text) MyDriftDataSource.InsertParameters.Add("TicketPrice", TicketPriceTB.Text) MyDriftDataSource.InsertParameters.Add("Location", LocationTB.Text) MyDriftDataSource.InsertParameters.Add("TeamID", TeamIDTB.Text) Dim rowsAffected As Integer = 0 Try rowsAffected = MyDriftDataSource.Insert() Catch ex As Exception Server.Transfer("EventProblem.aspx") Finally MyDriftDataSource = Nothing End Try If rowsAffected <> 1 Then Server.Transfer("EventProblem.aspx") Else Server.Transfer("EventConfirm.aspx") End If End Sub End Class

Page 69: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 69 of 91

Before executing the code I had to enable de-bugging:

  

Upon execution, the data from my ‘Events’ web page was displayed. I input some data into the fields and clicked on ‘Submit’:

 

 

Page 70: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 70 of 91

Unfortunately the data didn’t filter through to my SQL Server database.    I returned to Visual Web Developer to investigate the problem. However due to this being the first time I had used this programme, my subsequent lack of knowledge/experience meant I spent a long time looking at possible issues. Ultimately I was unsuccessful at identifying the error. And I am hugely disappointed that I was unable to get this working.

Page 71: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 71 of 91

4.5 Attach and Detach SQL Server database The below screen shots show how I detached and attached my SQL Server database in order to use it on another machine. Also to copy it onto a DVD to hand in for my ICA. First I had to take the database ‘Offline’. By right clicking the database, going to ‘Tasks’ and choosing the option ‘Take Offline’.

A pop up box appeared confirming the database was ‘Offline’.

Page 72: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 72 of 91

The next step was to ‘Detach’ the database, again by right clicking the database, going to ‘Tasks’ but this time choosing the ‘Detach’ option.

 

Page 73: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 73 of 91

 On the next screen I clicked ‘OK’.

 

  

I then performed a search in my PC’s C:\ drive to find out where the ‘My Drift’ files had been detached to.

 

 

Page 74: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 74 of 91

The My Drift files were located in: C:\Program Files\MicrosoftSQLServer\MSSQL10.MSSQLSERVER\MSSQL\DATA. I then copied the .mdf and .ldl files to my memory stick.

 

  

Next I logged onto PC ‘SCM-69132’ in room OL.9 at Teesside University. I transferred the .mdf and .ldl files from my memory stick onto the desktop of the above mentioned machine. I then opened MS SQL Server 2008 and right clicked over ‘Databases’ then clicked ‘Attach’. I browsed for the ‘My Drift’ files and clicked ‘OK’ to attach the ‘My Drift’ system.

 

   

Page 75: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 75 of 91

Once this was complete the ‘My Drift’ database appeared in the list of databases on the left hand side.

 

  

Before saving my database to a DVD (in order to hand in as part of my ICA) I took this opportunity to make a backup of the database onto the desktop of the SCM machine in case anything went wrong in the transfer to the DVD.

  

Finally I transferred the .mdf, .ldl and backup files onto a DVD.

Page 76: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 76 of 91

5 CRITICAL REVIEW 5.1 Conclusion

The purpose of this exercise was to implement an SQL Server database which could be used in conjunction with an online system called ‘My Drift’. Users of the system have the facility to upload Photo’s/Videos, Advertise Events/Competitions, Sell Tickets for Events/Competitions, Run Mini Leagues, Update Points Tables, Buy/Sell Items and Interact with each other using the Forum. The nature of the ‘My Drift’ system has allowed me to be creative and is potentially a very large system. Due to time constraints I decided not to model the entire system and concentrated on developing the main areas. As you can see from this report I have experimented with SQL script (both DDL and DML) in order to implement the ‘My Drift’ system using SQL Server 2008. This was my first encounter with SQL Server and subsequent SQL coding, I have thoroughly enjoyed the learning process and hope to re-visit this area in the future in order to further my knowledge. 5.2 Review of ICA Assessment Criteria & Deliverables. Initial issues with the installation of MS SQL Server 2008 on my home PC meant I was unable to spend as much time on the ‘Advance Server Client Features’ as I would have liked. I eventually installed SQL Server on my employer’s office laptop. This meant sharing the laptop over evenings and weekends with colleagues, which wasn’t ideal as it restricted the amount of time I spent on the project. I ensured the main points from the ICA specification were covered then spent the remaining time evidencing the advance features I attempted. I encountered some issues when attempting the advance features which slowed me down, so again, given more time I would have like to pursue these further. When investigating the advanced features I also had to learn how to use systems such as ‘MS Visual Web Developer’, these areas are not covered in the module content so the learning process was slow but I found it very beneficial. 5.3 Review of Database Many hours were spent creating tables and populating the tables with data. I felt it was important for the integrity of the system to use creditable data. I created all the tables first before using Insert scripts to input the data. The Insert scripts had to be performed in a certain order, due to the relationships I had set up between the tables. At first this was a little annoying but it reassured me that the way I set the system up was correct. It would have been a good idea to have some data entries which were not so unique, so when I perform ‘Select Distinct’ script my results were more obvious. When creating my database I used the information from my ‘Systems Analysis and Design (SAD)’ module, a few improvements were required, such as:

Accepted Apps and Rejected Apps became one table called ‘Applications’ with a date field for either accepted/rejected.

‘Stock’ was merged with ‘Products’. ‘Purchase Orders’ became ‘Orders’. ‘Supplier Returns’ became ‘Returns’. A ‘Team Secretaty’ table was added.

Page 77: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 77 of 91

Replaced ‘UserID’ with SecretaryUserID’ in the Teams table and linked ‘SecretaryUserID’ to ‘UserID’ table.

Added ‘Quantity’ to the ‘Orders’ table.

I recognise that improvements could be made to the ‘Logins’ table. For example the ‘Date’ and ‘Time’ fields could be merged and the data type ‘smalldatetime’ would improve the table. Also the addition of an ‘IP Address’ field could be beneficial. Further improvements include adding more constraints to my tables and setting validation for fields such as ‘Post Code’ and ‘Telephone/Fax Numbers’.

5.4 Review of Report On reflection, my report contains a lot of screen shots, maybe more than necessary. Perhaps inserting the SQL script into the report rather than a screen shot of the script (and the results in SQL Server) may have taken up fewer pages. However, I wanted to prove that I was writing and executing the code in SQL Server and found screen shots the best way of evidencing this. Altogether I am happy with the report but disappointed that I couldn’t get all of the advanced functions working properly.

5.5 Review of Personal Development When investigating the advanced features I had to learn how to use systems such as ‘MS Visual Web Developer’ as these areas are not covered in the module content, this slowed me down but I found the process very beneficial. I utilised on-line video tutorials to help me with ‘MS SQL Server 2008’ and performed internet search’s to help with the SQL script. I used ‘Ascent’ to draw an ERD (Appendix F) but was unfamiliar with the functions for drawing ERD’s - in the past I have only used Ascent for Context Diagrams and Data Flow Diagrams. I now feel confident I can set up a basic server database including tables, view and queries.

Page 78: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 78 of 91

6 REFERENCES

http://archives.postgresql.org/pgsql-sql/2006-10/msg00140.php PostgreSQL [30th December 2009]

http://forums.devx.com/archive/index.php/t-45626.html [10th January 2010]

‘https://outranet.scm.tees.ac.uk/users/u0000706/’ Mansha Nawaz – Staff pages [Accessed on numerous occasions during this module] http://stackoverflow.com/questions/176514/what-is-the-difference-between-nchar10-and-varchar10-in-mssql Luke at Stackoverflow.com [30th December 2009] http://www.google.com/search?q=google&sourceid=ie7&rls=com.microsoft:en-US&ie=utf8&oe=utf8 [10th January 2010]

http://www.microsoft.com/express/vwd/ [10th January 2010] http://www.microsoft.com/web/gallery/install.aspx?appid=VWD [10th January 2010]

‘MS SQL Server Installation’ Kirsty Amos [27th November 2009] ‘My Drift’ System by Kirsty Amos [12th May 2009]

‘Report Writing’ by Anne Siedle [3rd May 2009]

Page 79: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 79 of 91

7 APPENDICES

7.1 Appendix A – All script’s for section 3 An example of each script from section 3 is saved in my database and can be found by following: File> Open File> Projects\MyDrfit\MyDrift:

The scripts are also below:

Create Table: Create table Customer (CustomerID nchar(10), UserID nchar(10) , HouseBuildingNumber nchar(10), Street varchar(50) , Town varchar(50), County varchar(50), PostCode nchar(10), Telephone numeric(18,0), Email varchar(50), Fax numeric(18,0), -- set primary key constraint Customer_key primary key (CustomerID)) Alter Table: ALTER TABLE AllUsers ADD CurrentCar varchar(50) NULL Create Index: CREATE INDEX Supplier_Index ON Suppliers(SupplierID); Drop Table: DROP TABLE AllUsers;

Page 80: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 80 of 91

Constraint: CREATE TABLE AllUsers( UserID nchar(10) NOT NULL CONSTRAINT AllUsersPK PRIMARY KEY, Title char(10) NULL, Forename varchar(50) NOT NULL, Surname varchar(50) NOT NULL, Passwords char(10) NOT NULL, Email varchar(50) NOT NULL, CustomerID nchar(10) NULL

) Create View: CREATE VIEW Product_150 AS SELECT ProductName, Cost FROM Products WHERE Cost = 150; Create Trigger: CREATE TRIGGER Order_Received ON Orders FOR INSERT AS PRINT 'kirstyamos@hotmail,com' GO Select & From: SELECT TOP 5 ApplicantID, Forename, Surname, Email FROM Applications Where Clause: SELECT SupplierName, County, Email FROM Suppliers WHERE County = 'Essex' Order By: SELECT * FROM Quotes ORDER BY Cost Distinct: SELECT DISTINCT Teams.TeamID, TeamName, Date, Events.Location FROM Events Inner Join Teams on Teams.TeamID = Events.TeamID ORDER BY TeamID Between: SELECT * FROM Events WHERE Date BETWEEN '04/01/2010' AND '06/01/2010' Like: SELECT * FROM Products WHERE ProductDescription LIKE '%NISSAN%'

Page 81: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 81 of 91

In: SELECT * FROM Returns WHERE Reason IN ('Does not fit') Not In: SELECT * FROM Returns WHERE Reason NOT IN ('Does not fit') Joining Tables: SELECT Customers.CustomerID, Orders.OrderID, Orders.ProductDescription, Orders.OrderDate, [Returns].ReturnDate, [Returns].Reason FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN [Returns] ON Customers.CustomerID = [Returns].CustomerID AND Orders.OrderID = [Returns].OrderID Aliases: SELECT C2.CustomerID, C2.County FROM Customers C1, Customers C2 WHERE C1.CustomerID = '0000000004' AND C2.County <> C1.County; Sub-Queries: SELECT ProductID, ProductName, SupplierID FROM Products WHERE NumberInStock IN (SELECT NumberInStock FROM Products WHERE NumberInStock = 12) Insert: insert into Orders values ('0000000010','1','150','0000000111','Cobra Racing Seat - R','Drivers hard back, non adjustable seat in Red','01/04/2010','0000000010','0000000005','0000000010','1492774466','[email protected]','54','Richardson Road','Grimsby','Hull','HL4 7FS','DHL','Visa'); select * from Orders Delete: DELETE FROM Events WHERE Location = 'Knock Hill' Update: UPDATE Returns SET Reason = 'One Wheel Damaged' WHERE ReturnID = '0000000007' Run Stored Procedure – Insert Events data: EXEC sp_generate_inserts 'Events' I have not displayed the code to create the full Stored Procedure as it is many pages long. However it can be found saved in my SQL database as ‘STORED_PROCDURE_SCRIPT’.

Page 82: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 82 of 91

7.2 Appendix B - ‘My Drift’ Context Diagrams from Systems Analysis &

Design Report 7.2.1 Users

7.2.2 Admin

Page 83: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 83 of 91

7.2.3 Suppliers

7.2.4 Customers

Page 84: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 84 of 91

7.3 Appendix C – Creation of the ‘My Drift’ SQL database The following screen shots show how I set up the ‘My Drift’ database once SQL Server 2008 was installed. Once connected to my server, I right clicked over ‘Databases’ and clicked on ‘New Database’:

I then gave my database a name (MyDrift) and clicked ‘OK’:

The ‘My Drift’ database appeared in the Object Explorer. I was then able to start adding Tables and creating Database Diagrams:

Page 85: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 85 of 91

Along with SQL code you can also add tables manually, by right clicking on ‘Tables’ and selecting the ‘New Table’ option. However the purpose of this module is to demonstrate SQL code so this method should be avoided.

Page 86: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 86 of 91

7.4 Appendix D – Save changes not permitted After creating my tables I tried to edit some data and got the following error message:

I searched for a solution using the course material provided and found that if I followed the below process, I would be able to edit my tables and wouldn’t get this error again. Access the ‘Tools’ Menu, go to ‘Options’ expand the ‘Designers’ section, click on ‘Table and Database Designers’ and remove the tick from the 6th checkbox (Prevent saving changes that require table re-creation).

Page 87: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 87 of 91

7.5 Appendix E - Incorrect SQL Database On occasions when writing queries I found that my tables and fields names were not recognised by the system. This was because I was in the ‘Master’ database instead of in the ‘My Drift’ database. This may have occurred whilst opening and closing the database in between writing queries. In order to rectify this, I selected ‘MyDrift’ from the drop down menu as shown below:

Page 88: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 88 of 91

7.6 Appendix F – Ascent ERD Below is an Entity Relationship Diagram (ERD) I have created using Ascent software. I created this diagram at the start of this project to help me understand the relationships between tables. This diagram could now be updated and improved.

Page 89: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 89 of 91

7.7 Appendix G – Script Table As Another function I have learned about in SQL Server 2008 is the ‘Script Table As’. If you are unfamiliar with SQL script, this allows you to manually perform a function e.g. create a table using the ‘Right click’, ‘New Table’ method. Once the table is created you can ‘Right click’ over the table and click ‘Script Table As’ and the SQL script will be generated for you.

The script generated for the ‘Applications’ table is: USE [MyDrift] GO /****** Object: Table [dbo].[Applications] Script Date: 01/11/2010 17:50:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Applications]( [ApplicantID] [nchar](10) NOT NULL, [Title] [char](10) NOT NULL, [Forename] [varchar](50) NOT NULL, [Surname] [varchar](50) NOT NULL, [Email] [varchar](50) NOT NULL, [UserID] [nchar](10) NOT NULL, [AcceptedDate] [date] NULL, [RejectedDate] [date] NULL, CONSTRAINT [PK_Applications] PRIMARY KEY CLUSTERED ( [ApplicantID] ASC

Page 90: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 90 of 91

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[Applications] WITH CHECK ADD CONSTRAINT [FK_Applications_Users] FOREIGN KEY([UserID]) REFERENCES [dbo].[Users] ([UserID]) GO ALTER TABLE [dbo].[Applications] CHECK CONSTRAINT [FK_Applications_Users] GO

Page 91: School of Computing Databases & SQL Case Study: Server ... · Connecting my ‘SQL Server’ database to a ‘MS Access’ database. Import and Export of data between ‘SQL Server’

Kirsty Amos (D4142375)

Page 91 of 91

7.8 Appendix H – Hyperlinks to .mdf and .ldl files Below are links to the My Drift system .mdf and .ldl files, where they are saved on the laptop I used to develop the system. I don’t envisage these links working from any machine other than the laptop were the data files are stored.

..\..\..\..\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\MyDrift.mdf ..\..\..\..\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\MyDrift_log.ldf