If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Mapping table design?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-12-09, 02:04
tmpuzer tmpuzer is offline
Registered User
 
Join Date: Mar 2008
Posts: 52
Mapping table design?

Requirement:
Given an external code value, I need to be able to look up the corresponding internal code.

I need to create a schema to hold mappings between external and internal code values for each customer. There are different types of customers and often the mappings for the same type of customer will be the same. However, they can be customized on a per customer basis and therefore can vary. For example,
Code:
Customer 1 (Type Platform-A):
ExternalCode    InternalCode
100             orange
101             orange
102             cherry
...

Customer 2 (Type Platform-A):
ExternalCode    InternalCode
100             orange
101             orange
102             cherry
...

Customer 3 (Type Platform-A):
ExternalCode    InternalCode
100             apple
101             orange
102             cherry
...

Customer 4 (Type Platform-B):
ExternalCode    InternalCode
SUPERORANGE     orange
REGORANGE       orange
CHER            cherry
...
Here's an example schema that I think would work:
MAPPINGS {MapId, CustomerId, ExternalCode, InternalCode}

This would allow a select such as:
SELECT InternalCode FROM mappings WHERE CustomerId = 2 AND ExternalCode = '100'

I like the above design because the table is simple and querying it is simple. However, I'm wondering if having repeated data is bad design. In other words, if there are 100 customers of the same type, and suppose only 10 of them are customized, that means that 90 customers have identical mappings. And if a single customer has 50 mappings that's a lot of repeated data. So I'm wondering if there some way to indicate whether a customer uses a standard mapping or a custom mapping, and to treat it differently depending on that. Something like:

CUSTOMER_TO_MAP {CustomerId, MapId}
MAPPINGS {MapId, IsCustomMap, ExternalCode, InternalCode}

This way multiple customers can share the same map. And if a custom map is needed, one can be added. However, it seems like this requires a two step query to get data: 1. Figure out which map to use 2. Use that map to get the InternalCode.

Is this the right approach? Or is there a better way?
Reply With Quote
  #2 (permalink)  
Old 10-12-09, 04:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
use the simpler table

and lose the MapID column -- make (CustomerId, ExternalCode) the primary key
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-12-09, 12:26
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
As above but can't you also store the defaults in the same table under a separate CustomerId (say -1). Then do a left join on the defaults and the customer settings. Use ifnull to pull the customer setting if ones present and the default if not.
Reply With Quote
  #4 (permalink)  
Old 10-13-09, 19:01
tmpuzer tmpuzer is offline
Registered User
 
Join Date: Mar 2008
Posts: 52
What would that query look like?
Reply With Quote
  #5 (permalink)  
Old 10-22-09, 02:11
tmpuzer tmpuzer is offline
Registered User
 
Join Date: Mar 2008
Posts: 52
The largely repeated data has me concerned. Something seems bad about it.

I'm leaning toward:

CUSTOMERS_TO_MAPS {CustomerId, MapId}
MAPPINGS {MapId, ExternalCode, InternalCode}

To find out what code 'xyz' maps to for customer 5 use:

SELECT InternalCode
FROM Mappings
WHERE
ExternalCode = 'xyz'
AND MapId = (SELECT MapId FROM CustomersToMaps WHERE CustomerId = 5)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On