databaseforeign-keys

What is a simple example to explain what a foreign key is?


In a database a primary key is a single value that is unique to each row in your table. For example:

id   | name    | whatever
-------------------------
1      Alice     ....
2      Bob       ....
45     Eve       ....
988    ....      ....

What is a simple example to explain what a foreign key is?


Solution

  • A foreign key is a field that points to a primary key of another table.

    Example:

    Table Name - Users
    
    UserID    UserName    UserRoleID
    1         JohnD       1
    2         CourtneyC   1
    3         Benjamin    2
    
    Table Name - UserRoles
    
    UserRoleID    Desc
    1             Admin
    2             Moderator
    

    You can see that Users.UserRoleID is a foreign key which points to the primary key UserRoles.UserRoleID

    The use of foreign keys makes setting up relationships on other tables simple, allowing you to link together the data of multiple tables in a nice way:

    Example:

    SELECT
        a.UserID, 
        a.UserName, 
        b.Desc as [UserRole]
    FROM 
        Users a INNER JOIN 
            UserRoles b ON a.UserRoleID = b.UserRoleID
    

    Output would then be:

    UserID    UserName    User Role
    1         JohnD       Admin
    2         CourneyC    Admin
    3         Benjamin    Moderator