views:

48

answers:

5

I have a many to many relation for: Client and Broker tables (just an example). Obviously, each client can have multiple brokers and each broker can have multiple clients. What is considered the proper naming convention for the intersect table.... is it ClientBroker...?

+1  A: 

I usually use the names of both of the joining tables.

So, in your case, ClientBroker.

Michael Todd
+1  A: 

I've often seen the format "Client_Broker"

froadie
+1: Punctuation should be used sparingly, and in this case it helps distinguish the two sides.
S.Lott
A: 

I would prefer "Clients_Brokers" (pluralizing both names to indicate many-to-many).

FrustratedWithFormsDesigner
I voted this up since someone else voted it down. I think this convention is fine and often necessary. Some ORM libraries provided by MVC frameworks have a naming convention for tables to handle relationships automatically. I know of at least 3 PHP frameworks that use plural name and underscore separation for many-to-many pivot table names like this.
wmid
This is the old singular vs. plural table naming conflict (extended here to join tables). Do you name a table in the singular - e.g. "Customer" because each record represents one Customer? Or plural because the table itself contains many "Customers"? It's a matter of opinion, I don't think there's a reason to downvote this.
froadie
A: 

I prefer to distinguish between intersect tables and actual transactional tables. So, I end them with Map. So it would be Client_Broker_Map, or ClientBrokerMap.

Sumit
A: 

Some programmers don't like pluralizing table names for a few reasons:

  • it breaks the "is a" rule, meaning if you have a table called 'User', then each record in the table "is a" User object. This follows object-orientation rules.
  • a Model class is usually named for the table where its data comes from. So if you have a User model, the record represented by the model is in the User table

This makes a lot of sense if you're in control of the entire db and business layers of the project. However, a lot of frameworks now have ORM libraries that facilitate working with tables and relationships. Those ORM libraries often have a naming syntax that should be followed to let the ORM library do most of the heavy lifting.

For example, I use the Kohana MVC framework for PHP which offers an ORM library. The ORM suggests pluralizing table names, using all lowercase names, and using underscores for many-to-many table names. So for your example, you'd have the following tables: clients, brokers, and brokers_clients (the ORM suggests alphabetically arranging table names for many-to-many tables). When creating models for these tables (extending the ORM model), you use the singular value of the table name, so the model for clients would be Client. The ORM handles the pluralization conversion. Kohana's ORM also uses an inflection library, so unusual pluralization values are handled correctly. For example, a table named 'categories' can use the model name 'Category'. Finally, if you have a db structure already implemented but want to use the ORM library, you can override the default ORM table naming syntax and give it the table name you want to use.

wmid