- Read Tutorial
- Watch Guide Video
In this walkthrough, we are going to add one more table. Now we're not really going to do anything different than we've done before. I think it'll be a very good practice to add one more table. I also think that a course on relational databases that only had two databases would be kind of boring.
So we have users and addresses table but let's imagine that we're building an LMS (learning management system). We want the ability for our users to create guides. So let's go through the process. Right-click on table and then click ‘create table’. This is going to be called guides and we're going to go through what we've pretty much done before.
So we're going create a column that says guides_id
. This is going to be the primary key, unique, auto-incrementing, and It's going to be not null.
The next column we're going to make is guides_revenue. So we want to see how much money each one of these guides make. So for that as to guides revenue, we're going to use a different data type than we've used before. We're going to use the Decimal data type now.
Just know we're going to pick something that's not going to be of varchar because later on in this course. When we talk about doing things such as averaging and summing guide the revenue. We need to have a data type that can actually work with those functions. So we do not want this to be null and everything else is going to be the same.
Lastly, we want to have a foreign key. Adding a foreign key is probably one of the more tricky things that you will do when creating a new table. So I wanted to go through that process once again and I'd definitely recommend if you're doing this course for the first time.
Take a second to see if you can go through the steps needed to create a foreign key by yourself. Before we go any further I'm going to go through this and guides_users_id. This is going to be set as an integer. We also want this to be not null. Now coming down to the foreign keys tab. I'm going to pick out the item that we want to be the foreign key which is going to be guides_users_id. This is going to reference the “users_table” and then inside of this, we want to reference the users_id. We also want to make sure that all of the guides get removed whenever we delete a user. So I'm going to set the “on delete” to cascade.
Now one of the other potential items “On Delete” could be NULL. This would be kind of an interesting approach. So let's imagine that you have something you're building now, which is a Twitter clone. As you build it, what would happen to the tweets when a user deletes their account?
Well by default, you usually are going to see something happen where the tweets get deleted when the user gets deleted. That's how cascade works. Now if you have a scenario where you didn't want those tweets deleted or in this case these guides to be deleted. You simply wanted the value to be set to null what would mean is that if a user deletes their account? All of the different guides that are related to that user are no longer going to have that old user_id.
The user_id are just going to be “null”. Now that can be something that's a little bit tricky to implement. Let say that you have some type of function in your application where it looks through and it calls the user's first name. Then that shows the first name on the screen.
Well if you try to do that and you hit a null value, then your application is going to run into an error. So you have to create some workarounds that say “OK this is what happens when it's a null value in that foreign key”. So by default, usually I'm going to pick cascade.
Well, let's go back to columns tab. Everything here looks good. if I click Apply now I can click apply right here. This is going to execute and everything looks good. So if I go down to guides columns and you can see all of this is here.
Last time we talked about how we could edit a column. What happens when we do something like we just did? We want to add a new column to the guides table? It’s going to be a very similar process but I wanted to make sure that we cover that here.
So what I can do is, I can just click on a new column “” here.
In this case, we want to have a title. Now I will call that column “guides_titles”. I'm going to say we want the varchar data type and allow 150 characters. We also want this to be not null. Now that we have all of this in place all you have to come down and click apply.
This is going to pull up this script that’ simply going to add a columns. So it's going to say I want to add a column to the guy stable with 150 characters. It's not allowed to be normal and they're just going to place it right after the guide's_user_id. Now if I hit apply this all ran and now if you click down in your guides table you can see a column with guides_title.
That's how you can create a table with a foreign key, a primary key, And also you can alter a table after it's been created. That’s how you can actually add a new column to your table.