Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice For Lookup Tables In SQL

I am fairly new to SQL so apologies if this questions sounds odd.

I keep coming across the issue of poor data. As an example London could be stored as LON, London UK, London England etc. Prior to using SQL I had lots of Excel lookup tables where in the first column I would have the original, and in the 2nd helper column I would have the corrected version. As an example:

Name             Name_1
London, UK       London
Lon              London
LON              London
London           London
London, England  London
LND              London

Is there a straightforward way of doing this in SQL? I am currently trying to create lookup tables and then use joins. This is becoming tricky as I do not always have corrections for every instance, so in most scenarios my lookup tables have fewer items than the tables I'm joining them to.

I've been teaching myself about stored procedures and I wondered if this could solve the problem. Trouble is my searching on the subject of lookup tables is coming up empty.

Any advice or pointers would be gratefully received, even if it is just to say that this cannot be done.

Thanks as always for you help and apologies for the long post.

like image 610
chris1982 Avatar asked Oct 17 '13 11:10

chris1982


People also ask

When would you need to use lookup tables?

Lookup tables provide the essential function of helping you maintain data integrity in your database environment. For example, if you have users entering their gender into a data item, the table that contains the Gender item can reference a lookup table to verify that only the value M or F is used.

Are lookup tables faster?

You approximate the value of the ideal function at a point by interpolating between the two breakpoints closest to the point. Because table lookups and simple estimations can be faster than mathematical function evaluations, using lookup table blocks often result in speed gains when simulating a model.

What is the use of lookup table in SQL?

Lookups are an intuitive table linking syntax provided to simplify data integration and SQL queries. They represent foreign key relationships between tables, and once established, can be used to "expose" columns from the "target" of the lookup in the source table or query.


2 Answers

You can join to the lookup table and preferrable use the value given there. If not found, use the original:

SELECT t1.FirstName, LookupField = ISNULL(t2.Name_1, t1.LookupField)
FROM People as t1
LEFT INNER JOIN TableLookupCities as t2 ON t1.LookupField = t2.Name

Make sure, that for each name there is at most one match in TableLookupCities, or else the join will produce multiple results. Create a unique index on TableLookupCities.Name:

CREATE UNIQUE (CLUSTERED) INDEX djgndkg ON TableLookupCities (Name) INCLUDE (Name_1)
like image 174
usr Avatar answered Oct 12 '22 12:10

usr


You don't have to do any of the other things, just return the original if you don't have a translation for it.

SELECT
t1.FirstName,
t1.LookupField,
case when t2.Name_1 is null 
    then t1.lookupfield 
    else t2.name_1 end Name_1
FROM People as t1
LEFT INNER JOIN TableLookupCities as t2
ON t1.LookupField = t2.Name
like image 40
Tom48 Avatar answered Oct 12 '22 12:10

Tom48