Can 2 relationships have a relationship between them (Database)? - mysql

Consider the scenario between a Project, Task, and a User (ER diagram attached included):
A project can have multiple tasks and a task can be in multiple projects (thus the many-many relationship ProjectTask)
A project can have multiple users and a user can be in multiple projects (thus the many-many relationship ProjectUser)
Check the above example's ER Diagram here
Now a ProjectTask can be assigned to multiple ProjectUsers and a ProjectUser can be assigned multiple ProjectTasks, basically a TasksAssigned relationship
How will I go about storing that information in a database? Can two relationships have a relationship? If yes, is there a better way to showcase this scenario? And if no, how do you show this relationship?

You can have a three-way relationship, in your case:
user
project
task
You probably want to guarantee that the user and task have the same project. That can be handled by declaring composite foreign keys to the projectTasks and projectUsers tables. The structure guarantees that the user and task are in the same project (because it is explicitly mentioned in the row in this table).

Related

MySQL modelling task

I have next task to do in MySQL:
We have Accounts, Farms, Customers, and Users
An account belongs to a Customer
A User has access to one or more accounts
A User has access to one or more farms tied to that account
A farm is tied to one account
Here is ER Model:
Can someone take a look and check whether I made this correct?
For this SQL modeling task, you have three types of entity relationships: one-to-one, one-to-many and many-to-many (How to implement one-to-one, one-to-many and many-to-many relationships while designing tables? for more detail on their implementation).
In your toy model, if "a User has access to one or more accounts," this is modeled as a one-to-many relationship between the user and the accounts, which requires a foreign id on the many side (i.e. every account has a user_id) but user does not have an account_id.
The account-customer relationship appears to be well-modeled, because it is one-to-one (so either table can hold the foreign key), but your one-to-many relations appear to have some errors.

Can a Core Data Relationship Have Attributes

I'm porting a MySQL database to Core Data for a Mac OS app. I have two many to many tables in my database. In addition to containing the foreign keys, there are a few data columns. Is it possible to add attributes to a many to many relationship in Core Data? It doesn't look like it to me. My fallback is to replicate the linkage table in Core Data. Are there any problems doing this?
An example:
A record has one or more artists performing on it.
An artist performs on zero or more records.
The linkage table row contains a foreign key for the record, a foreign key for the artist, the instruments the player performed with, and a notes column that adds additional information such has which track the artist performed on.
You are correct: relationships themselves cannot have attributes. And you are on the right track in modelling the linking table as an intermediate entity. This approach is alluded to in the CoreData Programming Guide section on "Modelling a relationship based on its semantics". In their case, they model a (reflexive) many-many relationship from Person to Person using an intermediate FriendsInfo entity with a ranking attribute.
In your example, you might have a Record entity, an Artist entity, and an intermediate Appearance entity. The Appearance entity would have attributes for Instruments and Notes, and (to-one) relationships to Record and Artist (each with a to-many inverse).
The slight downside is that you have to create the Appearance object in order to link a Record object and an Artist object, rather than just adding them to the relevant relationship. You will also have to watch for uniqueness of the combination of Record/Artist, if that's important to you: by default there could be many Appearances for the same Record and Artist.

Laravel ORM: unique combination of fields from pivot table

This question extends the example Eloquent : Working With Pivot Tables provided in the Laravel Documentation.
The relationship here is that a User has many Role objects that it can relate to, and each Role is made up of a number of Task's.
The relationships between these models are:
User and Role:
Each Role may only relate to one User, however a User may have many Role's
A one-to-many relationship
Role and Task
Each Role can have many Task's associated with it, and each Task can belong to several Roles
A many-to-many relationship
In terms of a database schema (MySql), we therefore have:
A users table that has no foreign keys
A roles table with a user_id foreign key
A tasks table
A role-task pivot table
In plain English, the constraint I need to apply is that a user can not be associated with multiple roles that are made up of exactly the same tasks, however two role's can be made up of exactly the same task's
Is there an in-built way to apply this constraint using Laravels Eloquent ORM system? If not, what would be a tidy workaround?
edit:
Eloquent-like description:
User hasMany Role
Role belongsToMany Task
Question: How to prevent assigning to a user multiple roles having exactly the same related collection of tasks

When or use a many-to-many relation in a database?

I want to know in what situations we create many to many relation. What is the need of doing such?
A quick search goes a long way. Though the following is for MS Access, the concept is the same for any relational database.
Via: office.microsoft.com - Create a many-to-many relationship:
You have a many-to-many relationship when a single record in one table
can relate to many records in another, and a single record in that
second table can also relate to many records in the first. For
example, say your company has several types of computers and several
technicians, with each technician certified to work on some, but not
all, of the computers. Each technician can be related to more than one
computer, and in turn, each computer can be related to more than one
technician.
To track who can work on a given machine, you create a many-to-many
relationship by adding the primary keys from both sides of the
relationship to a third table, called a junction or link table. In
other words, a many-to-many relationship is really just a pair of
one-to-many relationships.

Relational DB Concept

I am trying to lay out the concept for a Relational DB and I ran into some conceptual Problems:
If I have multiple discrete Entities that are "nested" in each other/have a hierarchy e.g.:
Bosses can have multiple Employees. These employees have different Projects they do and One Project has again multiple sections.
So
B1-Bn:
E1-En
P1-Pn
Section1 -SectionN
How would that be best mapped in a database?
Or in other words, how is this hierarchy best mapped in a relational db?
Now I have Costumers that interact with these Employees.
They are met by the bosses
Then they decide which employee will work for them.
Then they are assigned Projects, with one or more sections.
How would that be best mapped.
the Relations 1-n, m-n, 1-1: Can they be used for e.g.:
This is a Foreignkey because of the 1-n relationship.
This is a ManytoManyField because of the m-n relationship.
And is there a excellent online tool to better understand/visualize that.
Thanks so much for your time!
You may want to follow a course on relational database design; this subject takes more than a few days to explain or master. But you are on the right track.
The first thing you may be seeing is a hierarchy, but before you know it there will be relationships that aren't hierarchical, so a network is formed.
This is why relational databases do not work with hierarchies.
You identify different types of entities and have one table for each type.
For each entity type you identify the properties of such entities - each property will be a column of the table.
If a property does not have an atomic value, but a structured value, these structured values must be regarded as an entity, and must be given its own table, and the property will be a foreign key referring that table.
In this way, you will form a network of tables linked by foreign keys. This is called an entity-relationship diagram. Many designers advocate creating such a diagram first, without mapping the entity types to tables directly. They allow many-to-many relationships between entity types in the diagram. A foreign key between tables on the other hand is always many-to-1 or 1-to-1. So these designers have an "implementation" step in which they introduce an additional table for each many-to-many relationship. Personally I don't use many-to-many relationships in my diagrams to begin with.