code green 1,726
Recognized Expert Top Contributor
I wasn't sure what title to give this, or whether I can explain my dilemma, but here goes.
My company sells products that are inter-compatible with each other, mainly four ranges I will call A,B,C,D.
A key table in the database is `product_compat ability` with two columns listing product IDs that are compatible with each other.. At the moment it lists the compatibility between A-B with A in col 1 and B in col 2. Straightforward .
This must now be expanded, with possible combinations of A-B; A-C; A-D; B-C; B-B; B-D; but no possible match C-D.
The dilemma now is the rule of A in column 1 and B in column 2 breaks down, as B can be in either and BOTH. Thereby complicating queries SELECT INSERT and UPDATE.
Our answer to avoid UNION the table with itself in every query is to double the size of the table and mirror each record with itself so it does not matter about the column order. The table goes from 30000 records to 60000 but queries are less complicated.
I sit uncomfortable with this. In effect every record is duplicated but in reverse order. However queries should be faster, I think!
Is this correct database design or is a rethink in order?
10 2219 Atli 5,058
Recognized Expert Expert
Ok, Im trying to understand this but there is one thing that's bothering me. You list all possible combinations of compatible products, but you include 'B-B'.
How does that make sence?
code green 1,726
Recognized Expert Top Contributor
I admit this is confusing.
The letters represent a class of products, not individual products..
And the combination B-B means the two parts are interchangeable rather than compatible with each other.
Atli 5,058
Recognized Expert Expert
Ok,
So, assuming that each product cluster has a auto_increment primary key, you could create a rule that states that the smaller ID should always be placed in the first column, and the larger in the second one.
(This rule could be adjusted to any primary key, like say in alphabetic order if that would fit your primary keys better.)
But lets assume for now, that I have a table, called 'ordTest' that was created like this: -
CREATE TABLE ordTest
-
(
-
val1 BIGINT NOT NULL,
-
val2 BIGINT NOT NULL,
-
PRIMARY KEY(val1, val2)
-
)
-
Then you could create a procedure that would insert into that table, putting the smaller ID into the first column and the bigger into the second. -
delimiter $
-
DROP PROCEDURE IF EXISTS InsertCompatible$
-
CREATE PROCEDURE InsertCompatible(x BIGINT, y BIGINT)
-
BEGIN
-
IF x < y THEN
-
INSERT INTO ordTest(val1, val2) VALUES(x, y);
-
ELSE
-
INSERT INTO ordTest(val1, val2) VALUES(y, x);
-
END IF;
-
END $
-
delimiter ;
-
Then you can create another procedure to check wheter any given ID numbers are compatible -
delimiter $
-
DROP PROCEDURE IF EXISTS IsCompatible$
-
CREATE PROCEDURE IsCompatible(xID BIGINT, yID BIGINT)
-
BEGIN
-
DECLARE x INT;
-
IF xID < yID THEN
-
SELECT COUNT(*) INTO x FROM ordTest WHERE val1 = xID AND val2 = yID;
-
ELSE
-
SELECT COUNT(*) INTO x FROM ordTest WHERE val1 = yID AND val2 = xID;
-
END IF;
-
SELECT x != 0 AS 'RESULT';
-
END $
-
delimiter ;
-
Now, all you have to do to insert compatible products classes into the table is call the first procedure, like this: -
CALL InsertCompatible(1, 2);
-
Note, that because of the two columns both acting as Primary Key's, you can not insert duplicate products using this procedure.
And to check if any two ID's are compatible you can call the second procedure:
Which would output a table with a singe col called 'RESULT' and contain a single row with a boolean value, indicating whether the ID's match.
code green 1,726
Recognized Expert Top Contributor
Thanks Atli,
I'll study this and reply next couple of days.
But if I am doing a select,update or delete on a particular ID,
then will both fields be included in every query with a self join or a union?.
Atli 5,058
Recognized Expert Expert
Thanks Atli,
I'll study this and reply next couple of days.
But if I am doing a select,update or delete on a particular ID,
then will both fields be included in every query with a self join or a union?.
I'm not entirly sure what you mean.
The table works just as any other table, except both fields are joined to form a primary key.
You can select or modify them seperately as you wish, as long as neither of them ends up empty and when they are combined, they must be unique.
code green 1,726
Recognized Expert Top Contributor
It is not uniqueness of table entries that concern me, as both fields are set up as primary keys. It is a balance of performance and memory usage. Do I go for this structure - product_id compatible_product_id
-
101 303
-
101 307
-
102 305
-
102 303
-
303 307
Or this structure - product_id compatible_product_id
-
101 303
-
101 307
-
102 305
-
102 303
-
303 102
-
303 101
-
307 101
-
305 102
-
303 307
-
307 303
The first LOOKS more efficient until you start to write queries, for example to SELECT all products compatible with id 303. The current queries will still work with the second design but need rewriting to check both fields in the first.
The second design does not need any stored procedure or knowledge of any order of INSERT rules. But INSERTs and DELETEs have to be executed twice with the second. I am simply looking for somebody's opinion
Since A is compatibile with B implies B is compatible with A then it doesn't matter which column holds A or B. A single row (A,B) or (B,A) or both rows implies A is compatible with B.
Atli's insert and delete procedures would eliminate duplicate entries in the table.
Since it doesn't matter which column A is in, you need to select values from either column, so the select would be something like:
select distinct ("product id" (if not :A), else "compatible product id")
from table
where "product id" = :A or "compatible product id" = :A
This should give you the other product when one is A.
Atli 5,058
Recognized Expert Expert
It is not uniqueness of table entries that concern me, as both fields are set up as primary keys. It is a balance of performance and memory usage.
Ok, I see what you mean now.
I'm not sure, but I would think a more complicated query executed on x ammount of rows would run faster that a simple one on x*2 rows.
Thats mainly because queries are most often just collections of boolean expressions that MySQL executes using the data it has, and I'm pretty sure it takes more time for MySQL to fetch extra rows that it takes to execute extra boolean expressions.
It would probbly depend on you queries, so you should really test both methods using random data and compair the results.
code green 1,726
Recognized Expert Top Contributor
Thanks for the replies.
This is just one table of about 50 in the whole database.
This query looks simple - select distinct ("product id" (if not :A), else "compatible product id")
-
from table
-
where "product id" = :A or "compatible product id" = :A
but becomes quite complicated when other tables are JOINed to pull in a product_name or product_descrip tion for example.
And UPDATEs would be even more troublesome.
As I said, I will need little modification of my current queries if I go with the second design with the mirror data..
To be honest I was hoping somebody would tell me off for even considering one of these designs.
Oh well, i will go with the mirrored data idea, if only to save extra work on my part. Thanks again.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Rushikesh |
last post by:
I am designing a WEB BASED Accounting Software with ASP and SQL
Server. For this I need some help for the Database design. My design
is as follows.
I)User table: User_id, UserName.....
Users (e.g. John Smith) Each User would contain a following Group of
tables
a)Customers
|
by: cmo63126 |
last post by:
I'm not sure if this is bad design or not. It seems flawed, but I'm not
sure. Is it wrong to create an instance of a class within the class
itself? Any feedback appreciated.
public class Article
{
int articleID;
string title;
//Constructor: Load Data from DataAccess Class
|
by: Don Vaillancourt |
last post by:
Hello all,
Over the years as I design more database schemas the more I come up with
patterns in database design. The more patterns I recognize the more I
want to try to design some kind of generic design patterns that can be
used and shared amongst many sub-schemas.
For example, the grouping of entities. I may have the following tables:
employee, product and client. These tables have no direct relationship
with each other. But...
|
by: mm nn |
last post by:
Hi,
I want to create a table like this:
ID Autonum
Datefld Date
Cat Text
Itm Text
tCount Number
|
by: Mike Bridge |
last post by:
I am using MS Access 2003, and I typed in a query in SQL view which was working fine. Now when I open it, it gives me
the error "Join expression not supported". Apparently, Access "fixed" it for me.
Unfortunately, I can't correct it because when I click "OK", the window disappears. I don't see any way to open the
query directly in SQL view, and both the Design and Data views exit as soon as I close the error dialog.
Is there any way...
| |
by: MuZZy |
last post by:
Hi,
I'm going to develop a pretty simple document management application and
i want to use WCF to connect back-end to smart client front-end.
That application will allow to create a hierarchcal structure where any
node can be linked with a document file, which are all stored in a SQL
Server database.
I'm trying to convince my boss towards smartclient instead of ASP.NET
|
by: vicky |
last post by:
Hi All,
Can u please suggest me some books for relational database design or
database modelling(Knowledgeable yet simple) i.e. from which we could
learn database relationships(one to many,many to one
etc.....),building ER diagrams,proper usage of ER diagrams in our
database(Primary key foreign key relations),designing small
modules,relating tables and everything that relates about database
design....Coz I think database design is the...
|
by: Laurynn |
last post by:
# (ebook - pdf) - programming - mysql - php database applicati
# (Ebook - Pdf)Learnkey How To Design A Database - Sql And Crystal
Report
# (ebook-pdf) E F Codd - Extending the Database Relational Model to
Capture More Meaning (1979)
# Addison Wesley - Database Design for Mere Mortals chm
# Addison Wesley - Refactoring Databases Evolutionary Database Design
(2006)
# Apress Beginning Databases with PostgreSQL From Novice to
Professional...
|
by: programmerx101 |
last post by:
Ok, I'm looking for expert advice on this one.
I have a database which keeps going into read_only mode. Sometimes it goes into read_only / single user mode. Once it was taken offline completely. This seemingly happens randomly. Out of all of the database I have worked with, this has happened on 3 of them - several times randomly to each. All three of the databases that have exhibited this behaviour have been databases I have written for the...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |