Case Study 9: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script BUS EXAMPLE MS SQL SERVER 2005 Management Studio – Create tables via SQL Script BUS EXAMPLE The following guide shows how to convert an entity relationship diagram (ERD) for BUS 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 a ERD. SCHEMA - PHYSICAL Exercise 2 – Create the physical schema for BUS ERD. CONSTRAINTS Inserting Data Exercise 3 – Review constraints for primary and foreign keys. Exercise 4 – Inserting data to a the BUS example Exercise 5 – workstation—server database MODEL SOLUTIONS Solution Exercise 1 – Produce a BUS ERD. Solution Exercise 2 – Create the physical schema for BUS ERD. Script file : BUS.schema.sql Solution Exercise 3 – Review constraints for primary and foreign keys. Solution Exercise 4 – Create the logical schemas for BUS ERD. FOR ICA PURPOSES THE ERD MUST BE FURTHER DEVELOPED. PROVIDE SQL CODE FOR NEW TABLES. CONSTRAINTS TO FULLY UTILISED WERE POSSIBLE. PROVIDE SQL CODE TO INSERT MEANINGFUL DATA INTO NEW TABLES PROVIDE ALL SQL CODE FOR LOGICAL VIEWS. ALL SQL DDL and DML CODE EXAMPLES TO BE PROVIDED. FOR HIGHER GRADE CONSIDER PRODUCING A FULL DEMO GUIDE OF EXCERSISE 5. THESE SHOULD ALL BE DOCUMENTED. PROVIDING EVIDENCE IN THE FORM OF SCREEN SHOTS DATA VIEW BEFORE SQL CODE IN QUERY ANALYSER RESULTING DATA VIEW. Mansha Nawaz BUS EXAMPLE Case Study 9: 1 Case Study 9: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script BUS EXAMPLE ER Modeling Exercise 1 – Produce a BUS ERD. A small bus company keeps details of the starting place, destination, and fare for all routes which it operates. Each route has a unique route number and is divided into a number of stages. A stage is identified by the names of the bus stops that mark its beginning and end. For example, route number 154 goes from Hartlepool-Greatham, Greatham-Wolviston, Wolviston-Billingham, and Billingham-Norton. Each stage may appear in a number of routes, and is given a different sequence number for each route. For example, Greatham-Wolviston is stage 2 on route 154, stage 3 on route 178 Billingham-Sunderland, and stage 6 on route 105 Stockton-Hartlepool. The distance in miles is kept for each stage. Each route is associated with a number of scheduled departures, each of which is identified by a day of the week and a time. The estimated arrival time at the destination is recorded for each departure. There may be a number of departures, for different routes, leaving at the same time?on the same day. For example, route 154 departs at 9.15 on Monday, 10.00 on Wednesday, and at 9.30 and 15.00 on Saturday. Route 105 also departs at 9.30 on Saturday. Once a week a particular bus is allocated to each departure scheduled for the following week. The data on which bus is allocated to which departure is only kept for one week , being removed when buses are allocated for the following week. Details of the registration number and seating capacity of each bus are recorded. Every route must consist of at least one stage, but details of a stage may be kept even if it is not currently part of route. Details of the scheduled departures are recorded before buses are allocated. Details of all buses are recorded even if they are not currently allocated to any departures. Details of routes are to be recorded before departures have been arranged. TASKS For the above 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. You may decide to follow the ERD’s in Answer Points. Mansha Nawaz BUS EXAMPLE Case Study 9: 2 Case Study 9: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script BUS EXAMPLE SCHEMA - PHYSICAL Exercise 2 – Create the physical schema for BUS ERD. SQL Server Physical Schema are the SQL commands required for Table Definitions. Create table bus (reg_no char(7), No_of_seats integer, Constraint buskey primary key(reg_no)); These commands are loaded into the QA interface and executed to create the tables for the ERD’s. Create a schema for the tables identified in the BUS ERD. Write the SQL BUS schema in Notepad or MS Word. Load and execute in MS SQL Server 2005 – Management Studio. Make sure the constraints for primary and foreign keys are embedded in the code. Validate your code and remove any errors. CONSTRAINTS Exercise 3 – Review the constraints for primary and foreign keys. Make sure the constraints for primary and foreign keys are embedded correctly in the SQL code generated in Ex 2. Constraints (any need careful attention?) All instances of entities must be uniquely identified. (Primary Key constraint)primary key of Departure? All attribute domains must be defined (Domain constraints-implemented as check constraints in less capable systems) Referential actions have to be defined all foreign keys. Requires considerations likeIf a stage is removed from the database what happens routes that use the stage? Are all the stages_on_a_route and route removed when the stage? If a route is removed from the database what happens to the stages_on_a_route-are they removed as well? What are the appropriate referential actions in these cases? Mansha Nawaz BUS EXAMPLE Case Study 9: 3 Case Study 9: MS SQL SERVER 2005 – Management Studio – Create tables via SQL Script BUS EXAMPLE Cardinalities must be enforced. Note cardinality of ‘Makes_a’. Details of the scheduled departures are recorded before buses are allocated? How many departures per bus? General Constraints Can route end and route start be the same? Can stage start and end be the same? Must be a meaningful sequence of stages for a route i.e. start point of next stage must be end point of previous, apart from first stage. Sequence numbers must be consecutive. Cannot allocate a bus for a departure time in the time interval involving another departure for that bus. (primary key constraint?) Inserting Data Exercise 4 – Inserting data to the BUS database Using the INSERT command write an insert schema to load the BUS database with sample data. ACCESS - SQL Exercise 5 –Produce a MS Access and MS SQL Server project based on this case study MS SQL Server Project - linking MS SQL server (physical tables) to MS Access workstation (logical views) Mansha Nawaz BUS EXAMPLE Case Study 9: 4 MS SQL – QUERY ANALYSER – BUS SOLUTIONS MODEL SOLUTIONS The following model and schema does not cover every aspect of the case study. Solution Exercise 1 – Produce a BUS ERD. A bus travels on a route. Travels_on_a Bus Route 0.. * 0.. * Minimum no. of buses travelling on each route? Maximum no. of buses travelling on each route? Minimum no. of routes travelled by each bus? Maximum no. of routes travelled by each bus? Many-Many relationship decomposed. PK is reg_no, route_no Makes_a Bus Departure 1..1 1..* PK is reg_no 0..* Along_a 1..1 Route PK is route_no MS SQL – QUERY ANALYSER BUS SOLUTIONS by Mansha Nawaz 5 MS SQL – QUERY ANALYSER – BUS SOLUTIONS A route consists of stages. Consists_of Route Stages 0..* 1..* Minimum no. of routes for each stage? Maximum no. of routes for each stage? Minimum no. of stages for each route? Maximum no. of stages for each route? Many-Many relationship decomposed. PK is Route_no, stage_start, stage_end Stage_on_a_route 0..* Consists_of 0..* Route 1..1 PK is route_no Is_a 1..1 PK is stage_start, stage_end Stage MS SQL – QUERY ANALYSER BUS SOLUTIONS by Mansha Nawaz 6 MS SQL – QUERY ANALYSER – BUS SOLUTIONS ERD BUS. Makes_a Bus Departure 1..1 1..* 0..* Along_a Consists_of 1..1 Route Stage_on_a_route 0..* 1..1 0..* Are all constraints correct? (any need careful attention?) Is_a 1..1 Stage Bus (reg_no, no_of_seats) Departure (reg_no, route_no, departure_time, day, est_arrival_time) Route (Route_no, Route_start, Route_end) Stage_on_a_route (stage_start, stage_end, route_no, sequence_no) Stage (stage_start, stage_end, distance) MS SQL – QUERY ANALYSER BUS SOLUTIONS by Mansha Nawaz 7 MS SQL – QUERY ANALYSER – BUS SOLUTIONS Solution Exercise 2 – Create the physical schema for BUS ERD. Script file : BUS.schema.sql Create table bus (reg_no char(7), No_of_seats integer, Constraint buskey primary key(reg_no)); Create table route (route_no char(3), route_start varchar(20), route_end varchar(20), Constraint route_key primary key (route_no)); Create table stage (Stage_start varchar(20), Stage_end varchar(20), distance integer, Constraint stagekey primary key (stage_start, stage_end)); Create table stage_on_route (route_no char(3), Stage_start varchar(20), Stage_end varchar(20) Constraint sorkey primary key(route_no, stage_start, stage_end) Constraint consists_of foreign key (route_no) references route, Constraint is_a foreign key (stage_start, stage_end) references stage); Create table departure (reg_no char(7), Route_no char(3), Constraint depart_key primary key (reg_no, route_no), Constraint makes_a foreign key (reg_no) references bus(reg_no), Constraint along_a foreign key (route_no) references route(route_no)); Solution Exercise 3 – Review the constraints for primary and foreign keys. See Solution Ex 2. Solution Exercise 4 – Inserting data to the BUS database Insert into bus values(……..); Insert into bus……..; Insert into bus…..; Insert into route…. Insert into stage…. Insert into departure….. Insert into departure….. Insert into stage_on_a_route….. Note order of insert. Load tables without foreign keys first. Otherwise an error- ‘cannot insert value does not exist as primary key’ will occur on foreign key tables. Important that data is consistent e.g. that departures refer to data in ‘bus’ MS SQL – QUERY ANALYSER BUS SOLUTIONS by Mansha Nawaz 8