Create a Relationship in SQL Server

Microsoft SQL Server 2017 for Everyone Relationships and Foreign Keys
19 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, welcome back. In this video, we will start fleshing out or putting in more tables into our school management database. Now, coming off the last topic of relationships, foreign keys and normalization, people generally say that Oh, they don't know where to start or how to start. They know that stuff needs to be related, but then sometimes they get it in the wrong direction. Now, when I'm designing a database, what I like to do is start with the tables that I know are atomic. When I say atomic, I mean that they have no dependencies, and they can exist entirely on their own.

So we already started that process here with our school management database, where we said courses and students now a student can exist entirely on their own the student is the student right? The student can be there, technically without Of course because if they pay the school fee for the general fees, they're allowed To go on the campus, but they may not be taking a course. So the student can exist with no association to a course, just the same way I can create a school and I am offering courses and I don't have any students just yet. So of course can exist on its own and and that is a that is something that you can actually employ when trying to decide what is atomic or not think about it in a real life scenario. If in real life, this element or this entity can exist without dependency on another one, right.

So that is the first step. So when designing a database, I always start with atomic elements, those that can exist by themselves. So we're actually just going to create about two more tables for this section. To begin this concept, I'm going to go ahead and use the editor and I'm going to create another table and this one will be for teachers. So We all know that we always start off with an ID primary key, you can see ID or just teacher ID, whichever one, it depending on your naming convention. And we'll give it the type of int.

And of course, we go down here are professionals that this by law, so you know that we just go and make it's the identity. And we also flag it as primary key, which we can just right click and say set primary key. So I'm just going to go ahead and fill out the details of this. Give me a few. Alright, so I filled out some columns here, I gave the teachers record a first name, a last name, and the date joined and I use the wrong data type here. So I just say a date time.

And of course, you could just say dates. So he's gonna say date, and then what we would be tempted to do and I could completely understand if you'd be tempted to put a course ID or associate this lecture. feature with a course right here. And of course, we're talking about foreign keys and primary keys. So you're thinking that, okay, so clearly a teacher has to have a course teaching, which is, which is fear. But then I teach, I can also teach many courses.

So that is something you need to take into consideration because then if we put an ID here, for the course ID, then we're, we're only associating this teacher record with one course at a time. And so if this teacher is teaching another course hold, we accommodate that we would have to repeat all of these details and set another course ID, which is not the optimal way to store our data in a relational database setting. All right, so that is one of those caveats that people would run into where they would try to put the foreign key at the wrong place because really, and truly the teacher or like I said in the beginning can exist in my school. Not be teaching because maybe as a free semester, maybe he's a specialist, I don't know. But I can have a teacher, I can hire a teacher. And because I have no students, I'm offering courses but I have no students, then this teacher is not actively teaching a course.

So I'm going to leave the teacher table like this. And save. Okay, so now we have three tables, I can just go and refresh. And I have three tables. I have my courses, I have my students and my teachers. Once again, they're all atomic.

So you probably say, Okay, so how do I relate the teacher to a course? No, let us explore once again in real life. I have a school right now. I have a course when I created this database course. The course was there. Yes, I was a teacher.

And what I had no students, what you did was enroll into this course and then the platform that you're learning This course on then said that me trouble Williams, the teacher, yeah, it will be associated with this course because I have many courses on this platform. And you the students will be associated with this course with this teacher. And I'm sure you're doing more multiple courses on this platform. And so that kind of relational relationship needs to exist between the course the student and the teacher. So setting a foreign key in any one of these tables would not fully accommodate that kind of three part relationship because at some point, something will have to repeat in these three tables with details being spread all over the place. What we want to reduce is a spreading of details in multiple places.

So when we see this situation we're trying to associate two tables with each other will lead to some duplication of details in the long run, what we do is have another table that is going to facilitate these relationships. All right? So what I'm going to do is create a new table and call it enrollment. So I'm just going to say ID, you get the primary key, and we make it identity. Alright, so it's auto incrementing. And then I'm going to have and once again, let me just save this table for now.

