different many-to-many relationships in a single table - mysql

I'm thinking about merging different many to many relationships in a single table and I would like to know if you think that's smart.
Here are two examples with two separate "relationships" tables:
table: products
table: product_categories
table - relationship table: link_products_product_categories
another example:
table: users
table: user_categories
table - relationships table: link_users_user_categories
Do you think it is smart to create a single table called for example "relationships", which would have fields like this:
first_field_table: users
second_field_table: user_categories
first_field_id: 2
second_Field_id: 5
or
first_field_table: products
second_field_table: product_categories
first_field_id: 3
second_field_id: 5
That would require some changes in my framework, but it would mean a lot less tables. Do you think that's a good idea to store all these relationships in a single table and identify both tables with fields or is it better to keep them separate and have more tables?

"..but it would mean a lot less tables.."
Many tables is not a bad thing per se. Your solution should have as many tables you need as long as you follow the rules of database normalization.
And to answer your question, no It's not good. Infact It's really bad! All it does is cause huge confusion.

Simple answer no it is not smart.
What is the problem with having lots of tables?

Related

Best practice create table or use ID in mysql in one table

I want to store many lists of users from forms. But i'm not sure what is the best practice about it.
I'm not sure if is better to create a mysql table for every list (CREATE table 'nameoflist') or better create 2 tables one for the names of the list and the other for each user with the ID of the first table (name of the list) to identificate from what list is.
Thanks in advance.
Creating separate tables for identical lists is generally useless. The only situation it may be helpful is when you expect millions of rows in every list - then you would have a performance advantage of looking through smaller individual tables.

MySQL many-to-many relationship table usage

I have three tables: Resumes, Orgs, and Resume2Org. Basically, Resume2Org is my many-to-many relationship table linking Resumes.resume_id to Orgs.org_id (so it only has those two keys in that table).
My question is, is it okay to use that many-to-many relationship table to store other data? My use case: the database is part of a system to sift through incoming resumes. But I've been asked to implement a "marked as read" feature so we can easily get the list of resumes we haven't looked at yet. But since a resume can belong to many different orgs, we only want to mark a resume as read for the org the user/viewer belongs to. I thought, hey, having that flag in Resume2Org would be perfect. Is this a smart approach, or should I create a new table specifically for "marked as read"? All the examples I've seen about many-to-many relationship tables is that those tables are used just for that... linking two tables.
Yes it is okey to have additional fields in a many-to-many table. I think it is the right way to do in your case as you don't need to join additional tables and you save spaces.
I was in a very similar situation last week and I added additional field for that.

Best Practice: Separate Tables for Separate Info on Same User

I am designing a database for student information. I wish to implement the best practices regarding separate tables and use of Primary and Foreign Keys.
Let's say I have the following tables (High Level):
Users
Student Information
Student Transcripts
Student Records
There will be different users with different levels. Also, the information in Student Info/Transcripts/Records will all have a Foreign Key with the ID that's in Users.
SO, it would be dumb to just clump all the tables into one big table, wouldn't it? Is it a good idea to keep all this information separate and just use Primary/Foreign keys to link things together, as well as maybe Joins? I just personally think a big table would be quite messy and through this way, it allows one to keep similar data together with its own kind.
Thanks for all input on the matter!

ID Refer to Table Rather than Column within Table

Multiple Voting Table Schema:
Single Voting Table Schema:
Businesses, Products, and Comments can all be voted on. For the first obvious solution, I chose to create an association entity for each of the relationships. We thought maybe there would be a better solution, though.
After extensive research and looking around for a solution, I found the concept of the second schema, which is to have a single voting table with a column (Entity) to define the type of the table or the table in which the ID (EntityID being the ID of the Table it Came From) belongs to. The only problem is that there is no relationship between the voting table and the three entities. Is there a way for the Entity Column to refer to the table rather than the table's ID?
Any suggestions of other constructive ways of developing the schema are welcome.
I think you will find this answer very sufficient.
Why can you not have a foreign key in a polymorphic association?
What you're looking at here is a Polymorphic Association. It has many solutions, of which three are described thoroughly through the given post.
I would suggest you combine the Business, Product and Comment tables into a single table, with an additional colume to denote 'Type'. Then you can enforce your relationship to the Vote table.

One-to-One Relation or Use the Same Table?

I have the following tables created:
Animes(id,title,date), Comics(id,title,date), TVSeries(id,title,season,episode,date)
Some of them have already foreign keys (for one-to-many or many-to-many relations) of directors, genres, articles and so on.
Now i would like to create two more tables Reviews(id,rating,date) and Posts(id,thumbid,articleid,reviewid).
A review is about one Anime and/or Comic TVSerie and vise-versa but properties of a review may be in more than one table. Its the same about a posts.
Is this a typical example of one-to-one relation in separate table or is it more efficient to add more properties to the existing tables?
So more tables and relations or less tables more columns?
Thank you and i hope my question isnt that stupid but im a bit confused.
In my view, It is better to avoid foreign key relationship for one-to-one relationship. It is best suitable for one - many relationships.
I'm not exactly sure what your requirements are, but the choices are as follows:
Have Reviews have 2 columns, either being a foreign key to the applicable table, can be NULL. This is really for when a single review can be about both.
Have a ReviewsComics and ReviewsAnime table. You'd then have all the fields from Reviews in each table (and no Reviews table).
An alternative (2) is to use them in conjunction with a Reviews table, then those 2 tables only has 2 fields which are foreign keys to Reviews and Comics/Anime respectively (thus no direct link between Reviews and Comics/Anime).
Have a base table to which Anime and Comics are linked to 1-to-1 and have reviews link to that table instead.
(Edit) If all the fields are all going to be the same (or similar) for Anime/Comics, you can merge them into a single table and add a type field, indicating Anime/Comics, then the problem goes away. This is similar to the base table option.
EDIT: The 2 reviews tables will probably give the best performance (unless you want to select all reviews for either, often), but with proper indices the performance shouldn't be an issue with any of the above.