How would you create and store user-defined custom fields in a SQL database?

后端 未结 3 1265
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 16:08

I need to allow users to add new fields to a record, e.g. if there is a Contact record, a user may want to add a \"SSN\" numeric field and a \"Birthdate\" date/calendar fiel

3条回答
  •  春和景丽
    2020-12-29 17:07

    Have a table that stores the field names and types.

    field_ID     INT
    field_name   VARCHAR
    field_type   ENUM('int','float','text','richtext')
    

    Have a table that stores a link to an entry in the record table, a link to an entry in the field table, and the field value.

    fieldvalue_fieldID   INT
    fieldvalue_recordID  INT
    fieldvalue_value     BLOB
    

    Making it searchable is another challenge - you would need to grab any searchable content out of that fieldvalue_value and index that. This would be database-specific. In MySQL you could make that a TEXT value and add a MySQL FULLTEXT index on it.

提交回复
热议问题