So we know we're working with this is enrollments. So an enrollment can only exist if we have a teacher with a course with at least one students. And then we have an enrollment because the student is enrolled in a class being taught by this teacher. Right? So then we will create teacher ID. So this is the person teaching and it's teacher ID.

So it has to be an integer. All right. I'll leave Based on our designs, so far, we have student ID, because the teacher is teaching the students and I'll just keep my casing consistent. And then we have the course that these that this teacher and the students are both going to be involved in. Alright. And that is the beginning.

And I will save this quickly so we can get it updated. That is the beginning of the creation of a foreign key. Now there's another step that needs to occur. So now we need to actually have the relationship. So yes, we created the foreign key properties, but they're not foreign keys yet because we need to actually establish the relationship between this and the teacher table. And then that is what will give us what we call database integrity when you know that the relationship and the constants are there.

Once a constraint is there, then teacher ID can never ever have a value that is not present in the teacher table. If I have five teachers, and they are numbered IDs, one through five, then teacher ID can never ever get a value of 10. So as it stuns that can happen because there are no constraints, there is no actual relationship. It's just a record saying, Oh, this is the teacher ID, teacher, Id should go here. But once I create this relationship, we set up the constraint how we discussed constraints in our design, that that's a limitation to what can actually occur in this column. And what we will do know is actually just right click on the first one, which is teacher ID, and then you'll see that they have an option relationships.

That brings up a dialog box, which allows us to add, all right, and then that creates some generic thing I use the enrollments enrollments, that's not really what we want. So the name of the foreign key here usually indicates the tables that are related to each other. So we want his enrollments to be related to teacher. So we'll click on this one. And then we go over to this side and drop down the tables and phone specifications. And we can just click the ellipsis button here.

And then that brings up another dialog box, which is reiterating our name. And the one is here for me, because I had started it before. So this is what yours would look like. And that's fine. And then what we want to say is, what is the primary key table, no door call, or primary key for teacher ID is stored in our teacher table. So our primary key is coming from our teacher table.

And the column that has that primary key is our ID column. See that so when I switched it to teacher, it gave me the drop down, and I select ID and I want the ID From teacher from the teacher table to be directly associated with which column in the enrollments table. So it's already it's agreeing, oh, it's the foreign key tip, because it knows it's a foreign key table. I'm initiating this relationship creation from the foreign key table. So it's going to meet that assumption, but it doesn't know what columns. So I'm going to go ahead, and I have the responsibility of naming my columns such that I can know exactly what the relationship is.

All right. So there's no direct stipulation that teacher ID has to be named that way. I could have said Puppy's ID, but that would be very bad practice on my part, because later down the line, if somebody inherits this database, they would know why puppies is related to teachers, right? So I kept call the teacher ID so that if I did this work, fell asleep and looked at it the next day, I could remember that teacher ID was earmarked to be the foreign key column for my teacher relationship. So teacher ID Here in our enrollments table will be directly related to the ID column from our teacher table. And then I click OK. And then you probably just want to update the name here just to see enrollments, teachers, just so that if you look at it tomorrow, you'll know that this foreign key is between the enrollments and the teachers.

And then you can click Close. All right, and then you see it made a change, and it's asking us to see. And then vice when we are when it asks us to see, it's telling us that we're about to make some changes to the teacher and enrollment. So that means it's making some structural change in the backend to associate the teacher and the enrollments. So I can just click Yes. And that will happen that that will be accepted.

So I just go ahead and do that same activity for students ID and I'll do them slowly. So we have The first one, and then we can just go ahead and add. And now we're going to do the same steps where we just click on our ellipsis button in the tables and column specifications space. And then this time we're doing students ID. So I say I want the primary key table to be students. And then when I drop this down, you'll see all of the student columns, but I know I'm only interested in the ID.

And on the foreign key side, I'm only interested in students ID. And so I click OK. And then this one was automatically named, because I didn't edit it. So that is what should happen. That is what probably happened for you and teachers. And that is what really should happen. So once you set up that relationship, we will actually rename it accordingly.

