[user Database model][1]
https://www.linkedin.com/feed/update/urn:li:activity:6345250684744630272
here is my database design for user I want to add user_ following table in this scenario can anyone help me to build user_follower table scheme?
1- Each User has many other Users that followed him
2- Each User can follow many Users
So you have a many-to-many Unary relationship on User in your ER.
Now, to Mapping this data model to Tables: you can use a new table named: User_Followers and set 2 IDs of User as foreign key named source and target of following (and other attributes like date of follow and etc.)
you can find other mapping many-to-many Unary relationship (like Bit-Wise or Graph Data Models), but to your project, I offer as I mentioned.
Related
I'm creating a db schema that involves users that can have multiple users.
I want to register different companies to use the web services.
For example:
user A or B (etc) can signup and create a company account
user A can create multiple accounts of other users with their types, similarly user B
If user A or B create different accounts, how would I know this particular user is belong to User A or B company ? I think user table have many to many relationship with itself (like basic friendship design).
Please suggest the best design .
Ex.
User 3,4 belongs to User A
User 5,6 belongs to User B
In general, I would recommend starting by identifying all the entities you are trying to persist. It sounds like you have two distinct entities in your question. One being "user," which represents a single person. Your second entity is "company." A "user" can belong to a company.
An example of a database design would be one table for users, and one table for companies. In the "users" table, you would want to have a foreign key column that references the primary key (unique id) of the company the user belongs to. If each user can only belong to one company, this becomes a simple one to many relationship.
In short, I would highly recommend treating company accounts separately from user accounts, since they are fundamentally different entities.
I am very new into backend stuffs like databases. That being said, I lack the database design knowledge.
I am wondering how and what is the best design for my scenario.
I am creating a social network app where the users can create groups and join other groups. Those groups have places in it. Those places are created by the User in the group
User
Group
Place
Rules:
One User can create and join many Groups
One Group can contain many Users
One Group can have many Places
Each Group have an admin User
I currently have 3 separate tables
1. USER TABLE
ID, EMAIL, USERNAME, PASSWORD, PROFILE PICTURE
2. GROUP TABLE
ID, NAME
3. PLACE TABLE
ID, NAME, COORDINATE, RADIUS
I am extremely confused in designing the proper database for it it.
Question:
How should I design the relation of the table.
I would like to have one User (maybe more) in a Group that has the permission to do certain stuff that normal User cant do. Such as Creating New Place, Deleting A Place
How should I define my table structure?
Any thoughts please? Any help is greatly appreciated!!
Thank you
You will need to have one more table in order to create what is known as a many to many relationship between the users and the groups.
Since you didn't specify the rdbms you are working with, I'll use SQL Server for my code:
CREATE TABLE TblUserToGroup
(
UserToGroup_UserId int FOREIGN KEY REFERENCES TblUser(UserId),
UserToGroup_GroupId int FOREIGN KEY REFERENCES TblGroup(GroupId),
UserToGroup_IsAdmin bit DEFAULT 0
CONSTRAINT UC_UserToGroup UNIQUE(UserToGroup_UserId, UserToGroup_GroupId)
)
As you can see, there is also an IsAdmin column that can take values of 0 or 1. If you only want one admin user for each team, you can add a check constraint to prevent having more then one admin for each group.
Seems I am not allowed to post images, so let me describe the image. It is a SQL table diagram showing the relationships between 4 tables. The Tables are:
People
Id
FirstName
LastName
PhoneNumbers
Id
Number
PhoneNumberTypes
Id
Name
Description
PeoplePhoneNumbers
PersonId
PhoneNumberTypeId
PhoneNumberId
The two main tables are People and PhoneNumbers. There is also a PhoneNumberTypes that describes the type of PhoneNumber (Home, Work, etc).
The PeoplePhoneNumbers table serves as a Many-To-Many relationship table between People and PhoneNumbers. However it also connects to PhoneNumberTypes to describe the relationship.
I have been trying to figure out how to handle this Entity Framework because EF does not allow you to add additional information to the Association(Many-To-Many) Table.
Besides the PhoneNumberType info, I also find that their are additional data pieces I need to record in the Association Table like "Start Date", "End Date", etc.
The only solution I have come up with so far is to create an entity in EF that combines the fields in PhoneNumberTypes, PhoneNumbers and PeoplePhoneNumbers into a single entity. Then use SQL stored procedures for CRUD operations against it.
I would prefer a more EF centric solution. Does anyone know of one?
PhoneNumberTypes should not be linked to PeoplePhoneNumbers. Let PeoplePhoneNumbers be what it is and only a junction table. PhoneNumberTypes should be linked to PhoneNumbers. EF should be able to create this setup by convention. Additional information describing phone number should also be linked off of PhoneNumbers. This design adheres to more Domain Driven Design (DDD) principles and also generates a better database design as well.
Is it better to create tables that store a lot of data that are related to an entity (User for example) or many tables to store said data?
For example:
User Table
Name
Email
Subscription Id
Email Notifications
Permissions
Or
User Table
Name
Email
Subscription Table
User ID
Subscription ID
Notification Table
User ID
Receives?
... etc
Please consider code in this as well, or I would have posted to ServerVault.
From a relational design standpoint what is important is the normal form you're aiming for. In general, if the "column" would require multiple values (subscription_id1, subscription_id2, etc) then it is a repeating group, and that would indicate to you that it needs to be moved to a related table. You've provided very general table and column notes, but taking a cue from the fact that you named "Email Notifications" and "Permissions" with plurals, I'm going to assume that those require related tables.
I have a database with a "users" table containing data about my users. Each user is to be linked to a company or a college. I wish to have two separate tables "college" and "company" each with a field "ID". So how do I link each record in the users table to either a company or a college?
The basic thing is that I wish to establish an "OR" relationship in the database.
You can use subtype/super-type relationship. Keep all common fields in the organization table. College and company tables contain only fields specific to those entities.
You could use an 'institution' or 'organisation' lookup table, with a structure something like
InstitutionId[PK], InstitutionType, LookupKey
where LookupKey is the PK to either Company or College.
Or,
InstitutionId[PK], CompanyId[FK], CollegeId[FK]
In both cases you link from user to institution, then onto Company and/or College.
I personally prefer the second option, because it allows you to easily validate the FK relationship and also allows (if applicable) for a user to be a member of a company and/or a college.
I would create relationtables. UserCollege and UserCompany. This way you are even able to have users that are linked to both if needed in the future. If not you simply just create a relationrecord on one of the both