Solved

Table of Types Examples

Posted on 2014-03-18
2
257 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:
Scott Pletcher 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

773 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