What are Relationships, Foreign Keys and Normalization

Microsoft SQL Server 2017 for Everyone Relationships and Foreign Keys
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.18
List Price:  €95.97
You save:  €28.79
£55.86
List Price:  £79.80
You save:  £23.94
CA$97.87
List Price:  CA$139.82
You save:  CA$41.95
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,267.89
List Price:  R1,811.35
You save:  R543.45
Лв131.37
List Price:  Лв187.69
You save:  Лв56.31
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪259.10
List Price:  ₪370.16
You save:  ₪111.06
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$69,057.73
List Price:  CLP$98,658.13
You save:  CLP$29,600.40
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,776.20
List Price:  ISK kr13,966.60
You save:  ISK kr4,190.40
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,259.44
List Price:  ARS$100,374.93
You save:  ARS$30,115.49
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
Already have an account? Log In

Transcript

Hey guys, congratulations for making it this far. Now, we move on to a new part of database and this aspect of database design and development usually people express a bit of angst and anxiety towards understanding and completely applying it. And to be fair, it takes a bit of understanding and practice to actually gather the concepts that are required for you to be proficient. So, this part of databases and has to do with relationships and foreign keys and the bigger elephant that is always in the room when this topic is being discussed is normalization. Now, let me start off with relationships. Relationships are essentially what what the word means they speak about how data is related.

Now, we will have started up the course by Seeing that we're using SQL server which is a relational database management engine or relational database, PostgreSQL my SQL, there are a few others on the market that are also relational. And there are some that are not so relational databases, they thrive on their ability to have relationships. And I keep on seeing that relationships. So let me explain. Relationships are essentially talking about how the data relies on each other. For instance, if you look on my screen, you'll see that I have what we call a database diagram or entity relationship diagram, which is showing entities in the tables in the forms of entities.

So a table once again, is the collection of different roles of different entities so that in database terms, the whole team is called an entity. All right. So this is a database I was working on outside of this course. But I'm just bringing this up. So we have a bigger picture of a completed and fully relation, fully normalized database. And so you see that each entity or table with all of its colors, right?

And then you see that there is something here is connecting it to another table. So you can see leaderboards is connected some way somehow to the users table. Predictions is connected to the users table. And so this visual representation of relationships can help us to, you know, conceptualize what purpose our relationship serves. So in a nutshell, it's literally saying that entities are connected to each other. All right, they share data, they share some dependency, you can't have one without the other artists.

It's very difficult to have one without the other. That's what this connection means. So we can have a user. And if you notice, the key is always going in one direction, right. So it is going from this leaderboards and the key is pointing to users, which means you can't have a leaderboard entity without some representation of a user. You can't have a prediction in the system without some representation of the user.

And the same for pretty much. So this database was being developed to be the storage mechanism for World Cup prediction application. All right, yes, I built one of those in the past. And so people could log in log a prediction. So of course, when they log the prediction, they needed to state which much it is that they were going to be predicting, making the prediction on then so they had to choose the much right? And then we would have to know who is making that prediction.

So of course, I had to keep You record off the user. And so that brings me to the second part of relationships, which is called the foreign keys. And, and this is all very high level. So of course, we'll be getting into more details as we go along. But I'm just giving you a high level representation of relationships on foreign keys. So a foreign key is essentially just the bits of data that you need to help you identify the related entity.

And that's all Spanish. Let me just show you in in practical terms, and then we can revisit that, that explanation. So let's stay on our predictions table. So we have a prediction. We all know that once we create a table and we're creating an entity, we need a primary key. And so that is depicted right here by this is the key emblem, and we would have gone to creating tables and setting primary keys and all those things.

So that shouldn't be foreign. And then we have a bunch of properties that we know we put on. And this doesn't display the data type, which is really immaterial in this setting in this world. So we have an idea for the prediction. And we know if it's an active prediction, or you know if it should be counted among the many. Any comment, that was the joke, Ray, just another property that was there if they spend the entire system for you to understand that, but that is not really important.

And then you'll see something much ID, okay. And if you notice, well, it's not always like this. I don't want you to necessarily take this as a rule of thumb, but sometimes you'll actually see this connection originating directly from the primary key or the foreign key rather. so in this situation, it doesn't so that's not really something to go by. But you'll see that we have much ID and much ID is the reference T shirt on off the much from our other table. So in other words, it would be difficult for us to have a prediction.

And we actually spell out the match. Because we're actually creating the matches here, we actually created a time table for all the matches that are going to occur. I was stored that in a table, it would be kind of foolish and redundant to keep on repeating all of these details, so you know which much the prediction is being created on. All right, so what we did was have a table just for matches. And then what we do is each match an ID, and then each time a prediction is made, we just reference the ID of the match that is being predicted. All right.

So let me say that again, we have a table solely for one entity, it has no dependencies or much can exist without having anything else. Well, in this case, I much needs our group. So you see that Much still needs to be associated with our group. And then the match can exist with other prediction. So the match table has all the matches. And then a prediction can only exist if a match is associated with it.

So that is that kind of association, you always have to map out what bits of data can exist without any other data, and then figure out what else what needs this pre existing data in order to actually survive. So let me use the users table since this one has absolutely no dependencies. So a user is a user user exists, right? I can create a user tomorrow. I don't need anything else. In this case, I don't need gender or anything.

So I just have a user who has either he's active, the data was created, he or she was created, and certain contact information. And that's all I need on a user. However, to make a prediction I have to see which user is making this prediction. So I can't, it would be foolish of me to create a prediction and write about the person's first name, last name, and username every time or just the username, because then I might have a spelling error over here. And the username is something over here, and then trying to match them back. That's going to cause problems.

And when we get into more advanced queries with Inner Joins, and so on, you'll understand what I'm saying. But what we do is we know that the ID is always absolute ID is always unique. So we want to reference this unique data points to make sure that we always know who is making a prediction. So we create the predictions table and we give the prediction all of its properties that it needs. But then we say okay, for who is making the prediction, we just want to reference that person by ID. So that user ID here much as the primary key values So if the user is one here, then the expectation is that when user one makes a prediction, all of this gets filled in.

And one would be the value here when user 10 makes a prediction. Everything else gets filled in. And 10 is the value here. So if we look at who made the prediction, we see user ID 10, I can go back to my users table, and I see 10. And I see Oh, Trevor Williams created that prediction. So that is the relevance of a foreign key, the foreign key cuts down all of the repetition of data across the tables.

And allows you too, have a unique reference points between two tables, and whatever data needs to be shared amongst them. All right. So that's as the example with predictions and users. So in the same way with the matches, the prediction can only exist on a much so instead of saying, oh, where where make a prediction on TV Want to Team tool and then somebody comes along and makes the same prediction on Team one and team two. And then we repeat that again for that entry, then we say this prediction exists on much ID five. If we go to our matches table, we can see match ID five and get all the details here.

So we don't really have to repeat the details across the tables. And that principle is also called normalization, where you're looking at repeating data and reducing it. So that is why this video is called relationships, normalization and foreign keys because they all go hand in hand with each other in reducing redundancy and repetition, again, across tables. So pretty much the only repetition you should have would be your foreign key values. Because if user 10 makes 10 predictions, then you'll see 10 predictions in the database. Having that repeated ID over the details of user can only appear once and that's it, one place that we can go to.

Alright. And that is essentially in a nutshell that is how foreign keys normalization, and relationships work. For the rest of this section we will actually be looking at creating tables and creating relationships and we will build on the database that we have already started and we will start looking into more advanced database concepts. See you soon

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.