473,651 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design question - type heirarchy with supertype queries

Is there a convient way to do a supertype/subtype heirarchy in mysql
and do queries on the supertype to return sets of subtypes?

For example, suppose I have a table with several types of military
hardware:

Table:
Id----Type--------Price
1.....Mig-15.....$20
1.....Mig-17.....$32
1.....Su-27......$80
1.....T-72........$20

What I'd like to be able to do is say:
SELECT FROM Table Where Type=Mig
instead of
SELECT FROM Table Where Type=Mig-15 or Type=Mig-17

Of course, we have to assume we have a type heirarchy:

Airplane
--Mig
----Mig-15
----Mig-17
--Su
----Su-27
Tank
--T
----T-72

What's the best way to represent this type heirarchy in the database
so as to require the minimum amount of queries to get a meaningful
result like all subtypes of Mig?

Thanks in advance,
Nick

Jun 27 '08 #1
3 5441
Why are you asking MySQL questions in a SQL Server Newsgroup?

Get a copy of TREES & HIERARCHIES IN SQL for several different ways to
model such structures.

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.
Jun 27 '08 #2
On Wed, 23 Apr 2008 14:48:28 -0700 (PDT), nflacco wrote:
>Is there a convient way to do a supertype/subtype heirarchy in mysql
and do queries on the supertype to return sets of subtypes?

For example, suppose I have a table with several types of military
hardware:

Table:
Id----Type--------Price
1.....Mig-15.....$20
1.....Mig-17.....$32
1.....Su-27......$80
1.....T-72........$20

What I'd like to be able to do is say:
SELECT FROM Table Where Type=Mig
instead of
SELECT FROM Table Where Type=Mig-15 or Type=Mig-17
Hi Nick,

In this specific case, you can use WHERE Type LIKE 'Mig%'. But I guess
that's sidestepping your actual question :)
>Of course, we have to assume we have a type heirarchy:

Airplane
--Mig
----Mig-15
----Mig-17
--Su
----Su-27
Tank
--T
----T-72

What's the best way to represent this type heirarchy in the database
so as to require the minimum amount of queries to get a meaningful
result like all subtypes of Mig?
There is no single best way. There are several methods; which one you
choose depends on the type of operations you often do. If you google the
terms below, you'll find a wealth of information:
* Adjacency list model
* Nested sets model
* Materialized path model

In SQL Server 2008, there will also be a new data type, HierarchyID,
which is basically an encoded and optimised materialized path. From what
I've seen so far, builtin support for actually handling the hierarchy
apppears to be rather sparse though.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Jun 27 '08 #3
"Why are you asking MySQL questions in a SQL Server Newsgroup? "

I actually am using MySQL and posted in the wrong group by accident.
However, I'm less worried about the specific features mysql or
postgres or mssql has that would deal with my problem as much as
general way of dealing with this as I'm thinking of switching to
postgres once mysql is close-sourced by sun.

As for the solutions, the cascade looks pretty interesting and I'll
try that if I have time in the next few days. Also, I looked up some
of the terms given in Hugo's response, found lots of good stuff like:

http://troels.arvin.dk/db/rdbms/links/#hierarchical

thanks!
Jun 27 '08 #4

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

Similar topics

4
4467
by: Opa1 | last post by:
I am designing and product ordering database and have a design question. I have a Payments table which records all payments for a given OrderID. There are several payment methods (cash, credit card, check). Each of these payment methods has their own fields (i.e. CreditCardNum for payments by credit card, CheckNum for payments by check, etc.) If I place the CreditCardNum and CheckNum fields into a payments
4
1604
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.
4
2522
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site http://www.dofactory.com/Patterns/PatternAbstract.aspx). Could anybody try to explain me in his own words how this pattern work and what the purpose is? thanks in advance
4
2016
by: Dave | last post by:
I am working on an access 2000 DB for some tree-growers that will be storing items in a heirarchy of locations. The items will obviously be stored at the lowest level in the heirarchy (in a row) but the rows are grouped into sections, and sections are grouped into houses. Here is a picture. I'm sure you understand already. HouseA -> Section A -> Row1 -> Row2 -> item -> Row3
22
3755
by: Mike Krous | last post by:
Hello All, I am having some problems modeling a relationship properly and could use some advice. My final question is at the bottom of this post (everything else is explanation). Basically what I have is a customer to our company who can be either an individual or a company, each company may have many employees (which are really individuals that work-for *or are-related-to* a company), now for the problem: each individual, company and...
13
2092
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My example is a program called HijackThis. I'm sure many are familiar that it is a spyware removal tool. The program looks at over 20 places on Windows computers to see what is starting (with options to remove the offending software). The program can...
3
4459
by: Joe Adams | last post by:
Hi All, How can I use GetType(<GenericType>).IsAssignableFrom(<MyType>) I need to now if the <MyType> is the same type of class as the <GenericType> without having to add the generic type member members. i.e. I do not want to use GetType(<GenericType>(Of String).IsAssignableFrom(<MyType>)
29
3562
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
18
1974
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller classes that each
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8694
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...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5605
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
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
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.