473,835 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Database design, inherit

PEK
I have a database that looks something like this:

Table: Creature
Fields, CreatureId {PK}, ...

Table: Bug
Fields: BugId {PK}, CreatureId {FK}...

Table: LadyBird
Fields: LadyBirdId {PK}, BugId {FK}...

Every creature in the system is in the Creature-table. If the creature
also is a bug, then it's also in the bug table. And if it's a
LadyBird, it's also in the LadyBird-table.

A problem with the current design is that several bugs could be of the
same creature, and several LadyBirds could be of the same bug which
should not be possible.

So I'm thinking about removing the fields BugId and LadyBirdId, and
use CreatureId (as primary key) instead in the bug and ladybird table.
Would that improve the design of the database? With a new design it's
possible to have a LadyBird that's not a Bug.

I still thinks the new design would be better, but I'm gladly accept
advices before I starts the redesign (which will be quite some work to
do :-/ )

PEK
Jul 23 '05 #1
2 1817
The classic scenario calls for a root class with all the common
attributes and then specialized sub-classes under it. As an example,
let's take the class of Vehicles and find an industry standard
identifier (VIN), and add two mutually exclusive sub-classes, Sport
utility vehicles and sedans ('SUV', 'SED').

CREATE TABLE Vehicles
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) NOT NULL
CHECK(vehicle_t ype IN ('SUV', 'SED')),
UNIQUE (vin, vehicle_type),
..);

Notice the overlapping candidate keys. I then use a compound candidate
key (vin, vehicle_type) and a constraint in each sub-class table to
assure that the vehicle_type is locked and agrees with the Vehicles
table. Add some DRI actions and you are done:

