views:

119

answers:

5

So I know the convention for naming M-M relationship tables in SQL is to have something like so:

For tables User and Data the relationship table would be called

UserData
User_Data

or something similar (from here)

What happens then if you need to have multiple relationships between User and Data, representing each in its own table? I have a site I'm working on where I have two primary items and multiple independent M-M relationships between them. I know I could just use a single relationship table and have a field which determines the relationship type, but I'm not sure whether this is a good solution. Assuming I don't go that route, what naming convention should I follow to work around my original problem?

To make it more clear, say my site is an auction site (it isn't but the principle is similar). I have registered users and I have items, a user does not have to be registered to post an item but they do need to be to do anything else. I have table User which has info on registered users and Items which has info on posted items. Now a user can bid on an item, but they can also report a item (spam, etc.), both of these are M-M relationships. All that happens when either event occurs is that an email is generated, in my scenario I have no reason to keep track of the actual "report" or "bid" other than to know who bid/reported on what.

+3  A: 

I think you should name tables after their function. Lets say we have Cars and People tables. Car has owners and car has assigned drivers. Driver can have more than one car. One of the tables you could call CarsDrivers, second CarsOwners.

EDIT

In your situation I think you should have two tables: AuctionsBids and AuctionsReports. I believe that report requires additional dictinary (spam, illegal item,...) and bid requires other parameters like price, bid date. So having two tables is justified. You will propably be more often accessing bids than reports. Sending email will be slightly more complicated then when this data is stored in one table, but it is not really a big problem.

LukLed
Yes I understand that, but what if the two subjects are the same?
primehunter326
@primehunter326: They have to differ somehow. Give an example.
LukLed
See my edits, that might make my situation more clear.
primehunter326
Data is a much too general name, even I have to look up in a thesaurus sometimes.
Thomas R. Koll
I agree that using separate tables is probably the best solution. It's just that my site is supposed to be as automated as possible, with a lot of functions achieved just by pressing one button. Having a "Bid" and "Report" table means the user has to fill out a form.
primehunter326
@primehunter326: You can use one table `AuctionEvents` and add `EventType` dictionary field. This will make some select statements more complicated, can affect performance, but will make e-mail sending logic easier. It is valid solution.
LukLed
Yes this was one of the original solutions I had in mind. I ended up doing a combination of a couple of ideas, but I'll probably go back and rework things later on. In asking the original question I thought there might be a standard convention to follow in my scenario, but from the answers it seems that isn't the case. Thanks anyways.
primehunter326
A: 

There's no official or unofficial convention on relations or tables names. You can name them as you want, the way you like.

If you have multiple user_data relationships with the same keys that makes absolutely no sense. If you have different keys, name the relation in a descriptive way like: stores_products_manufacturers or stores_products_paymentMethods

Ben
A: 

Then the relation will have a Role. For instance a stock has 2 companies associated: an issuer and a buyer. The relationship is defined by the role the parent and child play to each other.

You could either put each role in a separate table that you name with the role (IE Stock_Issuer, Stock_Buyer etc, both have a relationship one - many to company - stock)

The stock example is pretty fixed, so two tables would be fine. When there are multiple types of relations possible and you can't foresee them now, normalizing it into a relationtype column would seem the better option.

This also depends on the quality of the developers having to work with your model. The column approach is a bit more abstract... but if they don't get it maybe they'd better stay away from databases altogether..

Both will work fine I guess.

Good luck, GJ

GJ

gjvdkamp
A: 

I think you're only confused because the join tables are currently simple. Once you add more information, I think it will be obvious that you should append a functional suffix. For example:

Table User
    UserID
    EmailAddress

Table Item
    ItemID
    ItemDescription

Table UserItem_SpamReport
    UserID
    ItemID
    ReportDate

Table UserItem_Post
    UserID -- can be (NULL, -1, '', ...)
    ItemID
    PostDate

Table UserItem_Bid
    UserId
    ItemId
    BidDate
    BidAmount
egrunin
+1  A: 

I don't really see this as a true M-M mapping table. Those usually are JUST a mapping. From your example most of these will have additional information as well. For example, a table of bids, which would have a User and an Item, will probably have info on what the bid was, when it was placed, etc. I would call this table... wait for it... Bids.

For reporting items you might want what was offensive about it, when it was placed, etc. Call this table OffenseReports or something.

You can name tables whatever you want. I would just name them something that makes sense. I think the convention of naming them Table1Table2 is just because sometimes the relationships don't make alot of sense to an outside observer.

Ryan Elkins