0

I have a SQL 'architectural' design doubt, so I ask about it here... Excuse me if it does make little sense, I'm quite new to SQL... :-(

UPDATE: As I did just learn thanks to a comment, this is a "multi-tenant" issue...

My app is a kind of a "contacts manager".
It will be used by some "user"s, who have access to some "person"s data.
Users and persons sets are disjoint.
Person's data is periodically populated by the app, server side.
Users can interact with persons data, adding - for example - some "notes", or some additional "phone" number.
The user-added data should be (RW) accessible only to the user who owns it, and not visible to other users; instead, the system-added data should be (RO) accessible to any user.

The interested tables are something like:

--------------------------
 user
--------------------------
 id | username | password
--------------------------

 

------------------------------------------------------
 person
-------------------------------------------------------
 id | key | name | age | sex | address | phone | notes
-------------------------------------------------------

The "id" fields are "integer autoincrement primary keys", while the "key" field is a unique identifier for the person.

The best solution I came up with until now is to add a new record for each user's data added, adding a "id_user" field to the person's table layout, where to store the id of the user who owns that record of data.
So the person's table should become:

-----------------------------------------------------------------
 person
-----------------------------------------------------------------
 id | key | name | age | sex | address | phone | notes | id_user
-----------------------------------------------------------------

(of course "common" data should have a "id_user" field content of null, or some special reserved value.)

I don't entirely like this solution, because it duplicates records for each person, so extraction queries will become more complex, and persons table will possibly grow very big...
I could think for example about using different schemas for different users, but I don't know if this is reasonable/feasible...

The question is: Do you consider this solution acceptable, and/or do you see any better alternative?

P.S.: I'm currently working with sqlite, but a standard SQL answer should be preferable...

6
  • Can you specify what "common" data actually is? Mar 12, 2015 at 12:47
  • Of course, sorry... I mean data which is accessible (read-only) to all users...
    – MarcoS
    Mar 12, 2015 at 13:42
  • " Users and persons sets are disjoint." So, if I'm one of your users, I can't be anyone else's contact? Mar 12, 2015 at 15:31
  • In practice, no, they are completely different environments. It could in theory happen, but I suppose it doesn't matter, for this example... Probably you are right, my statement "Users and persons sets are disjoint." is probably unuseful as far as this question is concerned...
    – MarcoS
    Mar 12, 2015 at 15:40
  • 1
    @Mike Sherrill 'Cat Recall': Thanks! Your comment was very useful to me! I didn't even know the word "multi-tenant"... :-(
    – MarcoS
    Mar 12, 2015 at 15:47

1 Answer 1

1

I think, depending on how complex schema should be, there are several ways to improve it. In any system you should decide what data you will need to select, edit or delete commonly, it can really help to choose right model.

  1. Complete separation users from persons:

    users (id, username, password, name, age, sex, address, phone, notes)
    persons (id, key, name, age, sex, address, phone, notes)
    

    If you querying only users info or only persons info at a time, there is no purpose to join their info in one table.

  2. Storing profile data in separate table:

    users (id, username, password, profile_id)
    persons (id, key, profile_id)
    profiles (id, name, age, sex, address, phone, notes)
    

    It can help querying for phones of all users and persons in one select without union.

  3. Reverse depending:

    users (id, username, password)
    persons (id, key)
    profiles (id, type, parentid, pname, age, sex, address, phone, notes)
    

where type is 0 for users, 1 for persons, parentid is id from users or persons. Sort of eav model, would help someone need several profiles.

  1. If multiple profiles are in use (maybe future use) better way than 3. is to have transitional table:

    users (id, username, password)
    persons (id, key)
    user_or_person_profiles (type, parentid, profile_id)
    profiles (id, pname, age, sex, address, phone, notes)
    

    It also can help with person, which is user too (maybe disjoint of persons and users is temporary).

  2. Opposite to previous, you can join users and persons in one table

    people (id, groupid, username, password, key, name, age, sex, address, phone, notes)
    

    where groupid could be predefined const (0 for users, 1 for persons), or id from groups table, which can be used to build "group-role-permission" user rights system as complex as you need or imagine.

Again, what schema is better only you can decide, knowing what data and how often you will manipulate.

Hope, it will help somehow...

4
  • Thanks for your very complete and straightforward answer! I think I will go with: 3. Reverse depending (modified, since I don't need users profiles, I will store all their attributes in a flat table) users (id, username, password, ...) persons (id, key) profiles (id, person_id, name, age, sex, address, phone, notes, ...) One more question, sorry...: will I be able to select all profiles for one person with just one select?
    – MarcoS
    Mar 15, 2015 at 12:47
  • Sure. Just select * from profiles where person_id = ? and you'll get them all. Mar 15, 2015 at 15:37
  • I mean, select values from persons and profiles tables, combined?
    – MarcoS
    Mar 16, 2015 at 11:10
  • Using common join select persons.*, profiles.* from persons join profiles on persons.id = profiles.person_id where persons.id = ? Mar 17, 2015 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.