<p>I'm currently trying to improve the design of a legacy db and I have the following situation</p>

<p>Currently I have a table SalesLead in which we store the the LeadSource.</p>

<pre><code>Create Table SalesLead(
    ....
    LeadSource varchar(20)
    ....
)
</code></pre>

<p>The Lead Sources are helpfully stored in a table.</p>

<pre><code>Create Table LeadSource (
    LeadSourceId int,   /*the PK*/
    LeadSource varchar(20)
)
</code></pre>

<p>And so I just want to Create a foreign key from one to the other and drop the non-normalized column.</p>

<p>All standard stuff, I hope.</p>

<p>Here is my problem. I can't seem to get away from the issue that instead of writing</p>

<pre><code> SELECT * FROM SalesLead Where LeadSource = 'foo'
</code></pre>

<p>Which is totally unambiguous I now have to write </p>

<pre><code>SELECT * FROM SalesLead where FK_LeadSourceID = 1
</code></pre>

<p>or </p>

<pre><code>SELECT * FROM SalesLead 
INNER JOIN LeadSource ON SalesLead.FK_LeadSourceID = LeadSource.LeadSourceId 
where LeadSource.LeadSource = "foo"
</code></pre>

<p>Which breaks if we ever alter the content of the LeadSource field.</p>

<p>In my application when ever I want to alter the value of SalesLead's LeadSource I don't want to update from 1 to 2 (for example) as I don't want to have developers having to remember these <strong>magic numbers</strong>. The ids are arbitrary and should be kept so.</p>

<p><strong><em>How do I remove or negate the dependency on them in my app's code?</em></strong></p>

<p><strong>Edit</strong> Languages my solution will have to support</p>

