The input for filling the tables is a big file, which has unique animalIds, their names, their animal types and some other animal type specific values.
The database looks like this:
table animal:
- animalId(pk)
- name
- type(fk to animalType table)
table dogs:
- dogId(pk)
- animalId(fk to animal)
- hasCoat
- etc...
table fish:
- fishId(pk)
- animalId(fk to animal)
- animalId(fk to animal)
- dorsalFinCount
- etc...
I designed it this way, because my other source gives me an unique key (animalId, which identifies the single animal all over the world...like an uri) and i dont want to have one big single database with empty values (e.g. for hasCoat and hasDorsalFin) nor have to search in two type specific databases successively. Furthermore, i can easily extend this structure (table birds, with attribute hasWing, wingColor, etc...)
My (Java) program design looks like this:
abstract class Animal
- animalId
- name
- type
class Dog extends DatabaseObject
class Fish extends DatabaseObject
abstract class DatabaseObject
private UUID id;
public DatabaseObject(){this.uuid = UUID.randomUUID();}
public UUID getId();
The Problem is, that Dog and Fish classes are Animals, but need to have a UUID for each database entry, because the unique animalId is used as PK in the animal table.
Furthermore Dog and Fish should also extend Animal, because they need those data fields...but thats not possible in Java, because they allready extend the abstract DatabaseObject class.
A solution could be, that Dog and Fish get an Animal field as class member, but that seems to be a bad approach.
How would you solve this Problem and what are the pros for your design (database and program structure)?
edit: Since my assumption (...that a PK cant be used as a PK in another Table) is wrong, i could change dogId and fishId into animalId. Furthermore, such a database design is known as subtyping