Specialization and generalization are fundamental concepts in database modeling that are useful for establishing superclass-subclass relationships.
Specialization is a top-down approach in which a higher-level entity is divided into multiple specialized lower-level entities. In addition to sharing the attributes of the higher-level entity, these lower-level entities have specific attributes of their own. Specialization is usually used to find subsets of an entity that has a few different or additional attributes.
The following enhanced entity relationship diagram expresses the entities in a hierarchical database to demonstrate specialization:
Let's see an example of specialization. We have created an entity type Employee
which can be specialized into Manager
and Staff
depending on their roles.
-- Create the Vehicle table (General Entity Type)CREATE TABLE Vehicle (Chassis_no VARCHAR(50) PRIMARY KEY,Make VARCHAR(50),Model VARCHAR(50));-- Create the Car table (Specialized from Vehicle)CREATE TABLE Car (Chassis_no VARCHAR(50) PRIMARY KEY,stereo_present BOOLEAN,FOREIGN KEY (Chassis_no) REFERENCES Vehicle(Chassis_no));-- Create the Bus table (Specialized from Vehicle)CREATE TABLE Bus (Chassis_no VARCHAR(50) PRIMARY KEY,seats_no INT,FOREIGN KEY (Chassis_no) REFERENCES Vehicle(Chassis_no));-- Create the Truck table (Specialized from Vehicle)CREATE TABLE Truck (Chassis_no VARCHAR(50) PRIMARY KEY,load_capacity INT,FOREIGN KEY (Chassis_no) REFERENCES Vehicle(Chassis_no));
Generalization is a bottom-up approach in which multiple lower-level entities are combined to form a single higher-level entity. Generalization is usually used to find common attributes among entities to form a generalized entity. It can also be thought of as the opposite of specialization.
The following enhanced entity relationship diagram expresses entities in a hierarchical database to demonstrate generalization:
Let's see an example of generalization. We have created two entities Car
and Truck
which can be generalized into vehicle
.
-- Create the Vehicle table (General Entity Type with Specialization Attributes)CREATE TABLE Vehicle (Chassis_no VARCHAR(50) PRIMARY KEY,Make VARCHAR(50),Model VARCHAR(50),stereo_present BOOLEAN,seats_no INT,load_capacity INT);-- Inserting some sample data into the Vehicle tableINSERT INTO Vehicle (Chassis_no, Make, Model, stereo_present, seats_no, load_capacity) VALUES('ABC123', 'Toyota', 'Camry', TRUE, NULL, NULL), -- Car('DEF456', 'Volvo', 'XC90', FALSE, 7, NULL), -- Bus('GHI789', 'Ford', 'F-150', NULL, NULL, 2000); -- Truck