We have a client-server system that uses SQL Server 2012 as a database. The system is updated and enhanced on a regular basis.
We recently realized we have a recurring pattern of table design: a table that links the Users table to other tables in the database, forming a basis for views and permissions. Example:
CREATE TABLE Documents_Users (
ID int not null identity(1,1) not for replication,
UserID int not null, -- foreign key to the Users table
DocumentID int not null, --- foreign key to the Documents table
[...]
)
We were thinking, that instead of creating all of these tables, we might create one large table like this:
CREATE TABLE UserPermissions (
ID int not null identity(1,1) not for replication,
UserID int not null, -- foreign key to the Users table
ObjectID int not null, -- pointer to any object ID in another table
TableID int not null, -- actual id of the referenced table in sys.objects
[...]
)
Pros: one table solves all of our permission needs for all current and future tables.
Cons:
no foreign keys. we will have to create a job that runs every once in a while and deletes orphan pointers (if we don't just delete them in stored procedures).
Views will need to query along the lines of
select * from [some_table] where ID in ( select ObjectID from UserPermissions where TableID = object_id('someTableName') )
Is it worth it, and a good practice, to create a generalization like this? Would there be a performance gain from having one table and one index to read, instead of possibly many different tables?