CREATE TABLE SUV
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT 'SUV' NOT NULL
CHECK(vehicle_t ype = 'SUV'),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Vehicles(vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

CREATE TABLE Sedans
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT 'SED' NOT NULL
CHECK(vehicle_t ype = 'SED'),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Vehicles(vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

I can continue to build a hierarchy like this. For example, if I had a
Sedans table that broke down into two-door and four-door sedans, I
could a schema like this:

CREATE TABLE Sedans
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT 'SED' NOT NULL
CHECK(vehicle_t ype IN ('2DR', '4DR', 'SED')),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Vehicles(vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

CREATE TABLE TwoDoor
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT '2DR' NOT NULL
CHECK(vehicle_t ype = '2DR'),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Sedans(vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

CREATE TABLE FourDoor
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT '4DR' NOT NULL
CHECK(vehicle_t ype = '4DR'),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Sedans (vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

The idea is to build a chain of identifiers and types in a UNIQUE()
constraint that go up the tree when you use a REFERENCES constraint.
Obviously, you can do variants of this trick to get different class
structures.

If an entity doesn't have to be exclusively one subtype, you play with
the root of the class hierarchy:

CREATE TABLE Vehicles
(vin CHAR(17) NOT NULL,
vehicle_type CHAR(3) NOT NULL
CHECK(vehicle_t ype IN ('SUV', 'SED')),
PRIMARY KEY (vin, vehicle_type),
..);

Now start hiding all this stuff in VIEWs immediately and add an INSTEAD
OF trigger to those VIEWs. Faking an OO class model like this will be
slow and a bitch to maintain, but there it is. You might want to try a
relational design instead.

Jul 23 '05 #2
--CELKO-- wrote:
Now start hiding all this stuff in VIEWs immediately and add an INSTEAD OF trigger to those VIEWs. Faking an OO class model like this will be slow and a bitch to maintain, but there it is. You might want to try a relational design instead.


Thanks for your detailed answer. Adding a "type" key was an clever idea
:-). However, it seems that in some tables (like FourDoor) the type
column will have the same value in every row which I'm not very
comfortable with. But if there is no away around it maybe that's the
way to go.

A relation design may be better, but that's what I'm trying to create
(but sense I think that everything is objects I'm guess that affects my
solution :-). The problem I'm actually trying to solve is something
like this:

I have three tables with animals (say, Bug, Cat and Dog) and three
tables with places (like House, Car and Boat). Every animal could have
(but not always have) an relation every place (like "The cat 12 entered
the house 21 at 21:12"). It's the relation that is the problem. I could
create nine table for every possible solution (but that feels like a
bad design, or?). One generic table would be a more elegant solution. I
see two approaches:

First, inspired from your solution, the tables would look something
like this:
Bug (pk: BugId, AnimalType)
Cat (pk: CatId, AnimalType)
Dog (pk: DogId, AnimalType)
House (pk: HouseId, PlaceType)
Car (pk: CarId, PlaceType)
Boat (pk: BoatId, PlaceType)
AnimalPlace( pk: AnimalId, AnimalType, PlaceId, PlaceType )

The second solution, which I think is better, would require unique id's
as primary keys for the animals and places (so if a cat has id 1,
neither a bug or dog could have id 1). This would remove the type
problem (this is how I thought in the beginning):
Bug (pk: BugId)
Cat (pk: CatId)
Dog (pk: DogId)
House (pk: HouseId)
Car (pk: CarId)
Boat (pk: BoatId)
AnimalPlace( pk: AnimalId, PlaceId, )

Both these solutions has the problem is that I have no idea how to
create the foreign keys in the AnimalPlace table. The AnimalId should
be a foreign key to one the primary keys in the animal tables (BugId,
CatId or DogId). But that is not possible, or? I found this thread with
Google which discussing the problem:

http://groups.google.se/groups?hl=sv...ink.net#link10

A clever man almost solves the problem in the end. But it requires that
all children is created before there parents (in my case, AnimalPlace
should be created before Cat for example).

So how to handle this problem. Is subclass-superclass the way to solve
this?

PEK

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3154
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
4
1610
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
62
4133
by: SAN3141 | last post by:
There doesn't seem to be consensus about when to put code in the database or in the middle tier. There was a long discussion about this in an Oracle newsgroup (message ID: ULcQb.466$KU5.37@nwrddc02.gnilink.net). Elsewhere there's been discussion about Microsoft SQL Server 2005 adding the CLR to support stored procedures in languages such as C#. A scan of the Web and discussion forums finds differing opinions about this.
1
2160
by: Mark | last post by:
I have .aspx code-behinds that inherit from the class below. The code runs just fine, but the form designer bombs at design time when trying to view the .aspx page in VS.NET 2003. If I comment out the single line of code below, then there is no error. I posted the error message below - it implies that this code is being run at design time, but it bombs due to the fact that it isn't run time. Is there a line of code I could add to only...
3
1499
by: Mojtaba Faridzad | last post by:
Hi, my question is about OO design. imagine we have 2 classes with some common members and some different members. which one is better and why: 1) design class A completely and B as a drived from A with the different members. 2) design a super class with common members then design class A and B drived from this super class with their different members.
1
1896
by: Josh28 | last post by:
Hi We are a group of two chaps just out of undergrad, we created a software to automate the use of Design Patterns. We have put it up at Source Forge--http://dpatoolkit.sourceforge.net/ The software has been designed using the .NET framework and coded in C#. The patterns can be stored as plug-ins in XML, adding any number of attributes like Intent, Behavior and the like... Class
4
1836
by: Val P | last post by:
How does everyone design the database access layer in an asp.net application? Two options that come to mind is: 1. create a database class and instanciate it as needed, local to a function or 2. Provide database access in a base class from which all objects that need db access will inherit from. #1 seems wasteful for application that do frequent but light db access. Option #2 seems even more wasteful, because it adds overhead to the
4
1856
by: kikazaru | last post by:
Can I write methods in one class A, that use methods in another base class B, in such a way that I can make a class C1 that inherits A and B1 (a child of B) so that the methods in A use the implementations of B's methods provided by B1? (And also make C2 inheriting A and B2 (another child of B) and C3 etc.). Is it possible to do this entirely with inheritance, or do you have to store a pointer to a class of type B in A in order to get this...
0
1354
by: David | last post by:
Hi list. I have a few database-related questions. These aren't Python-specific questions, but some of my apps which use (or will use) these tables are in Python :-) Let me know if I should ask this on a different list. Question 1: Storing app defaults. If you have a table like this:
0
10815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10524
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6968
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3997
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3092
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.