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.35
List Price:  €96.21
You save:  €28.86
£55.83
List Price:  £79.77
You save:  £23.93
CA$100.76
List Price:  CA$143.95
You save:  CA$43.19
A$112.23
List Price:  A$160.33
You save:  A$48.10
S$95.14
List Price:  S$135.93
You save:  S$40.78
HK$543.62
List Price:  HK$776.63
You save:  HK$233.01
CHF 63.04
List Price:  CHF 90.06
You save:  CHF 27.02
NOK kr797.48
List Price:  NOK kr1,139.30
You save:  NOK kr341.82
DKK kr502.49
List Price:  DKK kr717.87
You save:  DKK kr215.38
NZ$124.10
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,361.55
List Price:  ৳11,945.58
You save:  ৳3,584.03
₹5,962.79
List Price:  ₹8,518.63
You save:  ₹2,555.84
RM314.04
List Price:  RM448.65
You save:  RM134.61
₦108,335.42
List Price:  ₦154,771.52
You save:  ₦46,436.10
₨19,495.71
List Price:  ₨27,852.21
You save:  ₨8,356.49
฿2,390.81
List Price:  ฿3,415.60
You save:  ฿1,024.78
₺2,467.44
List Price:  ₺3,525.07
You save:  ₺1,057.62
B$434.14
List Price:  B$620.23
You save:  B$186.09
R1,296.68
List Price:  R1,852.48
You save:  R555.80
Лв131.68
List Price:  Лв188.12
You save:  Лв56.44
₩102,198.93
List Price:  ₩146,004.73
You save:  ₩43,805.79
₪256.56
List Price:  ₪366.53
You save:  ₪109.97
₱4,090.81
List Price:  ₱5,844.26
You save:  ₱1,753.45
¥10,994.99
List Price:  ¥15,707.80
You save:  ¥4,712.81
MX$1,408.98
List Price:  MX$2,012.91
You save:  MX$603.93
QR255.09
List Price:  QR364.43
You save:  QR109.34
P967.82
List Price:  P1,382.66
You save:  P414.84
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,562.49
List Price:  E£5,089.49
You save:  E£1,527
ብር8,907.19
List Price:  ብር12,725.10
You save:  ብር3,817.91
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,323.69
List Price:  CLP$99,038.09
You save:  CLP$29,714.40
CN¥510.80
List Price:  CN¥729.75
You save:  CN¥218.94
RD$4,245.36
List Price:  RD$6,065.06
You save:  RD$1,819.70
DA9,465.07
List Price:  DA13,522.10
You save:  DA4,057.03
FJ$162.39
List Price:  FJ$232
You save:  FJ$69.60
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,638.77
List Price:  GY$20,913.42
You save:  GY$6,274.65
ISK kr9,772.70
List Price:  ISK kr13,961.60
You save:  ISK kr4,188.90
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,291.33
List Price:  L1,844.84
You save:  L553.50
ден4,143.97
List Price:  ден5,920.21
You save:  ден1,776.24
MOP$559.97
List Price:  MOP$800
You save:  MOP$240.02
N$1,289.45
List Price:  N$1,842.15
You save:  N$552.70
C$2,574.80
List Price:  C$3,678.44
You save:  C$1,103.64
रु9,534.71
List Price:  रु13,621.60
You save:  रु4,086.89
S/260.99
List Price:  S/372.86
You save:  S/111.86
K283.85
List Price:  K405.52
You save:  K121.66
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,936.45
List Price:  ZK2,766.47
You save:  ZK830.02
L335.12
List Price:  L478.77
You save:  L143.64
Kč1,694.03
List Price:  Kč2,420.15
You save:  Kč726.12
Ft27,771.44
List Price:  Ft39,675.18
You save:  Ft11,903.74
SEK kr774.38
List Price:  SEK kr1,106.30
You save:  SEK kr331.92
ARS$71,792.58
List Price:  ARS$102,565.23
You save:  ARS$30,772.64
Bs484.55
List Price:  Bs692.24
You save:  Bs207.69
COP$307,165.41
List Price:  COP$438,826.54
You save:  COP$131,661.12
₡35,530.54
List Price:  ₡50,760.09
You save:  ₡15,229.55
L1,777.33
List Price:  L2,539.15
You save:  L761.82
₲546,250
List Price:  ₲780,390.60
You save:  ₲234,140.59
$U3,115.91
List Price:  $U4,451.49
You save:  $U1,335.58
zł287.82
List Price:  zł411.19
You save:  zł123.37
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.