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
€64.33
List Price:  €91.90
You save:  €27.57
£55.09
List Price:  £78.70
You save:  £23.61
CA$95.29
List Price:  CA$136.14
You save:  CA$40.84
A$104.44
List Price:  A$149.21
You save:  A$44.76
S$94.20
List Price:  S$134.58
You save:  S$40.38
HK$546.10
List Price:  HK$780.18
You save:  HK$234.07
CHF 63.62
List Price:  CHF 90.89
You save:  CHF 27.27
NOK kr751.73
List Price:  NOK kr1,073.95
You save:  NOK kr322.21
DKK kr480.45
List Price:  DKK kr686.39
You save:  DKK kr205.93
NZ$114.09
List Price:  NZ$162.99
You save:  NZ$48.90
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,195.22
List Price:  ৳11,707.96
You save:  ৳3,512.74
₹5,830.20
List Price:  ₹8,329.22
You save:  ₹2,499.01
RM328.07
List Price:  RM468.70
You save:  RM140.62
₦102,885.30
List Price:  ₦146,985.30
You save:  ₦44,100
₨19,393.48
List Price:  ₨27,706.17
You save:  ₨8,312.68
฿2,536.99
List Price:  ฿3,624.43
You save:  ฿1,087.43
₺2,256.71
List Price:  ₺3,224.01
You save:  ₺967.30
B$357.27
List Price:  B$510.40
You save:  B$153.13
R1,265.02
List Price:  R1,807.25
You save:  R542.23
Лв125.89
List Price:  Лв179.86
You save:  Лв53.96
₩94,793.75
List Price:  ₩135,425.45
You save:  ₩40,631.70
₪259.28
List Price:  ₪370.41
You save:  ₪111.13
₱4,039.50
List Price:  ₱5,770.97
You save:  ₱1,731.46
¥10,895.69
List Price:  ¥15,565.94
You save:  ¥4,670.25
MX$1,162.43
List Price:  MX$1,660.69
You save:  MX$498.25
QR253.91
List Price:  QR362.75
You save:  QR108.83
P948.27
List Price:  P1,354.73
You save:  P406.46
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,283.23
List Price:  E£4,690.53
You save:  E£1,407.30
ብር4,009.02
List Price:  ብር5,727.42
You save:  ብር1,718.40
Kz59,310.71
List Price:  Kz84,733.22
You save:  Kz25,422.51
CLP$62,605.89
List Price:  CLP$89,440.82
You save:  CLP$26,834.93
CN¥505.55
List Price:  CN¥722.24
You save:  CN¥216.69
RD$4,078.44
List Price:  RD$5,826.59
You save:  RD$1,748.15
DA9,404.82
List Price:  DA13,436.03
You save:  DA4,031.21
FJ$155.95
List Price:  FJ$222.79
You save:  FJ$66.84
Q543.74
List Price:  Q776.81
You save:  Q233.06
GY$14,641.89
List Price:  GY$20,917.88
You save:  GY$6,275.99
ISK kr9,678.91
List Price:  ISK kr13,827.61
You save:  ISK kr4,148.70
DH693.20
List Price:  DH990.33
You save:  DH297.13
L1,231.95
List Price:  L1,760
You save:  L528.05
ден3,957.99
List Price:  ден5,654.52
You save:  ден1,696.52
MOP$562.43
List Price:  MOP$803.51
You save:  MOP$241.07
N$1,273.79
List Price:  N$1,819.78
You save:  N$545.99
C$2,572.13
List Price:  C$3,674.63
You save:  C$1,102.50
रु9,330.17
List Price:  रु13,329.38
You save:  रु3,999.21
S/260
List Price:  S/371.45
You save:  S/111.44
K271.59
List Price:  K388.01
You save:  K116.41
SAR262.46
List Price:  SAR374.96
You save:  SAR112.50
ZK1,777.13
List Price:  ZK2,538.86
You save:  ZK761.73
L320.28
List Price:  L457.57
You save:  L137.28
Kč1,590.03
List Price:  Kč2,271.57
You save:  Kč681.54
Ft24,925.85
List Price:  Ft35,609.88
You save:  Ft10,684.03
SEK kr750.08
List Price:  SEK kr1,071.59
You save:  SEK kr321.50
ARS$62,063.63
List Price:  ARS$88,666.13
You save:  ARS$26,602.50
Bs483.56
List Price:  Bs690.83
You save:  Bs207.27
COP$266,463.69
List Price:  COP$380,678.73
You save:  COP$114,215.04
₡35,817.30
List Price:  ₡51,169.76
You save:  ₡15,352.46
L1,723.87
List Price:  L2,462.78
You save:  L738.91
₲524,369.02
List Price:  ₲749,130.70
You save:  ₲224,761.68
$U2,697.33
List Price:  $U3,853.49
You save:  $U1,156.16
zł274.39
List Price:  zł392.01
You save:  zł117.61
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.