Insert Records in Related Tables

Microsoft SQL Server 2017 for Everyone Relationships and Foreign Keys
12 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.24
List Price:  €96.07
You save:  €28.82
£55.68
List Price:  £79.55
You save:  £23.86
CA$100.66
List Price:  CA$143.82
You save:  CA$43.15
A$111.82
List Price:  A$159.75
You save:  A$47.93
S$94.88
List Price:  S$135.55
You save:  S$40.66
HK$543.79
List Price:  HK$776.87
You save:  HK$233.08
CHF 62.67
List Price:  CHF 89.54
You save:  CHF 26.86
NOK kr792.28
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr501.55
List Price:  DKK kr716.53
You save:  DKK kr214.98
NZ$123.71
List Price:  NZ$176.74
You save:  NZ$53.02
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳8,328.16
List Price:  ৳11,897.89
You save:  ৳3,569.72
₹5,958.01
List Price:  ₹8,511.81
You save:  ₹2,553.80
RM314.25
List Price:  RM448.95
You save:  RM134.70
₦108,236.03
List Price:  ₦154,629.53
You save:  ₦46,393.50
₨19,398.69
List Price:  ₨27,713.60
You save:  ₨8,314.91
฿2,394.21
List Price:  ฿3,420.45
You save:  ฿1,026.24
₺2,465.01
List Price:  ₺3,521.60
You save:  ₺1,056.58
B$426.13
List Price:  B$608.79
You save:  B$182.65
R1,290
List Price:  R1,842.93
You save:  R552.93
Лв131.30
List Price:  Лв187.58
You save:  Лв56.28
₩101,582.03
List Price:  ₩145,123.41
You save:  ₩43,541.37
₪254.54
List Price:  ₪363.65
You save:  ₪109.10
₱4,098.92
List Price:  ₱5,855.86
You save:  ₱1,756.93
¥10,967.38
List Price:  ¥15,668.35
You save:  ¥4,700.97
MX$1,406.50
List Price:  MX$2,009.38
You save:  MX$602.87
QR254.06
List Price:  QR362.96
You save:  QR108.89
P963.25
List Price:  P1,376.14
You save:  P412.88
KSh9,053.20
List Price:  KSh12,933.70
You save:  KSh3,880.50
E£3,568.78
List Price:  E£5,098.48
You save:  E£1,529.69
ብር8,687.63
List Price:  ብር12,411.43
You save:  ብር3,723.80
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,297.02
List Price:  CLP$99,000
You save:  CLP$29,702.97
CN¥510.87
List Price:  CN¥729.84
You save:  CN¥218.97
RD$4,243.89
List Price:  RD$6,062.96
You save:  RD$1,819.07
DA9,435.69
List Price:  DA13,480.14
You save:  DA4,044.44
FJ$162.05
List Price:  FJ$231.51
You save:  FJ$69.46
Q536.99
List Price:  Q767.16
You save:  Q230.17
GY$14,580.66
List Price:  GY$20,830.41
You save:  GY$6,249.74
ISK kr9,757.30
List Price:  ISK kr13,939.60
You save:  ISK kr4,182.30
DH701.41
List Price:  DH1,002.06
You save:  DH300.64
L1,291.33
List Price:  L1,844.84
You save:  L553.50
ден4,135.36
List Price:  ден5,907.91
You save:  ден1,772.55
MOP$557.92
List Price:  MOP$797.07
You save:  MOP$239.14
N$1,283.07
List Price:  N$1,833.03
You save:  N$549.96
C$2,564.57
List Price:  C$3,663.83
You save:  C$1,099.26
रु9,479.94
List Price:  रु13,543.35
You save:  रु4,063.41
S/259.51
List Price:  S/370.75
You save:  S/111.23
K282.61
List Price:  K403.75
You save:  K121.13
SAR262.99
List Price:  SAR375.71
You save:  SAR112.72
ZK1,928.73
List Price:  ZK2,755.44
You save:  ZK826.71
L334.62
List Price:  L478.06
You save:  L143.43
Kč1,690.23
List Price:  Kč2,414.72
You save:  Kč724.49
Ft27,882.67
List Price:  Ft39,834.10
You save:  Ft11,951.42
SEK kr772.23
List Price:  SEK kr1,103.23
You save:  SEK kr331
ARS$71,547.27
List Price:  ARS$102,214.77
You save:  ARS$30,667.50
Bs481.59
List Price:  Bs688.02
You save:  Bs206.42
COP$305,890.51
List Price:  COP$437,005.17
You save:  COP$131,114.66
₡35,162.35
List Price:  ₡50,234.08
You save:  ₡15,071.73
L1,769.11
List Price:  L2,527.41
You save:  L758.30
₲543,432.39
List Price:  ₲776,365.26
You save:  ₲232,932.87
$U3,131.93
List Price:  $U4,474.38
You save:  $U1,342.44
zł286.59
List Price:  zł409.44
You save:  zł122.84
Already have an account? Log In