All right, and then we noticed that it's the same step, regardless of which one we click so even if we close this and then do it on the other one, we're still going to get this dialog box. So what I'm going to do is stay here and just go ahead and add another because I know I have a third one to ask. So I just click Add, and that gives me a third foreign key relationship. And then I'll just click that. And then this time we're dealing with course, so courses and then drop a donor we see course ID. So you see that sometimes I have ID sometimes Of course ID.

So in the courses table, I could have said Id, but I think I explained earlier that you know, it's can be used interchangeably. It's up to you and your pattern and your standards. And so courses course ID, and in the enrollments table, the foreign key column is course ID. All right, so we see a direct match there. So sometimes I don't like doing this because having course ID in the home table and and in the foreign key table can lead to confusion at times. But so I usually just use ID as the ID column.

So we can just go ahead and continue. Click OK. And so we have our three foreign keys and The Romans the courses, enrollments, students and enrollments, the teachers. So that's saying that the enrollments table is now directly related to courses, students and teachers. So once again, the whole purpose of adding those relationships is to set the constraints between what value is allowed here, relative to what value is available here, if the value is not available here, meaning there is no course with ID 10, then the enrollments deal with cannot have 10 in this column, because this column is directly bound to the ID column in our in our periods table. Alright, so just go ahead and save this and you'll see that it is trying to make changes to all the tables that were involved in that relationship creation process, we click Yes. And voila.

Now we can refresh the tables and see our newly created table there. And if I drill down then and click on the keys, then you see that this enrollments table actually has four keys, all right, but then if you take keen pay keen attention to the direction of this key, you'll see that this one is pointed to the left, and that's the pk. So that's showing that's primary key. And then all the others are pointing to the right to show you a foreign key in SQL, express Destro, they do not have that capability to show you the database diagram, you may have to download that separately or use developer to get that functionality. But at the end of the day, you can always just ensure that those keys are there, and you know that those are created properly. So I'm going to add one more column to this table, and I'm going to call the grid.

And the reasoning behind this is when we have a student doing a course he needs a grid, right? Well, then they're all atomic, so I can put greed in the course because there are many students during the course. To that, that wouldn't make sense terms of travel agreed in the courses table. And the student is doing many courses. So he's been taught many grades. So it doesn't make sense me trying to put the grid on the students record.

So what I will do is just put it in the enrollments section where I know that the teacher who is teaching the students through this course is going to give them a grade for this course. Alright, so that is that is the kind of thinking you have to employ when designing a database. So once again, this enrollments table is going to have the teacher who is teaching the students and many teachers can be teaching many students at anytime, and they can be teaching them multiple courses, right. And so for each instance, where you have this specific teacher teaching this specific students in this specific course we give them this specific breed saving if they repeats, we know that each record is going to have a unique greed tool. These three values. So I can just give that in char, I'll just leave that in char and the grid would be a little grid.

So I'll just see one, let me see two, because it could be a minus someone to give them two characters to put in that grid. And I'll save changes to that table. And so we have that relationship table, this table is a bridge to allow these three tables to be related to each other. All right. And there are situations where you have, you know, you have a table and you're going to put in a foreign key in that table. So this was not one of those scenarios.

So it's not a one size fits all, you don't just look at one relational database and then say, Oh, I'm going to use those exact techniques to build my own database in the school management system. The different rules apply to the previous system, the previous database that are shown in the last video where I was doing predictions For worth of matches, alright, so you just have to understand the core concepts and apply them as you go along. So once again, is this entity going to be atomic? Or is it going to have dependencies in this situation, of course doesn't have a dependency student doesn't have a dependency. And a teacher doesn't have a dependency or can exist by themselves. However, for an enrollment or occur, I can only have a record of a teacher teaching my students and this course through an enrollment and so my enrollments table has the dependencies on the teacher table, the student table and the course table.

And while it has its own columns afterwards, so you could always flesh this out an add on date of enrollment or date of start date and an end date. So this would actually creates an instance of when somebody being a student was taking a Class being taught by this teacher, the start of the semester, the end of the semester, those kind of things you can always put into this, but it does have three dependencies on three different tables. Alright, thanks for watching. Do remember to leave any feedback or ask any questions if you are still unclear as to what was going on here. Be quick to reach out

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.