Database Schema for Individual and Group accounts(Like Facebook Users and Pages)
You should use different tables for different entities.
- Users
- Groups
- Moderators (contains id of corresponding users and moderated by them groups id)
- Followers (contains id of users and followed by them groups)
Tables 3 and 4 provides such called "many-to-many" relationship (multiple users can follow multiple groups), as databases support only one-to-many or one-to-one relationship from the box
database design post privacy
You could add table like "post_access" with two columns post_id and user_id.
There are some possible tricks to prevent fast growing of this table:
- Author of the post could see it without adding of record to post_access
- Post table could have flag field "ispublic", and this post is visible to every one
- You could create user groups (additional table) and use post access not for users, but for groups (this make sense if you expect some grouping of users)
Facebook like data structure
If you want to represent this sort of structure in a relational database, then you need to use a hierarchy normally referred to as table inheritance. In table inheritance, you have a single table that defines a parent type, then child tables whose primary keys are also foreign keys back to the parent.
Using the Facebook example, you might have something like this:
User
------------
UserId (PK)
Item
-------------
ItemId (PK)
ItemType (discriminator column)
OwnerId (FK to User)
Status
------------
ItemId (PK, FK to Item)
StatusText
RelationshipUpdate
------------------
ItemId (PK, FK to Item)
RelationshipStatus
RelationTo (FK to User)
Like
------------
OwnerId (FK to User)
ItemId (FK to Item)
Compound PK of OwnerId, ItemId
In the interest completeness, it's worth noting that Facebook doesn't use an RDBMS for this sort of thing. They have opted for a NoSQL solution for this sort of storage. However, this is one way of storing such loosely-coupled information within an RDBMS.
Database Design for a system that has Facebook like groups
I've got an idea, which is a workaround basically: have another table like: group_type in which you have id(the PK) and then you have tablename (the full table name of the type).
Then, you should have a FK from your Group table linking to this group_type table.
id tablename
--------------------
1 School Group
2 Interest Group
After all this is done, you could build your queries based on the values from this table, as an example:
JOIN (SELECT tablename FROM group_type WHERE id=group.group_type_id) ON ..
Related Topics
SQL Query to Select Distinct Row with Minimum Value
In SQL/Mysql, Differencebetween "On" and "Where" in a Join Statement
How to Insert Values into a Table, Using a Subquery with More Than One Result
How to Do a Max(Count(*)) in SQL
How to Count Instances of Character in SQL Column
Generate a Range of Dates Using SQL
Postgresql: Encoding Problems on Windows When Using Psql Command Line Utility
What's the Proper Index for Querying Structures in Arrays in Postgres JSONb
What Does Sp_Reset_Connection Do
Equivalent of Oracle's Rowid in SQL Server
How to Convert a SQL Server 2008 Datetimeoffset to a Datetime
SQL Server Replace, Remove All After Certain Character
How to Run a Stored Procedure in SQL Server Every Hour
How to Delete Duplicate Rows with SQL
How to Convert Comma Separated Nvarchar to Table Records in SQL Server 2005