Transcript

Hey guys, for this video, we will be looking at inserting records into tables that have relationships. So in previous videos, we would have already inserted some records into some of our tables. And I discussed the atomic tables versus the ones that have dependencies, or periods tables versus child tables. That's another expression you hear in database nomenclature or database terminology. So we have courses and we created some of them just right clicking tables, I'm going to edit top 200 because now we're supposed to be comfortable with our editor. And we should be proficient enough in SQL to be doing our SELECT statements if we want to see what's there.

But for expediency for speed, I'm just going to use the editor for most of these operations. So we didn't put in any teacher so I'm just going to go ahead and insert about three teachers. Alright, so I did three teachers and notes because they use the date as opposed to date time we don't see a timestamp on on the day. Joined column. So that's what dates looks like. I think we did.

Date of Birth had the timestamp, right? So I'm just showing you the different data types. Once again, beats may not be available in previous versions of SQL and in your corporation, you may not be using the latest one. So I'm just showing you all of the different scenarios so you can act according to your context. So we have three teachers we have a few students, and we have a few courses know what we don't have our enrollments, we don't have any teacher activity teaching any students through any course. Alright, so that is why we created the enrollments table so we could actually bind them together through relationships.

So I'm just going to edit the top 200 and then you will notice based on our previous design and and based on what's on the screen know, all that is allowed in these columns are integer values. They're just IDs right and agreed. We had an string value a lot for this, but these are only willing to accept integer values. So what this means is when we have a table with relationships, and we set up the relationship to be on the primary key columns, and the IDS have too much, and they're all integers, your primary key foreign key column is always just going to have the referencing ID. So the detailed record, and that was discussed in the first video of this series. So once again, we're not repeating the teacher's details every time there's an enrollment, because if john is teaching multiple courses, then we don't need to see john, john, john, john, john, john, john, every time you see john, of course, we're seeing john throughout the database, right, that would lead to messy data.

And that's a part of normalization where we're cutting out all of that repetition. So if you realize you're in a situation where you The details of something is going to be repeated more than once, then you probably need to look at creating another table to have that detailed information once and then what you do is only repeat that referencing ID to that detail information. So that is that is how normalization works, you will see things about first Normal Form second Normal Form third normal form. To me, those normal forms are important when you're looking at already messy data and trying to fix it. But for me, I skip past the normal forms. And I just say design the database in a normalized form from day one.

If you see repeating details, put them in a table for themselves, so they only appear once and then repeat the ID associated with for whatever it is. So that is what we're doing here. So instead of repeating the teacher's details, because john is currently going to be teaching the top three courses in SQL and PHP, we want to just Repeat his ID. So we'll associate the ID two with the courses. One, two, and three. All right.

And I'm going to give him one students for each. So Jody McIntosh is currently in all three of those courses being taught by john. All right. And the reason I'm doing that is the fact that we have IDs, we can't insert any Oh, I did, I did alone No on my design, so I just pop on over to the design quickly. And I did a little note, which means that we don't have to enter all of the values. The only thing that is really absolutely necessary is the ID.

So we can do that. So as it stands, we have given john with ID to the course the schools one of these the courses, HTML, SQL, and PHP, and let's just see, he has no students at the moment. So we're going to associate teacher ID two is john with course ID one. I can press enter and see it says it's an all students. He's also teaching course ID two. And he's also teaching course ID three.

All right? No, this kind of looks stupid. I can appreciate that because then you're moving away from clearly seeing who teach ID to john. And here no just looking at twos and threes and numbers right, though, from a human perspective, it may look foolish because we prefer to see letters or numbers, but the computers thrive more on numbers. So it's actually more efficient to get this number and find all the details in somewhere else than to have those details repeating every time and that will actually lead to a slow database and a slower application overall because I figure you're building your database here to use in an application. And that's point number one.

