Relational Database Model

Relational Database Model

Welcome Back!

 Relational databases are the de facto standard for database applications. The model for a relational database states that information must be stored in tables, and in each table, information is viewed in a two-dimensional matrix consisting of rows and columns, which are often referred to as records and fields. Tables in SQL are typically modeled after real-world entities, such as vendors, donations, or invoices. A column always represents some attribute of an entity, such as vendor name, donation amount, or invoice total. A row contains a set of values for a single instance of the entity, such as one vendor name, one donation amount, and one invoice total.

If a table has a column that uniquely identifies each row in the table, this is the primary key. There is a rule in SQL that states that each table in the database should have a primary key defined. Without the primary key, the core functionality and power of SQL depreciates. Always remember that SQL is about relationships. Primary keys is the first step toward relationship building, but there is another key that builds an even better bridge or link between tables. It’s called a foreign key. But foreign keys, that is a new concept, what are those? Foreign keys quite simply are one or more columns in a table that refer to a primary key in another table. Tables, therefore, are related to each other through the primary and foreign key columns. Let us look at two tables: Donations and Colleges.

Donor Donations Table

DonorIDDonorNameDonorAmountDonorCity
100Jane Lee$250Cincinnati
101Mike Lee$500Dayton
102Dee Lee$650Kettering
103Regina Lee$1,000Centerville
The DonorID column is the primary key on the Donor Donations table.

College Table

CollegeIDCollegeNameDepartment
100UCEnglish
101Miami UArt
102NKUComp Sci
103XavierBiology
The CollegeID column is the primary key on the College table.

If I asked you to tell me the relationship between the two tables, what would you say? You might say:

  • Idk
  • Who cares?
  • Can we talk about something else?

Okay seriously, what would you say? You might say:

  • One donor can donate to any college
  • One college can receive a donation from a donor
  • Many donors can donate to many colleges
  • Many colleges can receive a donation from many donors

While we can visually see and understand the relationship between the two tables, there needs to be a relationship created that SQL understands. The most common type of relationship is a one-to-one or a many-to-many relationship with another table. Remember how I mentioned earlier that tables are related on their key columns? Well, looking at our tables we do in fact have primary keys identified in each table, the DonorID and CollegeID columns. Hoo-ray so that holy and sacred rule of having primary keys identified has been obeyed. However, we are missing foreign keys. These keys identify a related row in another table. To build the many-to-many relationship, we would simply add the DonorID column to the college table. Voila, we now have a valid foreign key!

Donor Donations Table

DonorIDDonorNameDonorAmountDonorCity
100Jane Lee$250Cincinnati
101Mike Lee$500Dayton
102Dee Lee$650Kettering
103Regina Lee$1,000Centerville
The DonorID column is the primary key on the Donor Donations table.

College Table

CollegeIDCollegeNameDepartmentDonorID
200UCEnglish100
201Miami UArt101
202NKUComp Sci102
203XavierBiology103
The CollegeID column is the primary key on the College table. By adding the DonorID column, it creates a reference (link) to the Donor Donations table, which is a foreign key.

In this lesson, you’ve learned a little about the relational database model. You have learned that

  • Information must be stored in tables
  • Information is viewed in columns and rows, which are also referred to as records and fields
  • Each table should have a primary key identified, which is a unique identifier
  • Foreign keys provide a link between two tables/one or more columns in a table that refer to a primary key in another table
  • Most common relationships between tables: one-to-one and many-to-many

Join me in the next blog to understand how columns in a table are defined.

Follow Me