Unique Identity Values Across Multiple Tables |
|||||||||||||||||||||||||||||||||
|
In a previous tip, I made an off-handed comment about queries of the following form: SELECT x,y,z FROM t1 WHERE Condition1 UNION ALL SELECT x,y,z FROM t2 WHERE Condition2 to the effect that I couldn't think of many situations where that type of query would be useful in a normalized database. Well I was wrong about that. Suppose that t1 and t2 are tables that have exactly the same structure, but which are owned by two separate users. I'm talking about a situation where each user has their own set of normalized data, even if technically, the data when taken as a whole is not normalized. It is perfectly reasonable that one might want to create a view which includes both t1 and t2, to be made available to senior management within the adminisphere. Such a view might look a lot like the query above, only Condition1 and Condition2 would be identical, or completely absent. SELECT x,y,z FROM t1 UNION ALL SELECT x,y,z FROM t2 What if x is an identity column, and the primary key for both t1 and t2? If the value of x is taken from multiple tables then the UNION query above, may return duplicate values of x. You might ask "So what?". Well what if the administrator has a table of their own, which is linked to this view? To make this example more concrete, suppose t1 and t2 are lists of projects. Each project manager has their own project table. In T-SQL, these tables would be referred to as user1.Project and user2.Project respectively. Each user would then have their own tables representing sub-tasks or time-sheets for each project. Now suppose our hypothetical administrator has a table containing monthly performance evaluations for each project, and that table is called adminisphere.Evaluation
I hope you can see the problem here. To use the ProjectID as the foreign key into
the project table, our administrator would need to have a separate evaluation table
for each project.
|
|||||||||||||||||||||||||||||||||