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
€66.16
List Price:  €94.53
You save:  €28.36
£54.85
List Price:  £78.36
You save:  £23.51
CA$98.25
List Price:  CA$140.37
You save:  CA$42.11
A$108.91
List Price:  A$155.59
You save:  A$46.68
S$93.78
List Price:  S$133.98
You save:  S$40.19
HK$544.47
List Price:  HK$777.85
You save:  HK$233.38
CHF 61.51
List Price:  CHF 87.87
You save:  CHF 26.36
NOK kr773.45
List Price:  NOK kr1,104.97
You save:  NOK kr331.52
DKK kr493.50
List Price:  DKK kr705.03
You save:  DKK kr211.53
NZ$119.60
List Price:  NZ$170.87
You save:  NZ$51.26
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,338.24
List Price:  ৳11,912.28
You save:  ৳3,574.04
₹5,926.99
List Price:  ₹8,467.49
You save:  ₹2,540.50
RM309.32
List Price:  RM441.90
You save:  RM132.58
₦112,431.93
List Price:  ₦160,623.93
You save:  ₦48,192
₨19,444.97
List Price:  ₨27,779.72
You save:  ₨8,334.75
฿2,384.84
List Price:  ฿3,407.07
You save:  ฿1,022.22
₺2,433.67
List Price:  ₺3,476.83
You save:  ₺1,043.15
B$423.60
List Price:  B$605.16
You save:  B$181.56
R1,262
List Price:  R1,802.94
You save:  R540.93
Лв129.37
List Price:  Лв184.83
You save:  Лв55.45
₩99,401.46
List Price:  ₩142,008.17
You save:  ₩42,606.71
₪251.78
List Price:  ₪359.70
You save:  ₪107.92
₱4,052.96
List Price:  ₱5,790.19
You save:  ₱1,737.23
¥10,538.90
List Price:  ¥15,056.22
You save:  ¥4,517.32
MX$1,416.05
List Price:  MX$2,023.01
You save:  MX$606.96
QR254.81
List Price:  QR364.03
You save:  QR109.22
P950.66
List Price:  P1,358.14
You save:  P407.48
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,497.86
List Price:  E£4,997.16
You save:  E£1,499.29
ብር8,738.25
List Price:  ብር12,483.75
You save:  ብር3,745.50
Kz63,935.86
List Price:  Kz91,340.86
You save:  Kz27,405
CLP$67,951.45
List Price:  CLP$97,077.66
You save:  CLP$29,126.21
CN¥508.41
List Price:  CN¥726.33
You save:  CN¥217.92
RD$4,232.13
List Price:  RD$6,046.17
You save:  RD$1,814.03
DA9,325.29
List Price:  DA13,322.42
You save:  DA3,997.12
FJ$159.43
List Price:  FJ$227.77
You save:  FJ$68.34
Q540.22
List Price:  Q771.77
You save:  Q231.55
GY$14,598.38
List Price:  GY$20,855.73
You save:  GY$6,257.34
ISK kr9,627.82
List Price:  ISK kr13,754.62
You save:  ISK kr4,126.80
DH694.75
List Price:  DH992.55
You save:  DH297.79
L1,283.63
List Price:  L1,833.84
You save:  L550.20
ден4,072.25
List Price:  ден5,817.75
You save:  ден1,745.50
MOP$561.43
List Price:  MOP$802.08
You save:  MOP$240.65
N$1,263.31
List Price:  N$1,804.81
You save:  N$541.50
C$2,570.38
List Price:  C$3,672.13
You save:  C$1,101.75
रु9,453.87
List Price:  रु13,506.11
You save:  रु4,052.23
S/260.85
List Price:  S/372.66
You save:  S/111.81
K281.92
List Price:  K402.76
You save:  K120.84
SAR262.93
List Price:  SAR375.63
You save:  SAR112.70
ZK1,889.20
List Price:  ZK2,698.97
You save:  ZK809.77
L329.33
List Price:  L470.50
You save:  L141.16
Kč1,662.42
List Price:  Kč2,374.99
You save:  Kč712.56
Ft27,357.83
List Price:  Ft39,084.29
You save:  Ft11,726.46
SEK kr760.23
List Price:  SEK kr1,086.09
You save:  SEK kr325.86
ARS$70,846.37
List Price:  ARS$101,213.44
You save:  ARS$30,367.07
Bs482.17
List Price:  Bs688.85
You save:  Bs206.67
COP$309,876.34
List Price:  COP$442,699.46
You save:  COP$132,823.12
₡36,026.42
List Price:  ₡51,468.52
You save:  ₡15,442.10
L1,766.80
List Price:  L2,524.11
You save:  L757.30
₲545,825.56
List Price:  ₲779,784.23
You save:  ₲233,958.66
$U3,030.92
List Price:  $U4,330.07
You save:  $U1,299.15
zł282.33
List Price:  zł403.35
You save:  zł121.01
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.