Does this ERD make sense? - mysql

I'm trying to replicate an email service provider where emails can be stored and also sent out based on subscribers. I'm focusing on the data structure atm but I can't help but to think I may have done something wrong.
As you can see in the diagram, towards the Subscriber, List and Subscriber List relationship. I'm trying to achieve the scenario of..
A subscriber can be part of many lists, but a list can hold many subscribers.
Does this diagram make sense on this matter?
Thanks

Looks good to me. Because you have a many to many relationship between subscribers and lists, you need a mapping table (subscriberlist) to hold the relationships.
If you're asking about the ERD as a whole, it's hard to say if it's what you need. In regards to subscribers having many lists and lists having many subscribers, the way you are doing that is the way to go.
EDIT: I'm not sure why you have a column called Lists in the subscriber table. The SubscriberList table should hold all of the mappings between Subscribers and Lists and trying to replicate this data anywhere else is going to create an issue of denormalization.
EDIT2: The classic example of this that I always think of in these many to many relationships is Users and Roles. In just about any complex web application you have lots of Users and multiple Roles. The only way to allow for a User to have many Roles (and each Role to have many Users) is to create a mapping table as you have shown in your ERD.
A nice short and to the point explanation can be found about 1/3 of the way down this article under the heading "Many-to-Many Relationships": http://www.deeptraining.com/litwin/dbdesign/FundamentalsOfRelationalDatabaseDesign.aspx

Related

Symfony 6 - Many to Many Relationship - Storage of Data in Linking Table?

I am currently working on a symfony 6 project with a mysql database and have the following issue:
My situation:
I have two tables/entities:
Orders
Article
This two entities are related in a many to many relationship. So one order can have many articles and one article can belong to many orders.
This relationship leads to the creation of a third table that links the other two tables in terms of the many to many relationship.
The Problem:
I am now wondering where to store the information about which Article gets ordered how often.
My Idea:
I thought about storing this information in the linking table. However normally Symfony does not create an Entity for this. Therefore I think that there might be a better solution.
Does anybody have Idea where to store this information? :)
Thanks to #Cerad I found out, that when there is the need for storing such extra information, a Many To Many Relationship is not the right answer.
In a case like this, it makes sense to create a further Entity like OrderArticle. This Entity is in a many to one relation with both Orders and Articles.
As also stated in the Symfonycasts Tutorial here: https://symfonycasts.com/screencast/symfony4-doctrine-relations/many-to-many-joins
Maybe this helps someone in the future as well. :)

MS access multiple relationships between two tables

We had an MS Access guru at our company who left for another position. Before she left she gave me a quick introduction on how to create queries from a sql server. I am really struggling with this and as I have no one to turn to at our company I was hoping you guys could help.
Hope you can help!
Thanks!
Well, keep in mind that when you build a query, it DOES NOT necessary mean that a enforced relationship exists here. (it might).
Further more, if you imported the tables, then again its doubtful that relations are defined in Access unless you use the relationships window to "enforce" such relationships.
However, when building a query? We will often join on two fields. When you build a query in the query builder, you are free to "make up" any kind of join you want.
Say I was given two different spreadsheets. One had some people, and another had a list of hotels.
Ok, so say we want to generate a list of all people in the same city as the hotels.
You might join between table "People" and say Hotels with city.
however, WHAT happens if there is more then one state with the same City name?
Well, then just join on City AND State!!!
So you get this:
So I not have some related tables here. I just feel like and want to, and need to join the two tables of data.
As such, we never cared or setup or "had" some relationship defined, but all we care about is creating and building a working query.
So, don't confuse the simple act of building some query with that of having setup a corrrect relatonships between tables.
For a working application? Yes, you most certainly will setup relatonships.
So, if you setup relatonships correctly, then you not be able to say add a customer "invoice" reocrd without FIRST having a customer record. You don't have to do this, but it is a very good idea for a working applicaton.
However, when dealing with imported data? You often may not have an pre-defined relationships.
Now, of course in "most" cases, a query that involves multiple tables will in near all cases "follow" what you defined as relationships in the relationships window but it not necessary a requirement at all.
As noted, when building a working application? Then yes, of course you want to setup the relatonships BEFORE you start adding data.
But for general data processing, and creating queries against say different tables of data you are slicing and dicing and working with?
You are free to cook up and draw lines between the tables in the query builder, and as such, often such quires will have zero to do with the relationships you defined, or in fact even when you don't have any relationships defined at all.
That above People and the list of hotels is a great example. I mean, it rather cool that I simple joined on both City and State, and did not have to write one line of data processing code for my desired results
(a list of people in cities that live in the same city as my hotel list).
So don't confuse what we call "referential integrity" and defined relationships. We define these relationships so it becomes impossible for you the developer to add a customer invoice without first having added the customer. And it also means that you, your code, or even a editing the tables directly will not allow this to occur.
However, when dealing with just reporting, or importing data to work on? Well, then often we will not have any relationships defined, but that sure does not stop us from firing up the query builder and drawing join lines between tables.
Between two given Tables you can have one relationship involving two (of more) fields or two (or more) relationships each involving one field. Both cases are possible and have different implications.
The first case, as the first commenter pointed out, is typically used when you have a compound key in the master Table of the relationship.
The second case is typically used when you have two candidate keys in the master table, each of which is used as a master field in each of the two independent relationships.
In Ms-access the case of two independent relationships may be identified because it implies two table-boxes for the same table in the relationships pane.

