Professional Documents
Culture Documents
TASKS
The following Case Study
a. Derive an Entity Relationship Diagram (ERD) showing the entities of interest and
associated attributes, relationships, dependency and indicate primary and foreign
keys
b. Implement the tables in MS SQL Server 2005 by draw the ERD in MS SQL Server 2005
c. Populate the tables with the sample data.
d. Provide sample SQL code to demonstrate and understanding of DDL and DML SQL
statements.
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.
MODEL SOLUTIONS
Solution Exercise 1 Produce a Car-Hire ERD.
Solution Exercise 2 Create the physical schema for Car-Hire ERD.
Script file : carhire.schema.sql
Solution Exercise 3 Create the logical schemas for Car-Hire ERD.
Script file : contract.schemaview
Solution Exercise 4 Create a trigger for Car-Hire ERD.
Script file : carhiretrigger.schema
Mansha Nawaz
Case Study 7:
Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script
ER Modeling
Exercise 1 Produce a Car-Hire ERD.
Car-hire Exercise
A car-hire company has decided to computerise its booking system. At present,
hire contracts details are recorded by hand on hire summary forms
Registration Number hyu 786j
Make of Car ford
Engine Size 1600
Customer No
Name
Hire Date
h67854e
j6785tgf
h67854
F. Bloggs
J.Smith
F.Bloggs
Model
Escort
Daily Hire Rate 42.00
Return Date
Price
13-may-2002 15-may-2002
16-may-2002 20-may-2002
21-may-2002 21-may-2002
84.00
210.00
42.00
Mansha Nawaz
Case Study 7:
Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script
SCHEMA - PHYSICAL
Exercise 2 Create the physical schema for Car-Hire 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 the QA interface and executed to create the
tables for the ERDs.
Complete the schema for the remaining tables in the Car-Hire ERD.
The schema for a customer table is provided above.
SCHEMA - LOGICAL
Exercise 3 Create the logical schemas for Car-Hire ERD.
SQL Server logical Schema are the SQL commands required for View Definitions.
These commands are loaded into the QA interface and executed to create the
views derived from the ERDs.
Consider what views are required and create the necessary schema views.
The following is an example of an external schema (view) to provide contract details, based on a
join between the tables customer, contract and car.
Mansha Nawaz
Case Study 7:
Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script
Customer No
Customer Name
Street
Town
County
Po
3451
SELWOOD CONSULTANCY LTD
MARINTET ROAD
THORNABY
CLEVELAND
TS17 0BB
Car Details:
Model
Mondeo
Contract Details:
reg_no,
date_out
date_in
gfr 564w
01-10-05
01-17-05
Write the SQL code for an external schema view for a CONTRACT to provide contract details which
is to be based on a join between the tables customer, contract, car
For reference purposes working example of SQL code has been provided in
contract.schemaview.sql and also in the appendix of this document.
Remember to write the SQL code first before consulting the sample SQL code provided. You may
also consider improving the SQL code provided.
TRIGGERS
General Constraints
The contract price must be derived from (Date_in-Date_out)*Daily_hire_rate.
A contract can be issued to a customer on a date other than date_out or date_in, so a
customer that hires the same car on different contract dates cannot have the same date_in
and date_out on the contracts.
For each customer the street must be in the town, the town in the county and the post code
must refer to the correct address details.
The number of cars involved in contracts over any hire period cannot exceed the number of
cars the company owns.
Mansha Nawaz
Case Study 7:
Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script
Review the trigger SQL code above before clicking on carhiretrigger.schema and
execute in MS SQL Server 2005 Management Studio.
What is the purpose of the above trigger?
Inserting Data
Exercise 5 Inserting data to a the car-hire example
Using MS SQL Management Studio New Query and add data to the car-hire tables using SQL
insert command.
Note any of the constraints set in place within the SQL commands generated for the above.
Mansha Nawaz
Case Study 7:
MODEL SOLUTIONS
Solution Exercise 1 Produce a Car-Hire ERD.
Hire_Rate
Car
Is_of_type
Is_costed_a
t
For_a
Contract
Takes_out_
a
Customer
Entities
Customer (Customer_no, name, street, town, county, post_code)
Car (reg_no, model, engine_size)
Contract (reg_no, customer_no, contract_date, date_out, date_in)
Type_of_car (model, make)
Hire_rate (engine_size, daily_hire_rate)
(customer_number,
customer_name,
street,
town,
county,
post_code,
registration_number,
model,
date_hired,
date_returned)
as select
contract.customer_no,
customer.name,
customer.street,
customer.town,
customer.county,
customer.post_code,
contract.reg_no,
car.model,
contract.date_out,
contract.date_in
from customer,contract,car
where
customer.customer_no=contract.customer_no and
contract.reg_no=car.reg_no
On Contract
For Insert
As
declare @customer_no char(4), @reg_no char(8),
@contract_date datetime, @date_in datetime, @date_out datetime
select @customer_no=customer_no from inserted
select @contract_date=contract_date from inserted
select @reg_no=reg_no from inserted
select @date_in=date_in from inserted
select @date_out=date_out from inserted
if exists (select * from contract where
customer_no=@customer_no and
reg_no=@reg_no and
contract_date<>@contract_date
and (date_in=@date_in or
date_out=@date_out))
begin
rollback tran
raiserror 50000 'conflict'
end