So in the previous lecture we are able to create a new table and that is for phones.
Therefore we have students table and have the phone table.
Now we want to have a difference between students and phone.
We want each student to have a phone, and we want to be able to reference both of this table.
And that is when foreign key comes into play.
So when we talk about a foreign key, a foreign key is a column or a group of columns in a table that
uniquely identifies a row in another table.
So there must be up to two tables.
And that is why I was able to create this table called phone table in the previous lecture.
So a foreign key establishes a link between the data and the two tables by referencing the primary key
or a unique constraint of the reference table.
So that table that contains a foreign key is referred to as the reference interval or a child table.
All right.
So I have this table and I have an active constraint.
And we all know about these two tables.
Now let me go ahead and open up my Visual Studio.
So I have the student table which I have here.
And I have the front table which is right in here.
Now I have about 1000 data to deal with in both of these, and for quality purposes, we want to just
limit the number will be just like three from the front table and maybe like 4 or 3 from the student
table, so that we can be able to see the results.
Because if you have a large data, remember it is really, really so bogus for us to see what we are
doing.
Therefore, I open up my atom.
I have an atom editor, which I have right in the end.
And here I'm going to insert this three, uh, rows from the phone and these four from the students
table.
And then I'm going to create a new table called phone.
And I'm going to create a new table called students.
And we're going to insert everything right back inside here.
Now, what I actually did is that I'm going to minimize this.
Go over to your students table and you can open another tab called uh, go ahead and open up that file.
And you can call that file maybe student phone dot SQL.
Okay.
But I just created that with my Acton.
And that is why I have this in atom called students I think phone dot SQL because I just want that to
be unique.
So you don't need to go ahead and open in another editor.
Go ahead and use just send editor.
And what you need to do is go over to phone and then you copy the create table.
We're going to just use that and then go over to students and copy exactly this.
Then you can copy it maybe the very first three or the very first four on this end right in here.
Okay.
That is what I just did.
Exactly.
So there is no thing back here.
Okay.
I copied the phone because I want the phone to come first, and then I want the table for students to
come second.
Then this doesn't matter.
Whichever comes first.
All right.
It doesn't matter whichever that comes first.
In the strength comes first or the phone comes first.
But now we are going to have something in common.
Because if you look at this table is different and this table is actually different.
That is not the referencing these two tables.
And that is where we are going to concentrate on.
We're going to have some reference in both tables.
And let's go ahead and do that.
So we have to first of all move over to the students table because we want the student to reference
the phone, meaning that we want each of the student to have a phone attached to them.
So we want to concentrate on the strings.
I use that to reference the phone table.
So go over to student.
I'm going to put a comma right in here.
And uh let's go ahead and uh move down.
And then let's go ahead and make this country to be not null.
All right.
Not null.
And then we go right here and I'm going to use the id which is the ID right in here.
And I'm going to give that a name maybe the phone ID.
So I'm going to say this is a phone underscore ID.
And we're going to use no longer the big area because this autoincrement we want to use the big INT
because that will help us to hold a right data and risk.
When I say big int and it's going to refresh the font table.
Therefore I'm going to say uh refresh and uh, and refreshes the phone.
Okay.
And then we're going to pass in this phone ID which is this ID right in here.
And for us to do that we just go ahead and put in ID and then we put a comma right in here because we
need a unique constraint for this particular, uh, column right in here.
So we're going to make that to be unique.
So currency unique.
And then we'll go ahead and pass in the phone underscore ID.
Now if you have done this what you need to do is to go ahead and import this.
Okay.
Go ahead and save this.
Try to save that because I didn't import that just as we always do.
Now if you want to import that the squad head and move over to SQL shell because we have table course
there's a table called phone.
If we try to import that we are going to run into an error.
So let's go ahead and see what we see and check our database table.
You can see we have a phone table and help students develop.
All we need to do is that we go ahead and drop these two tables.
So after we drop this two table that then we can now go ahead and uh do what.
Import the new table.
And to drop this table, all I need to do is to go ahead and say drop table, and the table I want to
drop is a student, and then go ahead and put a semicolon at the end and hit enter and it will drop.
And I'm going to drop the second table drop table.
And then the table is fun.
And go ahead and put a semicolon at the end and hit enter and have dropped the second table.
So if I go ahead and say blackboard slash d t for the database table and hit enter, I can see did not
find any relation.
And now I can go ahead and import the new table which is a student underscore form dot SQL.
And for us to import these I have this table right inside uh, this folder uh the the name of that folder
is uh program data to go ahead and open up the folder name so it can be good to go right in there.
So that is right inside the downloads and uh programs or initiatives.
Go ahead and copy the parts as usual.
And if I copy the part, I'm going to open up, uh, this, uh, notepad and I'll paste that.
And I've already done that immediately and change all these to a forward slash.
Ensure that you do that.
Then go ahead and copy that.
And uh go ahead and minimize that.
Always go ahead and then go ahead and click this.
First of all before we go ahead and import it.
So go ahead and see backward slash I and then press the button you just copied.
And now I'll go ahead and hit enter.
And you can see I have the table created for me.
And this table has a the four rows for the students and three rows for falls.
And if I go ahead and say backward slash and hit enter, I still have my phone and students table right
in here.
I hope that is cool.
And, uh, we can also go ahead and, uh, check each of them one by one and see them as and the properties
of each of these tables.
So I'll just go ahead and say the selector.
Select all from students and put a semicolon at the end and hit enter.
And I have these right in here.
Now if I do that for the second one and go ahead and say select.
All from foreign and put the stomach rod end and hit enter.
And you can see I have the second table for the form right in here.
And now in the next video we go ahead and uh, work with this and see how the foreign key actually works,
because you can see that under the form for students.
I can see the font ID here.
We have nothing right in here.
And in the next video we go ahead and work with that.
So thank you so much.
And I'm going to see you in the next video lecture.