Does anyone know and tell how to implement physical entities with supe开发者_如何学Gor type sub type logical solution in DB2 database.
Its basically the same whatever RDBMS you are using:-
CREATE TABLE VEHICLES (
V_ID INTEGER PRIMARY KEY,
V_DESCRIPTION VARCHAR(20),
V_MAKE VARCHAR(20),
V_MODEL VARCHAR(20),
V_COST DECIMAL(8,2),
V_ETC VARCHAR(50),
V_TYPE SMALLINT
-- 1 -> CAR
-- 2 -> BICYCLE
);
CREATE TABLE CARS (
V_ID INTEGER PRIMARY KEY,
V_ENGINE_SIZE DECIMAL(6),
V_SEATS SMALLINT
);
CREATE TABLE BICYCLES (
V_ID INTEGER PRIMARY KEY,
GEARS SMALLINT
);
VEHICLES is your supertype which contains attributes common to all vehicles, CARS is a subtype of VEHICLES which contains attributes that pertain only to CARS, the V_TYPE attribute in the VEHICLES table identifies which subtype applies.
The primary keys of the supertype and subtype should be the same value and you can enforce this with a foreign key relationship.
精彩评论