Creating a networked data model
In this last exercise, you will create a networked data model. A use case for this is finding all possible paths a bus can take from one location to another. Each route has a departure and destination location. A departure and destination location can appear multiple times. In the following image you can see the possible bus locations and routes. For example, you can go from San Francisco to New York, or from New York to Washington.
Your task is to create the Trip
table, insert some routes into this table, and finally, select all possible departure locations from the table.
This exercise is part of the course
Hierarchical and Recursive Queries in SQL Server
Exercise instructions
- Define the fields
Departure
andDestination
, neither of which can beNULL
. - Insert the route from
San Francisco
toNew York
forBus 1
. - Insert the route from
Florida
toSan Francisco
forBus 9
. - Select all possible departure locations.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
CREATE TABLE Trip (
-- Define the Departure
___ VARCHAR(255) ___ ___,
BusName VARCHAR(255) NOT NULL,
-- Define the Destination
___ VARCHAR(255) ___ ___,
);
-- Insert a route from San Francisco to New York
___ ___ ___ VALUES ( ___ , ___ , ___);
-- Insert a route from Florida to San Francisco
___ ___ ___ VALUES ( ___, ___ , ___);
INSERT INTO Trip VALUES ( 'San Francisco', 'Bus 2','Texas');
INSERT INTO Trip VALUES ( 'San Francisco', 'Bus 3','Florida');
INSERT INTO Trip VALUES ( 'San Francisco', 'Bus 4','Washington');
INSERT INTO Trip VALUES ( 'New York', 'Bus 5','Texas');
INSERT INTO Trip VALUES ( 'New York', 'Bus 6','Washington');
INSERT INTO Trip VALUES ( 'Florida', 'Bus 7','New York');
INSERT INTO Trip VALUES ( 'Florida', 'Bus 8','Toronto');
-- Get all possible departure locations
SELECT ___ ___
FROM ___;