Database design - best way to store templates and instances
Our database calls for having templates, for instance having a meeting template with a certain group of people who attend the meeting along with a base meeting name. The meeting can then be "instantiated" so that you can make copies of that meet开发者_如何学Going and modify the attributes. The best ways I can think of doing it are to:
- Store the "instances" in the same table and store related information similarly.
- Make different tables with the same attributes and store the templates in one table and the "instances" in another table.
I would store the templates in a separate table that mirrors the necessary columns in the instance table. That way, you won't need to put special logic anywhere to filter out one or the other type (template or instance) when looking for a specific type of data. Putting the templates in another table will allow you to add more metadata to the template if you need to in the future.
How about using one table, but adding a flag field like "isTemplate" to the table, which will serve to identify a record as the default or 'template' record.
You create a new record which has the default parameters, and set isTemplate as "1" or TRUE or YES or whatever.
When creating a new meeting, then just create is as a copy of the template, like so:
INSERT INTO meetings(place,people,time)
(SELECT place,people,time FROM meetings WHERE isTemplate = '1')
If you have multiple templates, then you will get a list of the available templates as:
SELECT * FROM meetings WHERE isTemplate = '1'
and then instantiate one of those templates by the primary key rather than 'isTemplate':
INSERT INTO meetings(place,people,time)
(SELECT place,people,time FROM meetings WHERE ID = 'xxxx')
精彩评论