I would appreciate any advice I can get for my simple modeling problem.
There is a set of properties. Each property must have an owner. Owners can be either companies or individuals. Owners can own more than one property.
The best integrity I've been able to come up with is the ability to make sure that the application cannot delete companies or individuals if they are currently property owners. However I have to have an 'owner_type' ENUM field so that the application knows how to get further details. I just wanted to run this past other people because I have a hunch that I am being blind to an alternative approach.
I've grossly over simplified my description and example implementation of the problem to illustrate just the relationship model I'm struggling with.
If there is a name for the relationship problem I've described I'd love to know that too.
Thanks for any help.
Code:
CREATE TABLE valid_id (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT
);
CREATE TABLE company (
id INT NOT NULL,
....
CONSTRAINT FOREIGN KEY (id) REFERENCES valid_id(id);
)
CREATE TABLE person (
id INT NOT NULL,
...
CONSTRAINT FOREIGN KEY (id) REFERENCES valid_id(id);
)
CREATE TABLE property_owner_account (
account_id INT NOT NULL PRIMARY KEY,
owner_type ENUM ('PERSON','COMPANY') NOT NULL,
owner_id INT NOT NULL,
...
CONSTRAINT FOREIGN KEY owner_id REFERENCES valid_id(id);
CONSTRAINT UNIQUE owner_id; # Owners should only have 1 account
)
CREATE TABLE property (
...
property_owner_account_id INT NOT NULL,
CONSTRAINT FOREIGN KEY property_owner_account_id REFERENCES property_owner_account(owner_id);
)