<ul>
<li>.NET 2.0 + 3 (for what its worth asp.net, vb.net and c#)</li>
<li>vba (access)</li>
<li>db (MSSQL 2000)</li>
</ul>

<p><strong>Edit 2.0</strong> The join is fine is just that  'foo' may change on request to 'foobar' and I don't want to haul through the queries.</p>

[{'answer_id': 81553, 'author': 'LohanJ', 'author_id': 11286, 'author_profile': 'https://Stackoverflow.com/users/11286', 'pm_score': 1, 'selected': False, 'text': '<p>Did you consider an updatable view? Depending on your database server and the integrity of your database design you will be able to create a view that, when its values change, in turn it will update the constituent tables.</p>\n'}, {'answer_id': 81565, 'author': 'Martin Marconcini', 'author_id': 2684, 'author_profile': 'https://Stackoverflow.com/users/2684', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to de-normalize the table, simply add the LeadSource (Varchar) column to your SalesLead table, instead of using a FK or an ID.</p>\n\n<p>On the other hand, if your language has support for ENUM structures, the "magic numbers" should be safely stored in an enum, so you could: </p>\n\n<pre><code>SELECT * FROM SALESLEAD WHERE LeadSouce = (int) EnmLeadSource.Foo; //pseudocode\n</code></pre>\n\n<p>And your code will have a</p>\n\n<pre><code>public enum EnmLeadSource \n{\n   Foo = 1,\n   Bar = 2\n}\n</code></pre>\n\n<p>It is OK to remove some excessive normalization if this causes you more trouble than what it fixes. However, bear in mind that if you use a VARCHAR field (as oposed to a Magic Number) you must maintain consistency and it could be hard to localize later if you need multiple languages or cultures.</p>\n\n<p>The best approach after Normalization seems to be the usage of an Enum structure. It keeps the code clean and you can always pass enums across methods and functions. (I\'m assuming .NET here but in other languages as well)</p>\n\n<p><strong>Update</strong>: Since you\'re using .NET, the DB Backend is "irrelevant" if you\'re constructing a query through code. Imagine this function:</p>\n\n<pre><code>public void GiveMeSalesLeadGiven( EnmLeadSource thisLeadSource )\n{\n  // Construct your string using the value of thisLeadSource \n}\n</code></pre>\n\n<p>In the table you\'ll have a LeadSource (INT) column. But the fact that it has 1,2 or N won\'t matter to you. If you later need to change foo to foobar, that can mean that:</p>\n\n<p>1) All the "number 1" have to be number "2". You\'ll have to update the table.\n2) Or You need Foo to now be number 2 and Bar number 1. You just change the Enum (but make sure that the table values remain consistent).</p>\n\n<p>The Enum is a very useful structure if properly used. </p>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 81570, 'author': 'pilif', 'author_id': 5083, 'author_profile': 'https://Stackoverflow.com/users/5083', 'pm_score': 0, 'selected': False, 'text': '<p>I really don\'t see your problem behind the join. </p>\n\n<p>Naturally, asking directly by the FK_LeadSourceID is wrong, but using the JOIN seems to be the right way to go as I masks changing IDs perfectly fine. If, for example, "foo" becomes 3 at one day (and you update the foreign key field), the last query you\'ve displayed will still work exactly the same.</p>\n\n<p>If you want to make the change to the schema without altering the current queries in the application, then a view encompassing this join is the way to go.</p>\n\n<p>Or if you fear that the join Syntax is non-intuitive, there\'s always the subselect...</p>\n\n<pre><code>SELECT * FROM SalesLead where FK_LeadSourceID = \n         (SELECT LeadSourceID from LeadSource WHERE LeadSource = \'foo\')\n</code></pre>\n\n<p>but remember to keep an index on LeadSource.LeadSource - at least if you have a lot of them stored in the table.</p>\n'}, {'answer_id': 81623, 'author': 'Olaf Kock', 'author_id': 13447, 'author_profile': 'https://Stackoverflow.com/users/13447', 'pm_score': 0, 'selected': False, 'text': '<p>If you "improve design" by introducing new relations/tables, you\'ll certainly have the need for different entities. If so, you\'ll need to deal with their semantics. </p>\n\n<p>In the previous solution you were able to just update the LeadSource name to whatever you wanted in the appropriate SalesLead row. If you update the name in your new structure, you do so for all SalesLead rows.</p>\n\n<p>There is no way around dealing with these different semantics. You just have to do so. In order to make the tables easier to query, you might use views as already suggested, but I\'d expect them mostly for reporting purposes or backward compatibility, provided they are not updatable, because everybody updating this view would not be aware of changed semantics.</p>\n\n<p>If you dislike the join try\nSELECT * FROM  SalesLead where LeadSourceId IN (SELECT Id FROM LeadSource WHERE LeadSource = \'foo\')</p>\n'}, {'answer_id': 81642, 'author': 'Seb Rose', 'author_id': 12405, 'author_profile': 'https://Stackoverflow.com/users/12405', 'pm_score': 0, 'selected': False, 'text': "<p>In a typical application the user would be presented with a list of Lead Sources (returned by querying the LeadSource table) and the subsequent SalesLead query would be dynamically created by the application based upon the user's selection.</p>\n\n<p>Your application appears to have some 'well known' lead sources that you need to write specific queries for. If this is the case, then add a third (unique) field to the LeadSource table that includes an invariant 'name' that you can use as the basis of your application's queries. </p>\n\n<p>This shifts the burden of magic-ness from a DB generated magic number (that may vary from installation to installation) to a system defined magic name (that is fixed by design).</p>\n"}, {'answer_id': 82185, 'author': 'Hank Gay', 'author_id': 4203, 'author_profile': 'https://Stackoverflow.com/users/4203', 'pm_score': 0, 'selected': False, 'text': '<p>There\'s a false dichotomy here.</p>\n\n<pre><code>SELECT * FROM SalesLead \nINNER JOIN LeadSource ON SalesLead.FK_LeadSourceID = LeadSource.LeadSourceId \nwhere LeadSource.LeadSource = "foo"\n</code></pre>\n\n<p>doesn\'t break any more than the original</p>\n\n<pre><code>SELECT * FROM SalesLead Where LeadSource = \'foo\'\n</code></pre>\n\n<p>when <code>foo</code> changes to <code>foobar</code>. Also, if you\'re using parameterized queries (and you really should be), you don\'t have to change anything when <code>foo</code> changes to <code>foobar</code>.</p>\n'}, {'answer_id': 82628, 'author': 'Hank Gay', 'author_id': 4203, 'author_profile': 'https://Stackoverflow.com/users/4203', 'pm_score': 2, 'selected': False, 'text': '<p>Have you considered just not using an artificial key for the <code>LeadSource</code> table? Then you get to use <code>LeadSource</code> as the FK in <code>SalesLead</code>, which simplifies your queries while retaining the benefits of using a canonical set of values (the rows in <code>LeadSource</code>).</p>\n'}]
['https://Stackoverflow.com/questions/81533', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']