Point number two is that because you're using your database in an application, filling out these IDs would be much easier from an application standpoint. If you're not an app developer just yet, that's fine. But I'll just give a high level overview. But do remember that when you're when you would be filling out and creating these associations, you would kind of be dynamically associating the controls in your app with the values from the ID columns. And so you can pass that into your database quite easily through code. And you wouldn't have to necessarily manually come in here and actually be writing twos and threes and ones and twos.

All right, but because we're learning database in the context of database design, I'm just showing you exactly how this data goes into the database. So back to what we're doing. We actually have associated john with courses One, two, and three. So if I look at this enrollment record, I see john teaching course one, which is HTML. See, john also teaching course two, which is SQL. And he's also teaching course three, which is PHP.

And she has given all grades because well, quite frankly, he has no students. So I did say that Jodie is actually taking all three of those courses. So now that we know Jody has enrolled because she looked on the timetable and saw that john is teaching these are these are the three courses being offered by john. And once again, you could have put on two semester details and whatever. Whenever I leave the campus, it's your database, you can do the design, though Jody is interested and Jody is going to click enroll so she goes on to the up the school management up school management system, and she enrolls in these three courses are being taught by john So know her ID, which is three gets associated with john, and each course. So this is what I was saying about repetition.

Imagine if we had john, john, john, Jodi, Jodi, Jodi and HTML, PHP, and and SQL. And it would be very tedious when balloons into more than one and then you have to track all of the John's and all of the journeys. We can just say, which student is doing this with john, we just look for our students ID three, and we see everything about God in one place. All right, so it could get messed up if it's in more multiple places. So we try to keep that repeating data. One place, all right.

No, here's another scenario by Troy has decided to also do SQL and PHP. He's not doing HTML, he's only doing SQL and PHP. Now here's the beauty of using our relationships. We can actually repeat this so we know john is teaching that course. So we can just put in john twice. And I'm just doing this.

So we know Johnny's teaching that and we did say the students in question would be Patra his idea six, so we just put in six and six. I'm sorry something's going on here. Let me do that over. All right, so we're associating petroi with john once more so that out, and he's doing courses one and three only. Alright, so here we see teacher ID two. So we see john is teaching all these courses and he has two students so far in these courses.

So now we can track directly if we were to pull all of the HTML students that john is teaching, we could just look at all of the course ID one and teacher ID two and then we will see all of the student IDs coming back in that query. Alright, and we'll get into that. Eventually, we're just looking at inserting and we will look Add how we go about pulling reports because I mean, obviously, we're only using numbers. So it will take a bit more cursing of the data to actually accomplish viewing certain things. So I'm just going to use this time and I'm just going to fill out some more data. And I'm going to put in about 20 records of enrollments and we'll reconvene.

Alright, so I said, so internality 11, I'm sorry, it's kind of tedious and annoying, and I'm sure you're, you're experiencing the same thing. But I don't know if you've done this as yet, but I'm just going to display something to you. So do remember that by setting up the relationship, we would have enforced certain constraints. The we shouldn't be able to put in a teacher ID that does not exist in our teacher table. So if in enrollments I tried to put in the value 10 and then I just fill out the rest and then move on to the next line. Then it will tell me nothing happened.

It was not clear. into it. And the error message is that it conflicted with the foreign key constraints, the conflicts that so it's not really telling you Oh, there's no value 10 in the parent table, but that is essentially what it is what is happening in the constraint is that no teacher ID value can exist in our relationship in our foreign key column that does not exist in the primary key column of the primary key table. Alright, so that is what that that brings up both. So if we were repeating details and seeing john all the time, then somebody could come in and say, oh, spell his name Quan, because they're trying to be cute or funny or something. And then that would lead to some data integrity, because then we will be wondering who is this one what Where did one come from, and then we really wouldn't know who this teacher was.

So by using foreign keys in a relationship where enforcing certain data integrity across the board to ensure that no value goes in without It being vetted by a second source, so to speak. So I can assume this little one and press Enter. And when I'm pressing Enter, it's telling me another arrow. But then this time it's with the students because I don't have a student with ID two. All right, deleted that one. So I'm just going to change that ID to 12.

And voila, so I have a rich mix of students and teachers, and it's all looks confusing by this point. If I went all the way up, it would have been even more confusing, and rightfully so. So, I mean, inserting isn't the cutest process and procedure and even looking at it, it's not very intuitive, as at least it's not as intuitive as looking at the teacher and the students and the courses. So what we'll do next is look at how we can go about pulling meaningful data from this table when it looks like this.

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.