Consider the following example
CREATE TABLE Vehicle (
Vehicle_id INT PRIMARY KEY
...
);
CREATE TABLE Bike (
Vehicle_id INT PRIMARY KEY REFERENCES Vehicle(Vehicle_id)
...
);
CREATE TABLE Car (
Vehicle_id INT PRIMARY KEY REFERENCES Vehicle(Vehicle_id)
...
);
??
)ALTER TABLE ONLY Car
ADD CONSTRAINT not_in_bike CHECK (??)
ALTER TABLE Car AND Bike
ADD CONSTRAINT Car_or_Bike CHECK (Car.Vehicle_id <> Bike.Vehicle_id)
Thank you!
There are two possibilities:
Store all entities in a single table and use a column (type
or so) to determine which type a certain row is.
Use the layout you have now, but add a column type
to all three tables. Add that column to the primary key or create a unique constraint on (vehicle_id, type)
.
Define the foreign keys to include type
and make sure they have the correct value using a CHECK
constraint:
ALTER TABLE bike ADD FOREIGN KEY (vehicle_id, vehicle_type) REFERENCES vehicle;
ALTER TABLE bike ADD CHECK (vehicle_type = 'bike');