Do this required many to many relationship? - mysql

This is my db design:
Portfolio:
- id
- name
- invested
- total
- ...... etc
And my second table
Investments:
- id
- portfolio_id
- amount
--- etc
so according to the logic, a each user can have only one portfolio and each portfolio can have multiple investments:
So basically "portfolio" can have multiple investments and "investments" can also have multiple Portfolio right ? (each user have one Portfolio), thus should i use many to many or one to many ?
As each user have unique profile, investments relates to their profile alone ( it cant be accessed by others ) so should i use one to many here ?

Consider a Associative Entity to bridge between the two tables instead of relying on a many-to-many relationship.
This is useful if you you need to have an investment mapped to multiple portfolios, but to answer your question, if you did it this way, each investment could be linked to more than one portfolio, which does not make any sense. While it is possible for the same stock name to be invested, this would require a separate table for the investment_options, not the same exact investment linked to a different portfolio. Your table should not ever be many-to-many.
Since you said that each user has a unique profile, the correct answer for your problem would be to use a One-To-Many relationship, linking one Portfolio to many Investments.

This is a comment that doesn't fit in the comments section.
For the relationship to be many-to-many you'll need to add an extra table that represents the relationship. For example:
Portfolio:
- id
- name
- invested
- total
Portfolio_Investment:
- portfolio_id
- investment_id
Investments:
- id
- amount
Notice the column portfolio_id is not longer present in Investments.

Related

Putting an entity in hierarchy, or as an attribute with lookup table?

Let's say my company is producing medical products, these products are used in many different lab testing instruments. The business logic hierarchy goes like this:
A lab has multiple locations (Up to thousands)
A location has multiple departments (Chemistry, Hematology, 3-5 per location)
A department has multiple instruments (No more than 10-20 instruments per location)
An instrument has many products.(No more than 1-5 product types per instrument)
The table structure currently mirrors the business logic, like displayed on the left. I suggested we make a small change, displayed on the right.
What are some pros and cons of each approach? I feel like the left-hand side approach might be a bit slower due to chaining so many Joins in a row.
The biggest "con" I see to the approach on the right-hand side is that you lose the association between Department and Location. For the relationships that you described atop your post, the structure on the left is correct from a design perspective.
HOWEVER...
The design that you have means that the Mass Spectrometer at your San Antonio facility will have a different ID than the one at your Denver facility. Is that intended?
------------------ revision after discussion in comments ------------------
You've described a couple of many-to-many relationships - a location will have multiple instruments and multiple locations can have the same instrument (e.g. Mass Spectrometer). To support that, you'll need cross-reference tables. Here's an initial sketch. My standard is to call the table's primary key "ID", and any field called "[table-name]_ID" is a foreign key to the corresponding table:
Lab
ID
Name
Location
ID
Lab_ID
Street_Address
City
etc.
Department
ID
Name
Location_Department -- this lists the departments at a given location
ID
Department_ID
Location_ID
Instrument -- Scale, Oscilloscope, Mass Spectrometer, etc.
ID
Name
Description
Location_Department_Instrument -- inventory at a given location
Location_Department_ID
Instrument_ID
Instrument_Serial_Number
Let me know if this makes sense.

Dynamic self referencing many-to-many relationship

How to construct dynamic self referencing many-to-many relationship ?
I have a problem that i cannot overcome.
I have tables:
Types:
id
name
Products:
id
name
type_id
product_products
parent_id
child_id
Let's assume that we have several products in Product table. Some of them are:
(Name is not relevant)
CB1234 - products.type_id -> cardboard
CBB999 - products.type_id -> cardboardbox
CBP321 - products.type_id -> paper
TSH123 - products.type_id -> tshirt
FAB321 - products.type_id -> fabric
THR321 - products.type_id -> thread
Now i want to tell that product_product relationship that i have cardboardbox that is made of cardboard and paper types of products. In cardboardbox-cardboard and cardboardbox-paper relationship i want to define how much percentage of product is used (consistency) to make cardboardbox product(lets say 80/20, not relevant)
I have different cases for different products. Now i want to define relationship between tshirt-fabric and tshirt-thread but instead of consistency i want to define fabric and thread cost for this product.
Products and product parameters count is not fixed. There could be 500 different products and different relations between them. What are good practices for this problem? There are few ways that i came up with
Lots of handmade many-to-many relationship tables
Lots and lots of columns to product_product table
In both cases there are things i don't like in design, but maybe those are correct way to do that and maybe i have designed my database wrong from the start.
1) would be the way to go - not sure there would really be "lots" once you worked out everything you wanted to store
product_product_consistency
product_product_cost (could this instead be represented as product_cost, then calculated from product_product_consistency or some such?)
What is the final usage, what kind of questions do you want to ask the data?