One or two tables: that is the quest*on

I'm trying to setup a database schema for a company which works as a middle man (selling items collected from vendors to buyers).
Both of these entities (vendors and buyers) can be generalized as a client - they both have very similar attributes (name, email, password, address, etc...) and multiple other entities depend on this. For example invoices are generated for buyers and settlements (different type of paperwork) are generated for vendors. The thing is that one person (a client) can by buyer and vendor in the same time.
The dilema I'm having is how to setup the database structure for this?
At the moment I'm more in favor of having both vendors and buyers in one table and distinguish between them using something like roles column. Thanks to this approach I would avoid the data redundancy and I could still create views to easily separate vendors from buyers to the outside world.
Am I thinking about this correctly? How would you typically solve this situation? Would it be better to use two separate tables?
Thank you for your advice and experiences :)
If you know the usecases, think about, what could be a rough solution. But that is quite dangerous, at the end sometimes the ingenious datamodel becomes too complicated to understand and maintain.
How important is it to decide now, will your datamodel or organization be fit for a later change?. Can you be agile? Then implement, what is best for your current usecases, nothing more!
btw.
if there is a 1 to 2 relationsship between person and role, you should factor out the role, not duplicate the data, or create two attributes, isBuyer and isVendor, or put in these attributes references to the buyer- and vendor-specific data, if there is any.

Improving my Database Design for future scalability

Well, I am working on a project which might involve thousands of users & I don't have much experience in databases especially when it involves relationships between entities.
Let me explain my scenario. First there's an User who can login into our system using his credentials. We have a module in our system, which will enable him to create Projects. So that brings a relationship between User table & Projects table.
Now there's another module, namely Team Creation Module, it does what it says. Out of the list of available members, he can pick who he likes and add them to a team. So there are tables for that Members & Team. Furthermore, a member can be a part of many teams and a team can have many members & a "User" can be member as well.
I have a designed the database myself but I am not sure if it is good or bad one. Moreover, I would really appreciate if someone can point me to good tutorials which shows how to insert or update into tables involving relationships.
Here's my design till now:
Update
After a discussion with someone on IRC, I came up with a revised design. I merged "User" & "Members" table as User is also a Member.
My question still remains the same, Am I on right track?
It's great that you're thinking long-term, but your solution won't work long-term.
This is not the first time this sort of thing has been tried before. Rely on the wisdom of those that have messed up before. Read data modeling pattern books.
Abstract and Normalize. That's how you get to a good long-term solution.
At least read up on The Party Model. A group and individual are actually the same (abstract) thing.
Put actually different things in different tables. An Address and Member don't belong in the same table.
"Am I on the right track" is not a useful question - we have no way of telling, because it depends on where you are headed.
A couple of things:
it's a good idea to name the relation columns after the relationship. For instance, in the first diagram, the "owner" of the project should not be called users_user_id - that's meaningless. Call it "owner_id" or something that meaningfully describes the relationship between the project and members table.
in the second diagram, you appear to have a "many to many" relationship between members and projects in the members table - but there's no efficient way of storing the id of more than one project in the members table. You need to factor that out into a joining table - projects_members, for instance, just like you did with teams_members.
the "teams_members" table has a primary key called tm_id. A purist would tell you this is wrong - the unique identifier for that table should be the combination of member_id and team_id. You don't need another unique identifier - and in fact it's harmful, because you must guarantee uniqueness of the member_id and team_id combination.
As Neil says, you probably want to start reading up on this. I can recommend 'Database Systems: Design, Implementation, and Management' by Coronel et al.

Data contexts - Linq to SQL

No one has taught me how to use these, and I can't find much on how I should be using them properly.
At the moment I say have a data context for my users, user details, user profiles etc. I then have a separate one for my help centre, holding tickets, ticket replies and ticket attachments etc.
Is this correct? Should I be splitting them like this? I cant create an association between two data contexts apparently in the designer. Should all tables be in just one data context? Any benefit to splitting them up? The tables in them are not fully independent of each other.
Have a look here at another Stackoverflow question: Linq-to-SQL: how many datacontexts?
Summary: just use one datacontext for you entire DB