Click here to Skip to main content
15,884,099 members
Articles / Database Development / SQL Server

Get Ready to Learn SQL: 7. Simplified Data Modeling

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
23 Oct 2014MIT3 min read 8.5K   9  
Simplified Data Modeling in SQL

Introduction

When working with SQL databases, it is often useful to create diagrams of the database tables and their relationships. These may be done during the design process, as you’re creating your model, or once the database is created, in order to document the tables’ dependencies. As I explain various concepts in my lessons, I’ll sometimes use models to illustrate my points.

There are many types of modeling software you can use to create models, such as MySql Workbench, which not only create smart looking diagrams, but also generate the code to create the database! In my case, since I’m trying to keep the diagrams simple, and I don’t have a need to generate code, I’m going to create my own diagrams. They are loosely based on the IDEF1X notation.

Modeling a Table

Image 1

Relational Database Table Model

The diagram above shows my method to model a relational database table. The major elements that are depicted include:

  • The Table Name, which is located at the top of the table.
  • The Primary Keys. Remember the primary keys uniquely identify each row in a table. A table typically has one primary key, but can have more. When the key has more than one column, it is called a compound key.
  • Table Columns – There can be one or more table columns. To keep the diagrams simple, I don’t show the data types. I may introduce those later when we focus on more comprehensive modeling.
  • Foreign Key – This is a column or set of columns which match a primary key in another table.

Speaking of foreign keys, the match between them and the primary key is what “glues” the database together. The significance of these relationships becomes apparent, front and center, once we start to work on joining tables together.

Modeling Table Relationships

Image 2

We connect lines between tables to show relationships. In some cases, an entry in one table can be related to more than one entry in another. This is called a one-to-many relationship. In our example, there are many employees in one department; therefore, we show a many-to-one relationship.

A many-to-one relationship is similar to a one-to-many relationship, this difference is in the point-of-view you take when naming the relationship. I think most people speak of one-to-many relationship more often.

Sometimes, there may not be an entry in a table, so technically speaking, you could have zero or one to many, but that gets hard to say, so when speaking in general terms, most people say “one-to-many.” However, when you want to get precise, you can use notation to specify the cardinality of a relationship.

Cardinality Notation
zero or one-to-many 0..*
one-to-many 1..*
zero or one-to-one 0..1
one-to-one 1..1

When we cover normalization, we’ll use these diagrams to show how we organize the database. I think it is important you become comfortable with these diagrams and concepts before we begin that journey. As such, make sure you understand the following:

  • Table
  • Field
  • Primary Key
  • Compound Key
  • Foreign Key
  • One to Many Relationship
  • One to One Relationship

Exercises

  • What is the difference between one-to-one relationship and a many-to-one relationship?
  • What is a Foreign Key? How do you define one?
  • What is the difference between a primary key and a compound primary key?

Answers

I’d be more than happy to share the answers with you. Sign up for my weekly newsletter at http://www.essentialsql.com so you’re sure to receive each week’s lesson.

Congratulations! You just learned how to use data modeling to better understand which tables and relationship exist within a database. More tutorials are to follow! Remember! I want to remind you all that if you have other questions you want answered, then post a comment or tweet me. I’m here to help you. What other topics would you like to know more about?

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
-- There are no messages in this forum --