CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Hierarchical and Recursive Queries in SQL Server

Afficher le cours

Instructions

  • Define the fields Departure and Destination, neither of which can be NULL.
  • Insert the route from San Francisco to New York for Bus 1.
  • Insert the route from Florida to San Francisco for Bus 9.
  • Select all possible departure locations.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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 ___;
Modifier et exécuter le code