Create Relationships with Workbench Table Design Tool

MySQL Database Development Mastery Relationships and Foreign Keys
20 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.96
List Price:  €92.81
You save:  €27.84
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$106.02
List Price:  A$151.47
You save:  A$45.44
S$94.41
List Price:  S$134.88
You save:  S$40.47
HK$546.80
List Price:  HK$781.18
You save:  HK$234.37
CHF 63.34
List Price:  CHF 90.49
You save:  CHF 27.15
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.42
List Price:  NZ$166.33
You save:  NZ$49.90
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,416.31
List Price:  ₨27,738.77
You save:  ₨8,322.46
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$356.70
List Price:  B$509.60
You save:  B$152.89
R1,295.44
List Price:  R1,850.72
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪259.50
List Price:  ₪370.74
You save:  ₪111.23
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,712.31
List Price:  ¥15,303.96
You save:  ¥4,591.65
MX$1,187.89
List Price:  MX$1,697.07
You save:  MX$509.17
QR254.57
List Price:  QR363.69
You save:  QR109.12
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.64
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,764.23
List Price:  ISK kr13,949.49
You save:  ISK kr4,185.26
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.59
List Price:  ден5,712.52
You save:  ден1,713.92
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,305.79
List Price:  Ft36,152.68
You save:  Ft10,846.88
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

Hey guys, welcome back. In this video, we will be working with our school database once more. and in this situation we will be modifying or altering the table to support foreign keys in some tables, we will be adding more tables to this database. And we'll just see exactly how we go about using workbench to facilitate all of these modifications that we're about to make. So in our previous video, we saw that when we were using our designer view, and we went ahead and use the relationship tool to create a relationship, even though we had already designed the table with a column that we had earmarked to be the foreign key column, the designer went ahead and created its own. So we could actually just get rid of this column out of the Orders table.

And it wouldn't be detrimental for anything because the designer gave us its own column for those Primary sorry for the forum. Foreign Key. However, when we're using this design view with an already existing database that we want to modify, the onus is on us to actually create these rules, these new columns sorry, and actually establish them as foreign keys. First order of business would be to run the scripts that are included with this video, and go ahead and insert some lectures and some courses out of this real courses quickly. So I took the liberty of entering some courses. You can enter your own if you wish, but I gave you the script so you could expedite that process.

And now we need to take a stock of what we have and what needs to happen. Now we're doing a school database. We already have three very important entities for any school to have. We have students, we have lectures, and we have courses so we have people to be taught People to teach and what to be taught. However, what we don't have is any way of associating these three entities with each other, because a class has all three elements in a class has students a class also has a lecture or at least one lecture and the class is centered around Of course. So that means we need some form of entity that will embody class which is going to have the associations for the students to the lecture to the course being taught.

No, the lack of diagram or the visual representation might be kind of a deterrent or discourage discouraging factor when dealing with us, modifying tables and putting in relationships, but it is something you will have to soldier through and learn to be comfortable with as long as you understand ization or rather how you go about associating an entity with dependency, then you should have no problem. That being said, in this file, I am depicting something that just occurred to me in talking about how we're going to include additional tables to facilitate the relationships. No, I did say a class would be a merger of a ledger student on course. But then it occurred to me that if I have a one table where you have many students, so you're going to have many students IDs, and then you're going to have one course that mean, all of these students are taking on to one lecture, then you're going to have many IDs and repeating courses and lecture IDs for many students.

And so that that while it could work, to me, that's just kind of unnecessary repetition. And I must warn you that this Kind of a diagnosis of what is unnecessary repetition can lead to what we call over normalization, where you try to breed on one table into too many small tables, and then end up with too many touch points and potential points for failure and break it down by data. So there is a middle ground and there's a balance that one must have when breaking down what needs to be related to watch and hold the facilitate these relationships. But in this situation, because I don't want the lecture and course ideas to repeat so much. I'm going to have one entity called class which is just meant to have the lecture and the course or courses so many lecture IDs and mini course IDs. So this is a link or table between the lecture table and the courses table because one lecture can be teaching many courses and this same course can be taught by many lectures, so we can have three lectures teaching into the programming.

And those same three lectures are teaching other courses. Alright, so that's why I'm creating that linker table just for these two. And you could push it a step further if you wish. And you could include a room, which would then because then if a lecturer is teaching a course, then they must be teaching it in a room. So then you would put in a room entity, which would have an ID and maybe a name. Alright, so you have a room, and a room stable, or entity.

