A New (and Hopefully Better) Approach to Constants

  • I'd agree with post instructing everyone to hard-code the value into a variable at the top of your code although I would always do so by referencing a lookup table of some kind.

    Despite the fact that the value in the id column in your lookup table will never change you may find that your need to use that value in your code will. If you need to stop using that constant and replace it with another you need to be able to identify all your procedures/functions/views that use it.

    As an example, say a table that contains status information has the status "failed" split into three new values.

    Eg. Your original status FAILED (1) is split into FAILED - Pending (5) and FAILED - Notified (6) and FAILED - complete (7).

    Now you want to change all stored procedures that use the constant value of 1 and change it to 5 (Dont' ask me why you just do). How would you easily identify which of your stored procedures have used the old failed status of 1 if the only reference to the value in your stored procedures is a declaration followed a set statement of SET @Failed = 1?

    It's for that reason i'd advocate looking up the value in a lookup table within your code as much as possible. It is a simple thing to see which objects rely on other objects in your database if you refer to it within your code. If you simply hard code a value then well....good luck when you need to find it later.

    This ability to "find" where you have used constants could be achieved using functions but it is my preference not to do so.

    -- JP

  • I think I'm in the boat of not agreeing with this. Although I do like bleeding edge stuff being a developer AND a DBA, this just doesn't jive. I use constants in another way.

    All my Lookup tables are in one table (well, two if you count the parent table that is just there to categorize the values). The parent table ReferenceLists stores the distinct list of Lookup "Lists". The ReferenceValues stores all the values for each list. Each List has a constant and each value has a constant (optional). I use the Constant for the Lists all the time (which you'll see in the View Triggers). However, the Constants for the values are only used when needed. This strategy gives complete flexibility for adding new Lookup Lists where I can even allow end-users to create their own Lookups (in the case of a CRM application where users want to add their own User-Defined Fields and one of those Fields has specific values).

    [h3]Table Scripts[/h3]

    CREATE TABLE [dbo].[ReferenceLists](

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [Name] [varchar](50) NOT NULL,

    [Constant] [varchar](25) NULL,

    [CreatedDate] [datetime] NULL CONSTRAINT [DF_ReferenceLists_CreatedDate] DEFAULT (getdate()),

    [ModifiedDate] [datetime] NULL CONSTRAINT [DF_ReferenceLists_ModifiedDate] DEFAULT (getdate()),

    CONSTRAINT [PK_ReferenceLists] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY],

    CONSTRAINT [UIX_ReferenceList_Constant] UNIQUE NONCLUSTERED ([Constant] ASC) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    CREATE TABLE [dbo].[ReferenceValues](

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [ListID] [int] NOT NULL,

    [Name] [varchar](50) NOT NULL,

    [Abbreviation] [varchar](50) NULL,

    [Constant] [varchar](25) NULL,

    [Description] [varchar](255) NULL,

    [Obsolete] [bit] NULL CONSTRAINT [DF_ReferenceValues_Obsolete] DEFAULT ((0)),

    [CreatedDate] [datetime] NULL CONSTRAINT [DF_ReferenceValues_CreatedDate] DEFAULT (getdate()),

    [ModifiedDate] [datetime] NULL CONSTRAINT [DF_ReferenceValues_ModifiedDate] DEFAULT (getdate()),

    CONSTRAINT [PK_ReferenceValues] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    CREATE TRIGGER [dbo].[trg_OnUpdateReferenceValue]

    ON [dbo].[ReferenceValues]

    AFTER UPDATE

    AS

    BEGIN

    Set NoCount On

    -- Update the ModifiedDate on any Rows where it wasn't specified explicitly

    Update dbo.ReferenceValues Set ModifiedDate = GetDate()

    From dbo.ReferenceValues R

    Inner Join inserted I ON (R.ID = I.ID)

    Where (I.ModifiedDate Is Null)

    Set NoCount Off

    END

    GO

    ALTER TABLE [dbo].[ReferenceValues] WITH CHECK

    ADD CONSTRAINT [FK_ReferenceLists_ReferenceValues] FOREIGN KEY([ListID])

    REFERENCES [dbo].[ReferenceLists] ([ID])

    GO

    [h3]Usage[/h3]

    Now, to make this easier on developers and me, I usually wrap views around these values for specific cases (although it could all be handled in code).

    Let's say we want to store Address Types in the database. So we create an Address table and put AddressTypeID on it. Next we add a Foreign Key to the ReferenceValues table's ID column. We could also specify a Check Constraint that the value must only be one where the ListID = 1 ('ADDRESS_TYPE'). In order to maintain this list of values for AddressTypes, we create a nice little view for it.

    Notice how the View doesn't expose the "ListID" column because it isn't necessary. This view should be used as a Table. So, the INSTEAD OF trigger looks up the ListID auto-magically (inserting it if neccessary).

    [h3]"AddressTypes" View Script[/h3]

    CREATE VIEW [dbo].[AddressTypes]

    WITH SCHEMABINDING

    AS

    Select

    ID, Name, Abbreviation, Constant,

    Obsolete, CreatedDate, ModifiedDate

    From dbo.ReferenceValues WITH(NOLOCK)

    Where ListID IN(

    Select ID From dbo.ReferenceLists WITH(NOLOCK) Where Constant = 'ADDRESS_TYPE'

    )

    GO

    CREATE TRIGGER [dbo].[trg_OnUpdateAddressType]

    ON [dbo].[AddressTypes]

    INSTEAD OF INSERT

    AS

    Set NoCount On

    -- Determine the ListID

    Declare @ListID Int

    Select @ListID = ID From dbo.ReferenceLists Where (Constant = 'ADDRESS_TYPE')

    If (@ListID Is Null)

    BEGIN

    Insert Into dbo.ReferenceLists (Name, Constant) Values('Address Types', 'ADDRESS_TYPE')

    Set @ListID = SCOPE_IDENTITY()

    END

    -- Insert the Values

    Insert Into dbo.ReferenceValues

    (ListID, Name, Abbreviation, Constant, Obsolete,

    CreatedDate, ModifiedDate)

    Select @ListID, Name, Abbreviation, Constant, Obsolete,

    IsNull(CreatedDate, GetDate()), IsNull(ModifiedDate, GetDate())

    From inserted

    Set NoCount Off

    GO

    You can also add other columns to the ReferenceLists table (ex. SequenceNo if you want to display values in a specific order).

  • Doesn't this overly complicate what should be a simple design.

    If you want to store address_types then create a table called address_types and store them in there. If you need a lookup table for phone number types then create a table called phone_types and store them in there. If you start mixing 'types' of things in the same table then you have changed the table from being an entity of one thing to being an entity of many things and make a joke of what a foreign key constraint is supposed to be used for.

    What happens in future when you want to add more detailed information for an adress type? You cannot simply add a column and insert the data because your table does not only contain address_types. On top of that if you did decide to create a new address_type table then making all the modifications to your original design would be time consuming and complicated.

    All foreign key constraints used in a mixed lookup table can never be relied upon to be 100% accurate. You can keep adding check constraints in every table that uses your one lookup table but this once again complicates what should be simple.

    I prefer the design of many different lookup tables each unique and unambiguous in their purpose. It allows for foreign key constraint checks to be 100% accurate (what could be more important than accurate data) while having the flexibility to expand the tables columns as you see fit.

    If there is a concern that the number of lookup tables will exponentially grow and you wont know where anything is....I guess that is a different topic.

    -- JP

  • If there is a concern that the number of lookup tables will exponentially grow and you wont know where anything is....I guess that is a different topic.

    And hence forth, you know why I created this...

    Joe Celko (10/19/2007)


    Let me be brutal about this. I can see one -- and only ONE -- thing you have done right in this non-relational schema. This thing is so bad I want to use it in a book on bad SQL.

    Wow guys, pretty brutal. No constructive criticism? I used to have respect for you but obviously you just like to hear yourself talk. Apparently you think I am a naive database coder or something.

    Yes, I've read your article before and it basically has no merit. I've read ALL the pros and cons on this design! You made the whole thing way too complicated plus your little example table design was completely flawed from the beginning. No one in their right mind would use the design you mentioned. For one there's no Primary Key on it. I'm sure you didn't notice because you were too busy trying to think up witty quips, but I store the Primary Key of the values, not some Magical number.

    I only use these "reference" tables for simple lookup values (IDs & Descriptions). When the Lookup "Type" (i.e. AddressTypes) becomes too complex or merits additional schema, that is when I offload it into it's own table. Removing the current Foreign Key and a couple of update statements later, I can reapply the new F-Key to the new stand-alone lookup table. So, flexibility and future growth are accounted for.

    There are two sides to designing. The Logical design says "Hey we need 600 lookup tables!". The Physical design says "Umm, they're all the same schema and they only store IDs and Descriptions". It doesn't make sense to me to store 30 tables with an ID and Description. To me, designing databases is about storing the most data with the least bit of space. Even if the single lookup values table had many values, I've never seen mine grow above 500 rows. Too little for indexing to be effective but all the same I do set some indexes. It's also just right for pinning in memory. If this design becomes too large, then it's time to reevaluate and re-read the 1st paragraph. Just because you do not understand the design doesn't make it bad.

    You have no relational keys. Didn't you know that proprietary auto-numbering is NEVER a relational key by definition? All you have is an exposed physical locator, as if you were creating an index into a sequential file.

    What are you yacking about "Proprietary numbering"? I didn't realize the IDENTITY function was no longer used!? In case you don't understand, the Constant column is ONLY used when necessary for the values. Taking AddressTypes for example, maybe one row's Constant would be 'MAIN' and another 'MAILING'. The ID should never matter to a programmer, T-SQL coder or anyone. Constants do not change but IDs may.

    I've used this design for about 3 years now and never had a problem with it. I was able to consolidate over 30 "lookup" tables. Yes, there are Foreign Keys. Every table that uses this as a Lookup HAS a Foreign Key pointing back to the Value ID. Obviously one flaw of this is that it's possible that one ID from another List ends up in your column, but how often have I had it beat into my head that "Business logic doesn't exist in the database". So, if you're that stupid of a programmer to give a user a drop-down of ALL the values instead of just those that belong in that column, then shame on you.

    Did you know that keeping audit data in a table is illegal? And that if it were not illegal, it is stupidly dangerous. Do you also keep the log file on the same hard disk as the DB? DUH!

    Also, what crack pipe are you smoking, who mentioned anything about auditing? I manage all my auditing through a Service Broker which hands off the audit data to a separate database asynchronously so it doesn't block inserts / updates. The modified dates are used to ensure there are no concurrency conflicts when two users are attempting to update the same row. The trigger is in place in case some stupid programmer creates a Proc that doesn't specify the ModifiedDate.

    Do you have any idea why the BIT and BIT VARYING data types were removed from SQL? They are assembly language and are a sure sign of design flaws.

    Oops, you got me on one thing! A Nullable Bit field! Overlooked on my part. However, bits are very useful and should not be counted out. I guess if you were king then all FLAGS in every programming language would be removed as well? What happens when I need to store multiple Statuses for my Application Members (ex. LockedOut but Active)? I suppose you'd tell me to add another bit column? And when that changes to another status that needs to be combined into the first two? Another column?

    Since the rest of the schema is soooo screwed up, you are trying to patch it with procedural code and you did it in the worst way. Use a trigger to replace a DEFAULT???

    Apparently you have no idea what the trigger is actually doing. AND Default values are only inserted for new records. If you didn't notice, the Trigger is AFTER UPDATE. Again, you love to talk before you read.

    I didn't just fall off the boat yesterday and I didn't wake up one day and think this would be a great idea. I've proven it's usefulness time and again over many years and I will continue to use it.

    I wonder, since this design is so non-relational in your book, how I would love to see your proposed solution to a GIS database.

  • If there is a concern that the number of lookup tables will exponentially grow and you wont know where anything is....I guess that is a different topic.

    And hence forth, you know why I created this...

    It would be good to have the simplicity of your single lookup table with a simple design back end. Perhaps wrapping many lookup tables in a single access point through a view is an option but like I said that is a different topic. If the design works for you then it works for you.

    On another note I do appreciate you sticking your head out and writing the article on what is a common database design issue. I'd hate to think that I had contributed to an environment where discussion was negative and not constructive. You have your opinion on your design and its merits and I gave a response. Let's leave it at that.

    -- JP

  • Jonathon Prosper (10/21/2007)


    On another note I do appreciate you sticking your head out and writing the article on what is a common database design issue. I'd hate to think that I had contributed to an environment where discussion was negative and not constructive. You have your opinion on your design and its merits and I gave a response. Let's leave it at that.

    [p]Jonathon, it is a little confusing as to whom you are speaking in that last paragraph since you mostly address the post by tymberwyld, although you mention "the article", which I wrote. Regardless, I think you touched on a subject that needs to be discussed, and that is the level of politeness and professionalism in these discussion forums. I do not mind that there are several responses to my article that very much disagree with what I was suggesting because, as you pointed out, everyone is entitled to their own opinion. However, I am a bit shocked at some of the pointedly rude, insulting, and down-right unprofessional comments made by a few people.[/p]

    [p]There is absolutely NO REASON whatsoever for anyone in these forums to be rude to someone else. We aren’t talking politics here, and even if we were it should still be polite. We are all database professionals trying to learn from one another and these forums -- as a means of information and idea exchange -- are a great way for people to learn. But it is a shame when people who are new to this learn that they most likely shouldn't ask questions or share a solution of theirs out of fear of being put-down for it. If someone is knowledgeable enough to know a better way to approach the problem, then it is quite easy to simply say: I would approach it in this way as it has these merits. For example, Joe Celko's first response to my article did just that. He stated that it should be a one-row table, or even a view, and that the CLR poses certain problems. Ok, fine. But I am really bothered by Joe's response to the post by tymberwyld in which he copies the code only to point out, line by line, how much he doesn't like it and also calls it a nightmare and some of the worst code he ever saw. Personally, whether anyone feels that way or not, it is simply not acceptable on any level for someone in these forums to speak to anyone else in that manner and tone. There is just no good reason for it.[/p]

    [p]Joe, given how well-known you are from writing various books on the subject and being widely regarded as a SQL expert, I would really think that you could use your wonderful insight into these technical issues to more positively teach others better way of doing things; yelling at someone and calling them stupid actually discredits both you and our profession by making us look like the stereo-typical techies that are lacking certain social skills. And once such an attitude comes out in these forums it is all too easy for others -- especially the one being yelled at -- to respond in kind and hence what was a productive discussion turns into immature insults. If someone posts code that appears hideous in your knowledgeable opinion, then rant and rave all you want in your mind, but when writing an actual response please be courteous. You have a lot of knowledge to impart upon others and it is great that you share that with us, but it is hard to listen to even a correct answer when the format the answer is given in is so harsh; I am not even the person being yelled at in this instance and I still don't want to take seriously any part of your response to tymberwyld, no matter how correct you may be, simply due to the tone and insults.[/p]

    [p]But, I am not singling out Joe since I have seen several posts in these forums that are also insulting and that just has no place here. We are supposed to be helping each other out, not competing for "I am better than you" points. By all of us being respectful of each other we will create a much stronger and more productive community. :)[/p]

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Jonathon, it is a little confusing as to whom you are speaking in that last paragraph since you mostly address the post by tymberwyld, although you mention "the article", which I wrote.

    Yes I meant the post. My slip.

    -- JP

  • Sorry to necro this thread, but I was looking for alternative solutions and came across this. Thought I'd throw my 2p in.

    I've had this problem a lot recently. My solution was to create a single scalar UDF, with a CASE inside it, which I passed a constant name in to, ie:

    @myValue = dbo.Constant ( 'asset' )

    rather than the more verbose:

    SELECT @myValue = Constants.Value FROM Constants WHERE Constants.Name='asset'

    I don't like use of CLR for this to be honest and applying UDF in queries is imho a disaster for performance in many cases.

  • Be cautious. Not all of these approaches optimize out the same.

    We use an object factory that is tightly coupled to database tables. To simplify data access we have a single view that becomes in essence a partitioned table, called dbo.Object

    This view looks something like this:

    create view dbo.Object

    as

    select 1 as objecttype_id, ID, Description from dbo.object1

    union all

    select 2 as objecttype_ID, ID, Description from dbo.object2

    union all

    select 3 as objecttype_ID, ID, Description from dbo.object3

    union all

    select 4 as objecttype_ID, ID, Description from dbo.object4

    Now to query against that view, you can simply predicate on an objecttype. If you check the IO statistics, it is only looking up against the one table, provided you query it properly. Don't bother looking at the Query Plan as it does not truly reflect what the server does to fetch the data.

    use this to check io stats:

    set statistics io on

    --this query works properly

    select * from dbo.object

    where objecttype_id = 2

    --this query does not, as it evaluates each table, but at least it doesn't scan them.

    declare @jobtype int

    set @objtype = objecttype.object1() -- a udf that returns a value

    select * from dbo.object where objecttype_id = @objtype

    --this query is closer, but it is creating a working table and is evaluating for every row

    select * from dbo.object where objecttype_id = objecttype.object1()

    --tried this, hoping that the deterministic decisions were based on the input param to output, but to no avail

    --results were same as above.

    select * from dbo.object where objecttype_id = objecttype.object1(0)

    --Same result

    select * from dbo.object where objecttype_id = objecttype.GetType('object1')

    --this is the worst, as it scans each table

    select * from dbo.object

    where objecttype_id = (Select objecttype.object1())

    Joining on a settings table is an option, unfortunately as we may be doing multiple joins in tables requiring these constants, we would have to do multiple joins on the settings table, which turns out to be very inefficient.

    So until we have a better CONST struct in SQL we will be forced to hardcode values for some of our queries.

Viewing 9 posts - 16 through 23 (of 23 total)

You must be logged in to reply to this topic. Login to reply