Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Table of Types Examples

Posted on 2014-03-18
2
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Assisted Solution

by:Paul MacDonald
Paul MacDonald earned 1000 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

636 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