Solved

Table of Types Examples

Posted on 2014-03-18
2
255 Views
Last Modified: 2014-03-21
I've got dozens of "type" tables that are mostly identical and I would like to consolidate them all into a handful of tables. (i.e. a table of types)

For example, the tables Permit Type, Employee Type, Line Type, Vendor Type, etc.  all have the same basic columns:
X_Type_ID
X_Type_Name
Display_Color
Display_Order
Created_Date
Created_By
Last_Updated_Date
Last_Updated_By

There are a few that might have an additional attribute column, but for the most part they're just an ID and a Name.

Does anyone have a link to an example of creating a table of types, or just advice in general?
0
Comment
Question by:Torrwin
2 Comments
 
LVL 34

Assisted Solution

by:Paul MacDonald
Paul MacDonald earned 250 total points
ID: 39937447
Just because they're all structured similarly, doesn't mean you should consolidate them.  It also doesn't mean you shouldn't.  What matters is how you query them.

That said, you could create a new table, identical in structure to what you already have, but with a couple additional columns indicating what the record is a type of (and an index for the new, consolidated table), so something like...

ID
MetaType <- something indicating Permit, Employee, Line, Vendor, etc.
X_Type_ID
X_Type_Name
Display_Color
Display_Order
Created_Date
Created_By
Last_Updated_Date
Last_Updated_By

Bear in mind, this will necessitate rewriting ALL the queries that use these tables.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 39938248
It's usually best to use a numeric code for the "metatype" / lookup_table_code for performance reasons.  Another table can contain the descriptive name of the underlying table.

For example, table "Permit Type" might be 1, "Employee Type" might be 2, etc..

Often a smallint allows enough values, you don't typically have to use a full int.

The combined table should be clustered on ( lookup_table_code, <lookup_value> [X_Type_Id] ).

That is, queries would still join to, say, table [Permit Type], but now it's a view, like this:
CREATE VIEW [Permit Type]
AS
SELECT
FROM dbo.lookup_table
WHERE
    lookup_table_code = 3 --<or> "= (SELECT lookup_table_code FROM lookup_table_directory WHERE name = 'Permit Type')"


>> Bear in mind, this will necessitate rewriting ALL the queries that use these tables. <<

Not necessarily.  You could instead create views with the same names as the existing tables.  Indeed, I'd strongly suggest using the combined lookup table that way anyway, rather than having each query specify the lookup table code.

Of course, once the view is created, it replaces the original table name completely, so the name then only points to the new table.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now