How can i design a DB where the user can define the fields and types of a detail table in a M-D rela

Posted by Simon on Stack Overflow See other posts from Stack Overflow or by Simon
Published on 2010-06-01T17:23:24Z Indexed on 2010/06/01 17:43 UTC
Read the original article Hit count: 173

Filed under:
|

My application has one table called 'events' and each event has approx 30 standard fields, but also user defined fields that could be any name or type, in an 'eventdata' table. Users can define these event data tables, by specifying x number of fields (either text/double/datetime/boolean) and the names of these fields. This 'eventdata' (table) can be different for each 'event'.

My current approach is to create a lookup table for the definitions. So if i need to query all 'event' and 'eventdata' per record, i do so in a M-D relaitionship using two queries (i.e. select * from events, then for each record in 'events', select * from 'some table').

Is there a better approach to doing this? I have implemented this so far, but most of my queries require two distinct calls to the DB - i cannot simply join my master 'events' table with different 'eventdata' tables for each record in in 'events'.

I guess my main question is: can i join my master table with different detail tables for each record?

E.g.

SELECT E.*, E.Tablename 
FROM events E 
LEFT JOIN 'E.tablename' T ON E._ID = T.ID

If not, is there a better way to design my database considering i have no idea on how many user defined fields there may be and what type they will be.

© Stack Overflow or respective owner

Related posts about sql

Related posts about joins