Download Exercise 1 – Produce an ERD for UniSpares PURCHASE ORDER

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

DBase wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Oracle Database wikipedia , lookup

Relational algebra wikipedia , lookup

Database wikipedia , lookup

Team Foundation Server wikipedia , lookup

Microsoft Access wikipedia , lookup

Tandem Computers wikipedia , lookup

Ingres (database) wikipedia , lookup

Btrieve wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Null (SQL) wikipedia , lookup

Database model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Relational model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Case Study 8: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script
UNISPARES EXAMPLE
MS SQL SERVER 2005
Management Studio – Create tables via SQL Script
UniSpares EXAMPLE
The following guide shows how to convert an entity relationship diagram (ERD) for
UniSpares to a SQL database using SQL script.
You will have experience ERDs in MS Access. The ERD is the diagram produced via the
relationship screen.




The purpose of this exercise is to derive an ERD on paper.
To use the ERD to derive a set of normalized tables.
To create the database by generate a SQL script file.
Write some basic SQL code to validate the data. To insert data into the database.
SQL Coding Exercises
ER Modelling
Exercise 1 – Produce an ERD for UniSpares PURCHASE ORDER (PO).
Exercise 2 – Produce an ERD for UniSpares INVOICE.
Exercise 3 –Create an ERD for UniSpares
SCHEMA - PHYSICAL
Exercise 4 – Create the physical schema for UniSpares ERD.
SCHEMA - LOGICAL
Exercise 5 – Create the logical schemas for UniSpares ERD.
Inserting Data
Exercise 6 – Inserting data to the UniSpares database.
MODEL SOLUTIONS
Solution Exercise 1 – Produce an ERD for UniSpares PURCHASE ORDER (PO).
Solution Exercise 2 – Produce an ERD for UniSpares INVOICE.
Solution Exercise 3 –Create an ERD for UniSpares
Solution Exercise 4 – Create the physical schema for UniSpares ERD.
Script file : Unispares.schema.sql
Solution Exercise 5 – Create the logical schemas for UniSpares ERD.
Script file : unispares.schemaview.PO.QA.sql
Script file : unispares.schemaview.Invoice.QA.sql
Script file : unispares.schemaview.PO.EM.sql
Solution Exercise 6 – Inserting data to the UniSpares database.
Script file :
Mansha Nawaz
UNISPARES EXAMPLE
Case Study 8: 1
Case Study 8: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script
UNISPARES EXAMPLE
ER Modeling
Exercise 1 – Produce an ERD for UniSpares PURCHASE ORDER (PO).
Unispares is a retailer of computer equipment. Unispares use a standard Purchase Order Form (Fig A)
when buying goods from its suppliers.
FIGURE (a) SAMPLE PURCHASE ORDER (PO) FORM
UNISPARES
PURCHASE ORDER (PO) FORM
Unispares Computer Ltd, 99 Borough RD, Middlesbrough, TS1 3BA
tel: 01642-123456 fax: 01642-654321
PO No. 002594
PO Date: 15.12.96
Supplier Name:
Supplier Address:
PO DETAILS :
PART#
CPU012
PRI6214
MON023
CON061
CPU015
KEY031
CPU072
SELWOOD CONSULTANCY LTD
KINGSDAEL HOUSE
MARINTET ROAD
PART DESCRIPTION
Pentium 100 16/1000
HP Deskjet 660C
Philips 14" Colour Monitor
HP Deskjet 660C Cartridge
Pentium 166 32/2000 CD
Extended Keyboard
Pentium 120 16/1000
COST£
175.00
125.00
45.00
20.00
700.00
6.00
400.00
Nett
Vat
Gross
QTY TOTAL
2
350.00
3
375.00
1
45.00
4
100.00
1
700.00
10
60.00
2
800.00
£
1685.00
£
294.88
£
1979.88
Payment Terms : COD/7days/30days/45days
In case of queries, please contact staff# 012 staff-name: FRED BLOGGS
ext. 321
TASKS
For the above form:
a. Identify all entities and allocate the main attributes indicating primary key(s).
b. Derive an Entity Relationship Diagram (ERD) showing the entities of interest and
associated attributes, relationships and dependency.
c. Draw the ERD in MS SQL Server 2005
d. List a set of tables derived from the ERD and indicate primary and foreign keys.
e. Implement the tables in MS SQL Server 2005.
f. Populate the tables with the sample data provided.
You may decide to modify the data requirements or generate additional data attributes to
assist in your design of the database. Write down details of any assumptions you have
made that you feel are important.
Mansha Nawaz
UNISPARES EXAMPLE
Case Study 8: 2
Case Study 8: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script
UNISPARES EXAMPLE
You may decide to follow the ERD’s in Answer Points.
Exercise 2 – Produce an ERD for UniSpares INVOICE.
Unispares is a retailer of computer equipment. Unispares use a standard Invoice (Fig B) when supplying
goods to its customers.
FIGURE (b) CUSTOMER INVOICE
UNISPARES INVOICE
Invoice No.
002594
Customer Name: University OF Teesside
Cust. Address : Borough Rd, Middlesbrough, TS1 3BA
Invoice Date: 15.12.96
Cust. Contact :
Cust PO.:
Delivery Date : 16.12.96
Delivery Area : 4
Staff# : 12
MARK
09123
Staff-Name : FRED BLOGGS
INVOICE DETAILS :
PART#
CPU012
PRI6214
PART DESCRIPTION
Pentium 100 16/1000
HP Deskjet 660C
QTY
2
5
UNIT£
250.00
100.00
Nett
£
Vat
£
Gross £
TOTAL
500.00
500.00
1000.00
175.00
1175.00
Terms of Business : COD/7days/30days/45days
TASKS
For the above form:
g. Identify all entities and allocate the main attributes indicating primary key(s).
h. Derive an Entity Relationship Diagram (ERD) showing the entities of interest and
associated attributes, relationships and dependency.
i. Draw the ERD in MS SQL Server 2005
j. List a set of tables derived from the ERD and indicate primary and foreign keys.
k. Implement the tables in MS SQL Server 2005.
l. Populate the tables with the sample data provided.
You may decide to modify the data requirements or generate additional data attributes to
assist in your design of the database. Write down details of any assumptions you have
made that you feel are important.
You may decide to follow the ERD’s in Answer Points.
Mansha Nawaz
UNISPARES EXAMPLE
Case Study 8: 3
Case Study 8: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script
UNISPARES EXAMPLE
Exercise 3 –Create an ERD for UniSpares
by merge the PURCHASE ORDER (PO) and INVOICE.
a. Merge the PO ERD and the INVOICE ERD to create an UNISPARES ERD.
(Remember to only include one occurrence of an entity on the diagram)
SCHEMA - PHYSICAL
Exercise 4 –Create the physical schema for UniSpares ERD.
SQL Server Physical Schema is the SQL commands required for Table
Definitions.
Create table Customer
(Customer_no char(4),
name varchar(30) ,
street varchar(30) ,
town varchar(30),
county varchar(30),
post_code char(8),
constraint customer_key primary key (customer_no),
constraint customer_numbers check (customer_no between '1111' and '9999'),
constraint post_codes check ((post_code like '[A-Z][A-Z][0-9][0-9] [0-9][A-Z][A-Z]') or
(post_code like '[A-Z][A-Z][0-9][ ] [0-9][A-Z][A-Z]')),
constraint countys check (county in ('Durham','North Yorkshire', 'Northumberland')));
These commands are loaded into into a New Script interface
executed to create the tables for the ERD’s.
and
Create a schema for the tables identified in the UniSpares ERD (ex 3
model).
a.
Run MS SQL Server 2005 and create a new database called UniSpares. Click on the
database in the Object Window pane before executing any of the SQL commands.
Failure to do so means all the created tables etc will appear in the master database.
b.
Click on the link below to automatically load the SQL code into a New Query
window
. The SQL code builds the above ERD and is also provided
in the appendix of this document.
Unispares.schema.sql
c.
Write and save a Unispares_Drop Query to remove the tables. Please note the drop
tables need to be in reverse order of the sequence in which they are created.
Mansha Nawaz
UNISPARES EXAMPLE
Case Study 8: 4
Case Study 8: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script
UNISPARES EXAMPLE
** Drop commands not provided for this exercise
d.
Examine the SQL code structure and contents of each table. Execute the SQL
Code by clicking
. Expand the UniSpares database – Tables and note
the tables have been created.
e.
Use the UniSpares ERD to check the structure and contents of each table, as well as
the relationships between them. Click on Database Diagrams and create a new
diagram to represent the ERD.
f.
Write and save a Unispares_Insert Query to insert data into the tables
** Insert commands not provided for this exercise
g.
Examine the SQL code structure and contents of inserting sample data into each
table. Execute the SQL Code by clicking
. Expand the UniSpares
database – Tables and note the tables have data inserted.
Select
each table.
and use the following commands to examine the content of
SELECT *
FROM tablename
h.
The SQL code Unispares.schema.sql for has been written in notepad. However
MS SQL Server 2005 can generate the SQL Script Code itself. Right click the
database UniSpares and Script Database > Create To > New Query Editor Window
. Compare the auto generated code with that written in notepad.
Compare, contrast and familiarise yourself with the SQL code.
SCHEMA - LOGICAL
Exercise 5 – Create the logical schemas for UniSpares ERD.
SQL Server logical Schema are the SQL commands required for View
Definitions.
These commands are loaded into a New Script interface
executed to create the views derived from the ERD’s.
and
Create the schema views for PO and INVOICES as seen in Ex 1.
Mansha Nawaz
UNISPARES EXAMPLE
Case Study 8: 5
Case Study 8: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script
UNISPARES EXAMPLE
a) Use Management Studio to create the views
b) Write your own SQL Code for UniSpares view schemas in Notepad /
MS Word.
I. Load and execute in MS SQL Server 2005 – Management Studio.
II. Validate your code and remove any errors.
Inserting Data
Exercise 6 – Inserting data to the UniSpares database
Using the INSERT command write an insert schema to load the data in the
PO and INVOICES forms as shown in Ex 1.
Mansha Nawaz
UNISPARES EXAMPLE
Case Study 8: 6
MS SQL – QUERY ANALYSER – UNISPARES SOLUTIONS
MODEL SOLUTIONS
Solution Exercise 1 – Produce an ERD for UniSpares PURCHASE ORDER (PO).
a. Identify all entities and allocate the main attributes indicating primary key(s).
ORDERS(po#, odate)
SUPPLIERS(sname, saddress, …….)
PARTS(p#, pdesc, …..)
PAYMENT-TERMS(pt#, ptdesc)
CONTACT(ct#, cname, cext)
b. and c.
Entity Relationship Diagram (ERD)
ASCENT DIAGRAM : ER Model for
PURCHASE ORDERS
PO-ERD in MS SQL - EM
MS SQL – QUERY ANALYSER UNISPARES SOLUTIONS by Mansha Nawaz
7
MS SQL – QUERY ANALYSER – UNISPARES SOLUTIONS
c. List a set of tables derived from the ERD and indicate primary and foreign keys.
PURCHASE-ORDERS(@po#, odate, s#, pt#, ct#)
SUPPLIERS(@s#, sname, saddress, …….)
PARTS(@p#, pdesc, …..)
PO-PARTS(@po, @p#, qty)
PAYMENT-TERMS(@pt#, ptdesc)
CONTACT(@ct#, cname, cext)
For reference purposes note how the data is inserted into the tables
Populate your skeleton tables using the data provided in the above order form.
PURCHASE-ORDERS
(po#,
odate
002594
15.12.96
s#
pt#
000001 1
SUPPLIERS
(s#,
000001
sname,
Selwood Consultancy
PARTS
(p#,
CPU012
PRI6214
MON023
CON061
CPU015
KEY031
CPU072
pdesc, …..)
Pentium 100 16/1000
HP Deskjet 660C
Philips 14" Colour Monitor
HP Deskjet 660C Cartridge
Pentium 166 32/2000 CD
Extended Keyboard
Pentium 120 16/1000
ct#)
01
saddress, …….)
Kingsdale House
PAYMENT-TERMS
(pt#,
ptdesc)
1
COD
2
7 days
3
30 days
MS SQL – QUERY ANALYSER UNISPARES SOLUTIONS by Mansha Nawaz
8
MS SQL – QUERY ANALYSER – UNISPARES SOLUTIONS
4
45 days
CONTACT
(c#,
01
cname,
BLOGGS F.
PO-PART
(p#,
CPU012
PRI6214
MON023
CON061
CPU015
KEY031
CPU072
QTY,
2
3
1
4
1
1
2
cext)
321
po#)
002594
002594
002594
002594
002594
002594
002594
The above data set is only a partial view. Additional orders can be generated and added to the database. The
following shows how the sample order (figure A-2) affects our database.
Note : No REDUNDANT or DUPLICATED data in tables which show strong data INTEGRITY.
TO GENERATE AN ADDITIONAL PURCHASE ORDER
FIGURE (a2) SAMPLE PURCHASE ORDER (PO) FORM
UNISPARES ORDER FORM
Unispares Computer Ltd
99 Borough RD, Middlesbrough, TS1 3BA
tel: 01642-123456 fax: 01642-654321
Unispares Purchase Order No. 002595
Order Date: 15.12.96
Supplier Name: JOE BLOGGS Computers
Address:
LINTHORPE RD
MIDDLESBROUGH
CLEVELAND
TS1 3BA
ORDER DETAILS :
ITEM# PART# PART DESCRIPTION
1
CPU012
Pentium 100 16/1000
2
PRI6214
HP Deskjet 660C
3
MON023
Philips 14" Colour Monitor
4
CON062
HP Deskjet 690C Cartridge
5
CPU016
Pentium 200 32/2000 CD
6
CPU074
Pentium Pro 150 16/1000
QTY
10
5
4
4
1
2
Payment Terms : COD/7days/30days/45days
In case of queries, please contact : HARRY SMITH
ext. 322
Populate your skeleton tables using the data provided in the above order form.
PURCHASE-ORDERS
MS SQL – QUERY ANALYSER UNISPARES SOLUTIONS by Mansha Nawaz
9
MS SQL – QUERY ANALYSER – UNISPARES SOLUTIONS
(po#,
002594
002595
odate
15.12.96
15.12.96
s#
pt#
000001 1
ct#)
01
000002 3
02
SUPPLIERS
(s#,
sname,
000001
Selwood Consultancy
000002
Joe Bloggs Computers
PARTS
(p#,
CPU012
PRI6214
MON023
CON061
CON062
CPU015
CPU016
CPU074
KEY031
CPU072
saddress, …….)
Kingsdale House……
Linthorpe Rd…..
pdesc, …..)
Pentium 100 16/1000
HP Deskjet 660C
Philips 14" Colour Monitor
HP Deskjet 660C Cartridge
HP Deskjet 690C Cartridge
Pentium 166 32/2000 CD
Pentium 200 32/2000 CD
Pentium Pro 150 16/1000
Extended Keyboard
Pentium 120 16/1000
PAYMENT-TERMS (pt#,
1
2
3
4
CONTACT (c#,
01
02
PO-PARTS
(p#,
CPU012
PRI6214
MON023
CON061
CPU015
KEY031
CPU072
CPU012
PRI6214
MON023
CON062
CPU016
CPU074
QTY,
2
3
1
4
1
10
2
10
5
4
4
1
2
ptdesc)
COD
7 days
30 days
45 days
cname,
BLOGGS FRED
SMITH HARRY
cext)
321
322
po#)
002594
002594
002594
002594
002594
002594
002594
002595
002595
002595
002595
002595
002595
For reference purposes only ... note the SQL code generated by Ascent2
MS SQL – QUERY ANALYSER UNISPARES SOLUTIONS by Mansha Nawaz
10
MS SQL – QUERY ANALYSER – UNISPARES SOLUTIONS
TABLES IN SQL FORMAT
Generated by ASCENT2
create table --contact-(
@ct# char(15) not null
ctext char(15)
)
create table --payterms-(
@pt# char(15) not null
pt-desc char(15)
)
create table --supplier-(
@s# char(15) not null
sname char(15)
saddress char(15)
)
create table --po-(
@po# char(15) not null
podate char(15)
s# char(15)
pt# char(15)
contact char(15)
)
create table --part-(
@p# char(15) not null
pdesc char(15)
)
create table --part-po-(
@part# char(15) not null
@po# char(15) not null
cost char (15)
qty char(15)
Solution Exercise 2 – Produce an ERD for UniSpares INVOICE.
ASCENT DIAGRAM :
ER Model for INVOICE
MS SQL – QUERY ANALYSER UNISPARES SOLUTIONS by Mansha Nawaz
11
MS SQL – QUERY ANALYSER – UNISPARES SOLUTIONS
INVOICES ERD in MS SQL - EM
MS SQL – QUERY ANALYSER UNISPARES SOLUTIONS by Mansha Nawaz
12
MS SQL – QUERY ANALYSER – UNISPARES SOLUTIONS
Solution Exercise 3 –Create an ERDs for UniSpares
by merge the PURCHASE ORDER (PO) and INVOICE.
Solution Exercise 4 – Create the physical schema for UniSpares ERD.
Script file : Unispares.schema.sql
Solution Exercise 5 – Create the logical schemas for UniSpares ERD.
Query Analyser Solutions
Script file : unispares.schemaview.PO.QA.sql
CREATE VIEW [Purchase Orders]
AS
SELECT PO.PO, PO.[date], PO.s#, supplier.name, supplier.street, supplier.town, supplier.county,
supplier.postcode, [po-parts].p#, parts.[desc], [po-parts].qty, PO.pt#,
payments.[desc], PO.ct#, contacts.name, contacts.ext
FROM
PO, supplier, parts, [po-parts], payments, contacts
WHERE
PO.PO = [po-parts].po# AND
PO.s# = supplier.s# AND
[po-parts].p# = parts.p# AND
PO.pt# = payments.pt# AND
PO.ct# = contacts.ct#
GO
SELECT *
FROM [Purchase Orders]
MS SQL – QUERY ANALYSER UNISPARES SOLUTIONS by Mansha Nawaz
13
MS SQL – QUERY ANALYSER – UNISPARES SOLUTIONS
Script file : unispares.schemaview.Invoice.QA.sql
CREATE VIEW [Customer Invoices]
AS
SELECT invoices.invoice#, invoices.[date], invoices.c#, customers.name, customers.street, customers.town,
customers.county, customers.postcode, [inv-parts].p#, parts.[desc], [inv-parts].qty, invoices.pt#,
payments.[desc] AS paymenttpye, invoices.ct#, contacts.name AS contactname, contacts.ext
FROM
invoices, customers, parts, [inv-parts], payments, contacts
WHERE
invoices.invoice# = [inv-parts].invoice# AND
invoices.c# = customers.c# AND
[inv-parts].p# = parts.p# AND
invoices.pt# = payments.pt# AND
invoices.ct# = contacts.ct#
SELECT *
FROM [Customer Invoices]
Enterprise Manager
Script file : unispares.schemaview.PO.EM.sql
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE VIEW [PO View EM]
AS
SELECT PO.PO, [po-parts].po#, PO.[date], PO.s#, supplier.name, supplier.street, supplier.town, supplier.county,
supplier.postcode, [po-parts].p#, parts.p# AS popart, parts.[desc], [po-parts].qty, PO.pt#, payments.pt# AS
paymentterms, payments.[desc] AS paymentdesc, PO.ct#, contacts.ct# AS contactno, contacts.name AS
contactname, contacts.ext
FROM
PO INNER JOIN
payments ON PO.pt# = payments.pt# INNER JOIN
supplier ON PO.s# = supplier.s# INNER JOIN
[po-parts] ON PO.PO = [po-parts].po# INNER JOIN
parts ON [po-parts].p# = parts.p# INNER JOIN
contacts ON PO.ct# = contacts.ct#
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SELECT *
FROM [PO View EM]
Solution Exercise 6 – Inserting data to the UniSpares database.
Script file :
MS SQL – QUERY ANALYSER UNISPARES SOLUTIONS by Mansha Nawaz
14