When to use pivot tables in Laravel - mysql

I have a one-to-many relationship for Gym to User relationship. That being a Gym can have multiple users, but a User can only have one gym.
Is it better to have a pivot table for this to link gym_id to user_id like follows?
Gym_User Table:
gym_id | user_id
or in the User table like follows?
User Table:
id | name | password | gym_id

Pivot tables are used when having many-to-many relationship.But for your case, you have One-to-many relationship. And don't need a pivot table.
A typical example of many-to-many relationship is Article & Tags. Where 1 article can have many tags and 1 tag can belong to many articles.
But yours is simple one-to-many relationship.

You have a 1 to many relationships, in this case you donot need pivot table, gym_id is foreign key in the user table and you will define relations on the models.
In gym model you will define a hasMany relationship to user model and in user model define belongsto relationship to gym model. (Refer to Laravel documentation).
One of nice example for pivot table is Access Controller Models
Such as User,Role and Permission..

Related

Odoo 10 : How to create many2many field?

Is there any explanation with example of many2many fields? What is the difference between relational tables and models ??
suppose you have two table teacher and student...
student has multiple teacher and teacher also has multiple students so here we can not apply many2one and many2one relationship so we need to add many2many relationship on this two tables...
suppose in odoo we have two model 'student.student' and 'teacher.teacher' so we can apply many2many relationship like this in student.student model
techer_ids = fields.Many2many('teacher.teacher', 'student_teacher_rel', string='Teachers')
how it works,
it will create different table in database with name student_teacher_rel .
this table has two column teacher_id and student_id both are foreign key of student table and teacher table...
so we can easily manage relationship between them...

MySQL: What is the best way to store user_type and user_type_id?

I created 3 tables for store users, students and examiners.
Users table contain login details
id
name
email
password
user_type (3 user types: 1 - administrators, 2 - students, 3 - examiners)
user_type_id
If user_type == 2 or 3, then based on that value, user_type_id field get
student id or examiner id, if user_type == 1 then value == 0.
students table contain student details
id
name
age
birthday
examiners table contain examiners details
id
name
age
qualifications
my question is, is it ok to use above way to link users, students and examiners?
If not please suggest a way. Thank you.
First of all: your data model needs some improvements.
You put age attribute and birthday attribute in Student. Adding age in database is incorrect. You should use only birthday. Same problem in Examiners.
The best practice for designing data model for Information Systems with Authentication (and Authorization) is to separate them into 2 modules in Data Model. AA module and Other parts of Information System. This separation has som advantages like extendability of your whole system and usabilty of AA in other systems and etc.
Another improvement is to define a new Entity named UserType and use the foreign key in Users entity.
When you use both Student ID and Examiner ID in one attribute in Users entity, your model is not Normal and you can not set foreign keys.
Secondly: To provide a data model
we have relationships between Users and two other entities (Student and Examiners). Maybe in future other entities added to this list.
We have 2 relationships:
one relation between Users and Student and another relation between User and Examiner
These relationships are one-to-one. For example each Student has one User and each User can be set for only one Student. And similar statement between User and Examine.
In one-to-one relationships we have 2 solutions:
1- pass primary key of Entity at side 1 in 1-1 relationship as foreign key to
Entity of side 2.
2- pass primary key of Entity at side 2 in 1-1 relationship as foreign key to
Entity of side 1.
Both solutions are true and Normal. So we have 2 solutions for this case:
Solution 1: transfer User ID as foreign key to both Student and Examiner. In this solution we can easily find each User ID of specific Student or specific Examiner. But the hard part of this solution is finding a Student or Examiner based on User ID. Answer is: Notice that we have User Type for each User ID. So based on User Type of User ID, we know which table to be search (Student or Examiner).
Solution 2: transfer both Student and Examiner IDs as foreign keys into Users. Meaning that User Entity have 2 foreign key. First for Student ID and Second for Examiner ID. This solution seams so effective. But it conflict Modularity of AA Module (Authentication and Authorization Module). Another problem emerge when the number of other tables (Student, Examiner,...) that have relation with User is HIGH. Anyway, in small Information Systems we can use this solution.

Foreign Key in SQL pivot table - database design

I am using DBDesigner 4 for designing my database relations.
I have a users table and a recipes table. One user can own many recipes but one recipe cannot be owned by many users. This relationship is shown by the user_recipes relation in the picture. (A one-to-many relationship from users to recipes).
However, recipes can be liked by users. Many users can like many recipes. This is a many-to-many relationship between users and recipes and the pivot table for this is users_like_recipes.
But when I create this pivot table, I only need the users_id and recipes_id column. The recipes_users_id column is getting added on its own and I am not able to remove it. It says the third column has come from another Relation which is defined in the model. I guess its the user_recipes relation.
When I remove the user_recipes relation, I get the pivot table like I want to.
But I need the user_recipes relation too!
Please. Any help would be appreciated.
I would suggest removing user_id as a primary key from from the recipes table. Currently the combination if id and user_id provides identification for your recipes table. In this situation multiple user_id's can create the same recipe id because the combination has to be unique. user_id can just be a normal column in your table. If you REALLY want to, you can make an alternate key on (id, user_id) but you do not need it because the id is unique.

one to one and one to many relation with same entity mysql

I have two entities Invoice and User.
Invoice have following relations with user
invoice have a owner(one to one relation with user)
one invoice can share with multiple users, it means invoice can have multiple shared users(one to many relation with user)
How to map these relations in mysql database? how many tables? and table structure?
invoice could have two fields, billed_to and payed_by. your billed_to field would be your one to one relationship, and the payed_by field would be your one to many.
For One-to-one relationship, it is advised not to keep separate table. Keep it with the invoice table itself.
For one-to-many relationship, keep another mapping table.
Your table structure should be
Users
----------
id | xxx | .....
-
Invoices
------------
id | user_id | .....
in the above case user_id is the owner.
shared_invoices
-------------------
id | user_id | invoice_id

Null foreign Key or table join

I have a user profile table.
id int (pk)
user_name varchar(50)
email_address varchar(100)
relationship_status tinyint(1)(FK)
I then have a relationship table. Relationship is optional field in user profile table. Is it best practice to do a null join if relationship_status is not selected or a table between relationship and user_profile.
This is simple example but I would ultimately end up with multiple tables in between if a join was optional. This might make for too many joins. However I have read can run into issues with null joins and not best practice.
It depends on the relationship between the tables. If it's a one-to-many or a many-to-many. If each user can have one and only one relationship status then do "null join" if each user can have multiple relationships than do a "table in between"
Example of a one-to-many relationship:
User Table
- id
- name
- relationship_id
Relationship Table
- id
- type
In this instance Relationship Table would have data like "user", "admin", etc and a user can only be a "user" OR an "admin" but not both. (In this particular case sometimes I will have the Relationship table's id column would be a string like "user" or "admin" that way you don't have to do a join on it, you always have it in the user table).
Example of a many-to-many relationship:
User Table
- id
- name
Relationship Table
- id
- type
User Relationship Table
- user_id
- relationship_id
In this case a user can have multiple relationships. The relationship table would have data like "editor", "reviewer", "copywriter", "admin", etc. and each user can be any combination of those (i.e., user 1 can be an editor AND a copywriter).