Search code examples
database-designforeign-keysprimary-keyentity-relationshipforeign-key-relationship

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 table B has a reference to the same table in row A, then this is a one-to-many relationship, correct?

Thanks in advance


Solution

  • 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.