8.3
A car-rental company maintains a database for all vehicles in its current fleet. For all vehicles, it includes the vehicle identification number, license number, manufacturer, model, date of purchase, and color. Special data are included for certain types of vehicles: * Trucks: cargo capacity. * Sports car: horsepower, renter age requirement. * Vans: number of passengers. * Off-road vehicles: ground clearance, drivetrain (four- or two-wheel drive).
Construct an SQL schema definition for this database. Use inheritance where appropriate.
For this problem, we use table inheritance. We assume that MyDate, Color and DriveTrainType are pre-defined types.
CREATE TYPE Vehicle (
INTEGER,
vehicle_id CHAR(15),
license_number CHAR(30),
manufacturer CHAR(30),
model
purchase_date MyDate,
color Color
);
CREATE TABLE vehicle OF TYPE Vehicle;
CREATE TABLE truck (
INTEGER
cargo_capacity
) UNDER vehicle;
CREATE TABLE sportsCar (
INTEGER,
horsepower INTEGER
renter_age_requirement
) UNDER vehicle;
CREATE TABLE van (
INTEGER
num_passengers
) UNDER vehicle;
CREATE TABLE offRoadVehicle (
REAL,
ground_clearance
driveTrain DriveTrainType ) UNDER vehicle;
If you use postgresql you can use the following code:-
CREATE TYPE COLOR AS ENUM (
'Red',
'Orange',
'Yellow',
'Green',
'Cyan',
'Blue',
'Magenta',
'Purple'
);
CREATE TYPE DriveTrainType AS ENUM (
'4-wheel',
'2-wheel'
);
CREATE TABLE vehicle (
INTEGER PRIMARY KEY,
vehicle_id CHAR(15),
license_number CHAR(30),
manufacturer CHAR(30),
model DATE,
purchase_date
vehicle_color COLOR
);
CREATE TABLE truck (
INTEGER
cargo_capacity
) INHERITS (vehicle);
CREATE TABLE sportsCar (
INTEGER,
horsepower INTEGER
renter_age_requirement
) INHERITS (vehicle);
CREATE TABLE van (
INTEGER
num_passengers
) INHERITS (vehicle);
CREATE TABLE offRoadVehicle (
REAL,
ground_clearance
driveTrain DriveTrainType ) INHERITS (vehicle);