I am looking at using a SQL software to store and query objects with a loose schema.
We know that an object corresponds to a row of an SQL table, and it’s attributes correspond to columns. By ‘loose schema’, I mean that an object can have attributes that are not hardwired to the table’s (rather tables’) structure. An arbitrary (attribute,value) can be attached to an object.
Let me illustrate with a specific example:
For the given objects:
<subject>
name:data structures
tag:CS
description:easy
desired_seniority:5.0
<subject>
name:microprocessors
tag:CS,electronics <multi-valued
description:easy
desired_seniority:5.5 <numeric
and
<teacher>
name:John Doe
tag:electronics
seniority:5.8
The query will be
For each teacher,
return all subjects whose tags match and
whose desired seniority is less than or equal to the teacher's
The teacher ‘John Doe’ will be matched with subject ‘microprocessors’ because they share the tag ‘electronics’ and his seniority 5.8 is greater than the subject’s desired seniority 5.5
Note that I have used numeric (queries do comparison) and multi-valued (queries do an ‘is in’ match) string in the strings.
What exactly: I am looking for the right data model and corresponding queries that lets me simulate a loose schema on a SQL software.
I accept these constraints:
- teacher and subject may be described by distinct tables
- there will be one to one correspondence of attributes (if a query uses attribute ‘x’, all rows have attribute ‘x1’ and ‘x2’ of same type)
I saw http://www.igvita.com/2010/03/01/schema-free-mysql-vs-nosql/ and https://github.com/jamesgolick/friendly .
Yes, there are NoSQL databases and special ORMs. We use Solr (it’s great!). But I would avoid increasing the complexity of the project by including them. The performance overhead of emulating a noSQL store is not a concern.
In MySql I would use something like this:
And to get teachers with matched subjectes: