"has_one" vs "belongs_to" vs "has_many" in RDBMS terms
Can someone please clarify what these mean in RDBMS terms. I always get these confused,开发者_如何学JAVA and I can't find a single decent tutorial on this. Try to reduce the definitions to,
- Linking table
- Primary key
- Foreign Key
Or, tell me why these definitions can't be reduced to something simple like that. Do not define them with ORM terms.
EDIT: An ORM is usually a pretty thin veneer over a database. typically, classes are mapped to tables, with instance of those classes as rows in the table. A table named 'USERS'
with a varchar field named USERNAME
might be mapped to a class named User
with a string property named username
.
Things get more interesting when two tables in the mapped database are related by a foreign key constraint. The 'USERS'
table might have a primary key called 'ID'
, and a second table, say, 'ADDRESSES'
might contain a field called 'USER_ID'
which is constrained to 'USERS'.'ID'
. In such a case, Users is independent, and Addresses is dependent. A row in the users table may take any value, but rows in the addresses table must have a corresponding row in the users table, else the foreign key constraint is not satisfied
When this is mapped to application classes, primary keys are often occluded. It's not the ID's that the application is interested in anyways. What we really want in the above example is for the mapped Address
class to have a property of type User
, that is the corresponding mapped user for the address. Similarly, we might want the User
class to have a property that is the collection of Addresses
that are dependent upon it.
The independent table is called, in the ORM, the parent class, and the dependent table mapped to a child class. The terms we use when describing these classes is that a child class belongs to the parent class, which is equivalent to say that the dependent table has a foreign key constraint on one of its fields with the independent table as the referent. The Parent class, comparably, has the child class, which is equivalent to saying that the indpendent table is a referent in a foreign key constraint on a field in the dependent table
Supposing class A belongs to class B, Such a relationship may be of a one to one correspondence, where each A can belong to exactly one B, and each B can have exactly one A. This is the least common case, however. Most cases are One to many, where each A belongs to exactly one B, but each B may have zero, one, or more than one A. Such a relationship is called One to many. For instance, a User can have more than one Email address.
The final twist is that sometimes the two classes may be in a many to many relationship. This is almost always achieved by means of a third table. An example of such a relationship is Stack overflow questions, which may have many Tags, and each tag applies to many questions. Some ORM's may have a notion that one can belong to the other without the relationship being reflexive, but most ORM's call this situation "Has and belongs to many"
I use :has_one
only for one-to-one relationships, defined on the "foreign" table. Here's a straightforward guide: http://blog.hasmanythrough.com/2007/1/15/basic-rails-association-cardinality
精彩评论