Relationship between entities in database
When describing relationships between entities have I understood it correct if it the occurrence of a primary key in the table, lets say A has in table B that determines whether it is a one-to-one, one-to-many and so on?
If table B has an attribute with foreign keys, and each row in tab开发者_如何学Gole B has a reference to the same table in row A, then this is a one-to-many relationship, correct?
Thanks in advance
You're mixing up tables and rows in your question, so it'm trying to explain a little more.
If Table B has a column for foreign keys, and each of the rows in this column has a foreign key that refers to the primary key column of a row in Table A, then this is a one-to-one relationship.
However (and this is what you're stating), if several foreign keys in rows of Table B refer to one row in another Table A, then this is a one-to-many relationship, as there are multiple B's referring to one A.
Example
Employees are assigned a type of computer.
Table Employees:
ID Name Computer
==============
1 Mike 1
2 Tom 3
3 Joe 1
4 Emily 4
Table Computer Types:
ID Name
==============
1 HP
2 Macbook Pro
3 Dell
4 Alienware
Then this is a one-to-many relationship telling us that one computer type can be assigned to many employees, i.e. Joe and Mike are using HP computers.
Relationships are between specific "Tables" not between rows.. A table can never have duplicate primary keys, but can have duplicate foreign keys. The relationship between tables is the thing you need it to be.. One Cardriver can have multiple vehicles and a vehicle can have multiple Cardrivers, but can only have one owner. One car can only have one license plate and one license plate can only be linked to one specific car..
For more information.. Just look at the following link.. it's described in detail there..
http://en.wikipedia.org/wiki/Entity-relationship_model
The Relational Database Model and Entity-Relationship Modelling (ER-modelling) are two quite different data modelling paradigms. ER-modelling tends to reduce a data model to a few very simple concepts (e.g. entities and binary relationships between them) whereas the relational database model can in principle be much richer and more powerful than that. This potential mismatch should be kept in mind when attempting to describe relational database concepts using ER-modelling terms, which is what you are trying to do.
In a relational database all relationships are represented in the same way as all other information: as values within tuples within relations (a relation or relation variable is what is more familiarly called a table). However, it's often the case that the most important or obvious relationships within a table are those that also reference other tables ("foreign keys"). Those are the relationships which ER-modelling is usually concerned with.
For simple binary relationships, whether within a single table or between tables, consider the canonical example of a relationship between attributes A and B. If A only is a candidate key in the table(s) in question then the effective relationship between A and B is one to many (or one to zero-or-many). If both A and B are candidate keys then the relationship is one to one (or one to zero-or-one).
精彩评论