If you were building a database with tables Documents, Clients, Users and Firms, where in Firms you would store the companies that use the software how would you design the first three tables to support multiple Firms to store in them? So, in Documents we want to store documents for all the firms, of course, we need to tell them appart somehow, so we would need a column like FirmID. We can also put this in Clients and Users.
Now the next requirement is that each firm can have its own IDs for documents, clients, because obviosuly when we add a new firm, their IDs for whatever they create should start at 1.
I was thinking something like this but it requires manual construction of all the fields but RowID.
CREATE TABLE [dbo].[ClientTest](
[RowID] [int] IDENTITY(1,1) NOT NULL,
[FirmID] [int] NOT NULL,
[ClientFirmID] [int] NOT NULL,
[ClientFirmPrettyID] [varchar](10) NOT NULL,
CONSTRAINT [PK_ClientTest] PRIMARY KEY CLUSTERED
(
[RowID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
RowID will run automatically in this case but it’s useless for us because for everything we do we need to use ClientFirmID and ClientFirmPrettyID. Is there a way to automate the creation of these two ?
It is a good practice to use database facade pattern (views, stored procedures). They hide all database internals (structure) from other world. I think, it is another point that is security. Every firm wants to have restricted access to some data on table level, (not row level). The second point is: performance. It is better have many tables than one big one.
So I think, it is better to leave tables of every firm as they are and create a view for reports (I think):
or
ADDED:
Use trigger or stored procedure to generate the next id for some firm and scope (‘clients’ or ‘documents’):
UPDATED:
Insert
UPDATE dbo.zz_IdGenerator...into stored procedure, and call it before insert to the document or client tables.