You are on page 1of 9

Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script

CAR HIRE EXAMPLE

MS SQL SERVER 2005 Management Studio Create tables via SQL Script

CAR HIRE EXAMPLE

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.

SQL Coding Exercises


ER Modelling Exercise 1 Produce an ERD. SCHEMA - PHYSICAL Exercise 2 Create the physical schema for Car-Hire ERD. SCHEMA - LOGICAL Exercise 3 Create the logical schema for Car-Hire ERD. TRIGGERS Exercise 4 Creating triggers for data validation. Inserting Data Exercise 5 Inserting data to a the car-hire example

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

CAR HIRE EXAMPLE

Case Study 7:

Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script

CAR HIRE EXAMPLE

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 84.00 210.00 42.00

13-may-2002 15-may-2002 16-may-2002 20-may-2002 21-may-2002 21-may-2002

The following data is recorded for each booking. Booking# Car hires for Vehicle Registration Number: Make of car: Model: Engine size: Daily Hire Rate: Customer No: Customer Name: Customer Licence Details: Hire Date: Return Date: Price: Bookings are recorded in the order in which they are made. The price of the hire is calculated at the time of the booking from the daily hire-rate then current. Engine-size determines the current rate but rates are changed from time to time. 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.

You may decide to follow the ERDs in Answer Points.

Mansha Nawaz

CAR HIRE EXAMPLE

Case Study 7:

Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script

CAR HIRE EXAMPLE

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. Write the SQL Car-Hire schema in Notepad or MS Word. Load and execute in MS SQL Server 2005 Management Studio Validate your code and remove any errors.

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

CAR HIRE EXAMPLE

Case Study 7:

Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script

CAR HIRE EXAMPLE

Car Hire Contract Car-Hire Services, 99 Borough Rd, Middlesbrough, TS1 3BA tel: 01642-123456 fax: 01642-654321 Customer Details: Customer No Customer Name Street Town County Po Model reg_no, date_out date_in 3451 SELWOOD CONSULTANCY LTD MARINTET ROAD THORNABY CLEVELAND TS17 0BB Mondeo gfr 564w 01-10-05 01-17-05

Car Details: Contract Details:

Write the SQL Car-Hire view schemas in Notepad / MS Word. Load and execute in MS SQL Server 2005 Management Studio. Validate your code and remove any errors.

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 CAR HIRE EXAMPLE Case Study 7:

Case Study 7: MS SQL SERVER 2005 Management Studio Create tables via SQL Script

CAR HIRE EXAMPLE

Exercise 4 Creating triggers for data validation.


Triggers are needed to deal with general integrity constraints (amongst other things). The following trigger should be viewed as a first attempt to address the constraintA 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. Does this constraint and trigger completely address the problem of overlapping hire periods? If not, rewrite the constraint and trigger.
create Trigger hire_period_conflict 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

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

CAR HIRE EXAMPLE

Case Study 7:

MS SQL QUERY ANALYSER CAR HIRE SOLUTIONS

MODEL SOLUTIONS
Solution Exercise 1 Produce a Car-Hire ERD.

Car-Hire Example (BASIC)


Data Model Type_of_car
Is_of_type For_a

Car
Is_costed_a t

Hire_Rate

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)

MS SQL QUERY ANALYSER CAR-HIRE SOLUTIONS by Mansha Nawaz

MS SQL QUERY ANALYSER CAR HIRE SOLUTIONS

Car-Hire Example (ASCENT Diagram)

MS SQL QUERY ANALYSER CAR-HIRE SOLUTIONS by Mansha Nawaz

MS SQL QUERY ANALYSER CAR HIRE SOLUTIONS

Solution Exercise 2 Create the physical schema for Car-Hire ERD. Script file : carhire.schema.sql
Create table Type_of_car (model varchar(20), make varchar(20), constraint cartype_key primary key (model)); Create table Hire_rate (engine_size integer, daily_hire_rate money, constraint rate_key primary key (engine_size)); Create table Car (reg_no char(8), model varchar(20), engine_size integer, constraint car_key primary key (reg_no), constraint is_costed_at foreign key (engine_size) references hire-rate, constraint is_of_type foreign key (model) references type_of_car, constraint registration_numbers check (reg_no in ('gfr 564w', 'way 765v','tfn 445v')), constraint models check (model in ('Mondeo', '406','Focus')), constraint engine_sizes check (engine_size in (1200, 1400, 1600, 1800, 2000, 2500))); Create table Contract (reg_no char(8), customer_no char(4), contract_date datetime, date_out datetime, date_in datetime, constraint contract_key primary key (reg_no, customer_no, contract_date), constraint for_a foreign key (reg_no) references car, constraint takes_out_a foreign key (customer_no) references customer);

MS SQL QUERY ANALYSER CAR-HIRE SOLUTIONS by Mansha Nawaz

MS SQL QUERY ANALYSER CAR HIRE SOLUTIONS

Solution Exercise 3 Create the logical schemas for Car-Hire ERD. create view contract_details Script file : contract.schemavie w
(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

Solution Exercise 4 Create a trigger Car-Hire ERD. create Trigger hire_period_conflict Script file : carhiretrigger.schema

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

MS SQL QUERY ANALYSER CAR-HIRE SOLUTIONS by Mansha Nawaz

You might also like