Database design with multiple Person-like relations

I have a database with relations Witness, PoliceOfficer, Suspect, and many other person-like relations. Often the same person can figure in multiple relations as a police officer could be a suspect in some cases. Would it make sense to make another relation Person to store all person relevant data and just point at the person objects in all my person-like relations?
I fully agree with #Guranjan's comment.
Also consider whether a person can have multiples of the same role, and in what circumstances. You may have a person who is a Suspect in more than one case. So, each case can have many suspects, witnesses and officers, and each of those points to a unique person.
So, the tables would be:
case
case-id
crime-description
date
time
etc
case-person
case-id
person-id
type (witness, suspect, officer)
person
person-id
name
dob
address
etc
Not sure of your use case, but it would make sense to model it using a few tables: Person, Role would be the dimension tables - Person containing the details you need to store on the individuals (name, address, dob etc) Role containing the information relating to all possible roles (Witness, Police Officer, Suspect etc).
You'll need a fact table as well, joining these two for a a particular Scenario so for example:
Person 1 - in scenario 1 - is a Police officer
Person 2 - in scenario 1 - is a Witness
Person 3 - in scenario 2 - is a Suspect
Person 1 - in scenario 2 - is a Witness
Person 2 - in scenario 2 - is a Witness
The Scenario table would contain the Person and Role keys

How to structure table Activities in a database?

I have a site written in cakephp with a mysql database.
Into my site I want to track the activities of every users, for example (like this site) if a user insert a product I want to put this activity into my database.
I have 2 ways:
1) One table called Activities with:
- id
- user_id
- title
- text
- type (the type of activity: comment, post edit)
2) more table differenced by activities
- table activities_comment
- table activities_post
- table activities_badges
The problem is when I go to the page activities of a user I can have different type of activities and I don't know which of this solution is better because a comment has a title and a comment, a post has only a text, a badge has an external id to its table (for example) ecc...
Help me please
I'm not familiar with CakePHP, but from purely database perspective your data model should probably look similar to this:
The symbol denotes category (aka. inheritance, subclass, subtype, generalization hierarchy etc.). Take a look at "Subtype Relationships" in ERwin Methods Guide for more info.
There are generally 3 strategies for implementing the category:
All types in single table. This requires a lot of NULLs and requires CHECKs to make sure separate subtypes are not inappropriately "intermingled".
All concrete types in separate tables (excluding the base, which is ACTIVITY in your case), which means common fields and relationships must be repeated in all child tables.
All types in separate tables (including the base). This implementation requires a little more JOINing, but is flexible and clean. It should be your default, unless there are strong reasons against it.

Preferable database design for job posts

I have two types of job posts -- company job posts and project job posts. The following would be the input fields (on the front-end) for each:
*company*
- company name
- company location
- company description
*project*
- project name
- project location
- project description
- project type
What would be best database design for this -- one table, keeping all fields separate -
`job_post`
- company_name
- company_location
- company_description
- project_name
- project_description
- project_type
- project_location
One table combining the fields -
`job_post`
- name
- location
- description
- project_type
- is_company (i.e., whether a company (True) or project (False)
Or two tables, or something else? Why would that way be preferable over the other ways?
Depending on a lot of factors including the maximum size of this job, I would normalize the data even further than 2 separate tables, perhaps having a company name table, etc... as joining multiple tables results in much faster queries than one long never ending table full of all of your information. What if you want to add more fields to projects but not companies?
In short, I would definitely use multiple tables.
You have identified 3 major objects in your OP; the job, the project, and the company. Each of these objects will have their own attributes, none of which will are associated to the other objects, so I would recommend something akin to (demonstrative only):
job
id
name
company
id
name
project
id
name
link_job_company_project
job_id
company_id
project_id
This type of schema will allow you to add object specific attributes without affecting the other objects, yet combine them all together via the linking table into a 'job post'.
This surely got to do with volume of data stored in the table . In an abstract view one table looks pretty simple . But as Ryan says what if requirements change tomorrow and more columns to be added for one type either company or project. If you are with the prediction that large amount of data top be stored in the table in future even I will prefer 2 tables which will avoid unnecessary filtering of large amount of data which is a performance overhead.