And each room gets its own ID and its own name. And so a class would be a lecturer teaching this course in this room. And I mean, the ideas are just coming to me at this time. All right. So that that is essentially what the timetable looks like. All right, a timetable is going to show you who is teaching what, where and at what time All right.

And then an enrollment suggests that a student saw a class that he or she is interested in. So the student which will be referred to by ID and where I have referees that is just a foreign key reference. All right, and that's just my notation. This isn't necessarily standard. I'm just doing this to depict something too. Alright, so a student would see that Mr. Let me look at my lectures, Mr. Harold Benedict, Harold, is our how identity truther is teaching introduction to programming in science room, one at 4:30pm.

And so students one is interested in this class. So we just reference the class by ID and associated with that student. And so instead of having two columns repeating, which would have been lecture on course, as many students as subscribed to the same class, you have many students at IDs referencing one class ID. And then we can always look at the details of the class by going back to our class table and looking for that ID. And then we can further see the details of every other referenced entity by going to the original table and the candidate ID. And as we move along, we will be looking at how we go about extracting details of related data.

All right, so for now, we just want to set up this kind of relational system in our already existing database. So the first table I'm going to create is a class table and I'm just going to use the editor to do this, I won't create the script file, so I challenge you to either follow along or try to do it on your own. And I'm calling this one class or classes because I like giving my tables the plural name. Alright, and the first column is an ID column which is primary key or for implementing, and nodes that you don't always need a primary key auto incrementing when dealing with a link or table, so this is a link or table, he really doesn't need its own ID. But I'm going to practice what I preach. And I'm going to put an ID, and then we have a lecture ID.

So, lecture on a square ID and this is a naming convention because we need to be able to just look at the column header and know Oh, that's what it is. So by just seeing lecture on the square ID, I know exactly that if I see any value here, it is referencing some lecture in the lectures table. Also, the data type must match. So because our primary keys are all integers, in at least in this situation, because people do use things that are not integers for primary keys, but in this situation, whatever or ever situation, whatever the origin of the the type of the primary key is The foreign key must be the same. so in this situation we have int. Alright, and then we repeat that beat, I'm also going to set this to not know.

Well, I'm actually in the new does not as as I'm going to make it nullable. Alright, so that means we can look at the timetable from time to time and see that a class course is scheduled to be taught in a certain room at a certain time, but there's nobody assigned to it just yet. So that's another design consideration when to make something low level. and in this situation lecture ID is nullable. So courses on the score ID and once again, this must match the data set. And I'm going to start with those two I did include room and time but then that would require me to create the room table and and then we can put in the time, just the same.

Alright, so I put in time and I made it datatype time and remember if you select it from the drop down or move the parentheses, and then I ended up with a random column that I'm just going to right click, and delete. All right. So that is essentially it. And I'm going to meet these two not lovable. So our class can be created without at least the course and at least the time it's to be taught. But we can be still searching for the lecture even while we have scheduled look class.

Now in order to associate these two columns with their respective primary key tables, I'm going to go down to the bottom parts, the bottom set of tabs in this editor view, and I see here that I have columns, indexes and foreign keys. So this brings me to a new screen where I am asked for a foreign key name, and then it talks about a reference table. Alright, so the foreign key name for me, I usually give it a name that helps me to know which two tables are involved in this relationship. So this will be classes on the score lecturer, there is no real, there's no real stipulation as to what you must name it. This is just a practice that I use once again so that if I look at it tomorrow, I can exactly know what this foreign key meant and what this this relationship is for.

So after putting in the foreign key name, I No need to say which table is being referenced. And there are a friend's table in this situation because I named it classes underscore lecture, we're already modifying the classes table. So that means the reference table must be the lecturers table. Alright, and then over to the right hand side, while having this this foreign key entry still selected, we'll see the list of columns available to us because of what we're creating. In our classes table, know the column to be to be the reference point from our classes table for the classes to lecture relationship would be my lecture ID. So I take that, and then I go ahead and let them know that a referenced column meaning which column in the lectures table should this reference.

And that's easy, because it's the ID. So the lecturers that the ID sorry, from the lectures table is supposed to reference the lecture underscore ID, from our classes table, and that foreign key, that whole relationship will be called classes underscore the lecture. All right. And then to the right, we see that we have foreign key options aware on OBS or undelete. It says no action really great. No, but then you can change it to cascade or strict or set no meaning that if so, thing on either side of the relationship goes missing.

Or rather if the source the if it gets augmented, meaning if the source data gets deleted, you delete the lecture, then do you want us to delete all of the associations with this lecture? So everybody who has some relationship with a lecture table, those records will also be deleted? Or do you want me to just set that value too low? Or do you want me to restrict what you can do? Alright, so no action means that if if anything happens, just leave the data as it is. Now we'll repeat that feat with the next column which is courses.

So I need a lot of foreign key name or relationship a lot. This is classes on the score courses. And this time or referenced table would be school underscore DB dot courses, and then our column that should reference that is horses. Id and their friends column will be course ID. So you see that the names don't really matter much is just a matter of you keeping up with whatever you're doing and whatever the design is. And if you have a standard stick to the standard, alright, so I used just simply ID for the literacy that I use course ID for the courses table, but either way, the name is irrelevant.

The fact is, either one is acting as a primary key in its respective table. And I am making sure that I'm referencing them with the foreign keys in my resulting dependent table. All right, so when I'm satisfied with my design, I just set up the foreign keys, I have all my columns and if I go back over, you see, they have that red prison to the side showing that okay, there's a relationship here, one is filled where one is blank and the blank one is because I'm allowing it to be loadable whereas this one must exist for recording But once I'm satisfied with everything that I've done, I can just click Apply. And then that will generate this block of code, which actually recreates the table so to speak. Now we're seeing that we have a bit more code here, because yes, we should be familiar with all of this.

But then we're adding what we call an index, which is a high speed lookup point on a table. So a primary key is always an index. And by default, any foreign key will also become an index. All right, and then we're adding the constraints. And there is a name that we said classes underscore lecture. So the foreign key will be referencing the ID column in the lecture stable, and it just fills out all of that so you can see this and NP and copy and paste it in another editor and just assess the code that's been generated.

But ultimately, we can just click Apply, and then it will go ahead and execute that. script and alter our tape will create our table with those and it will essentially establish that referential integrity that is implied by our foreign keys. Now the next table that we have on our reader is enrollment and enrollment has a dependency on students and class. So I'm going to go ahead and do the same thing, just go to tables, create table. Now went ahead and filled out the table name and the column names and the data types and then making everything not null or not nullable because it doesn't make sense to have an enrollment without a class being referenced and the students being referenced and that's putting error there. Alright, and then we set up our foreign keys just the same way.

All right, so using the same naming convention, I associate the home table With the primary key table or the foreign key table, the primary key table, and I reference the table accordingly and then the columns and associated columns. Alright. And then once I'm satisfied with all of that, I just click Apply, I can review the code, but I just click Apply once more and finished. And then what happened just know was that there was an all creation of our enrollment stable, and it just appeared over here. Now, the next thing that if you want a visual representation, you can always go to database and you can reverse engineer or school DB database into the ER diagram so you can see all of the lines and the actual relationships that we just created. And in our next video, we will look at how we go about inserting data because if you were paying enough attention to what we were doing all we have at this point in classes, and enroll lense is a bunch of columns that are just supposed to be taking numbers.

So let's look at enrollments, enrollments have student ID, which is an integer class ID, which is an integer and an auto incrementing integer. So we moved away, or we are slowly moving away from having these kinds of, you know, data rich and clear tables where we have lastname firstname gets over two tables where we're just looking at a bunch of integers. All right. And so if I try to type something here, that's going to give an error if I try to apply because clearly that data type does not much what is expected because there are integers, all right. And just the same way that I cannot enter a class or student ID that doesn't exist. Because I have classes that are bigger than my classes stable.

I have no classes actually. So If I tried to enter a class ID here, and I know I have a student, I have students with ID number one, so I can just put in student one. If I apply this, I will get an error, because it's going to tell you that it cannot add. Well, it seemed that the constraint field because the class ID column value must match some value in the classes primary key, the primary key of the classes table. So as a result of the fact that we don't have any classes right now we have an empty table for classes. There is nothing for surface and bring up classes quickly.

And that's empty. So there are no classes to reference. So there is no way I can enter an enrollment for class number one when there is no class number one, and that is what a foreign key and relationship brings to the table and that is what we mean by referential you Integrity where data cannot exist or data can't exist in the pending table without it existing in the parent table. All right, so our next lecture, we're going to look at how we go about inserting records in tables that are, you know, have a dependency on another table.

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.