در حال بارگذاری ...

Creating a MySQL Database0:00

All right, so now that we've managed to build, admittedly, a very basic and primitive router, I think the next order of operations, or the next order of business, is to learn how to interact with a MySQL database. So I'll warn you, there's a lot to cover here, but I hope you're excited. Let's get going. The first thing we should do is create a database, and of course, if you want, you can do it directly from the terminal, like this, mysql -u, for username, ours is root, and we haven't set a password. And sure enough, I'm in. So yes, I could run the command CREATE DATABASE myapp, you know, whatever you want to call.

Connecting via TablePlus0:35

And sure enough, I'm in. So yes, I could run the command create database myapp;, you know, whatever you want to call it, end it with a semicolon, and sure enough, one row affected, which means I have created a new database. But yeah, these days, I prefer to use a GUI for things like this. So instead, I will hide the terminal and bring up TablePlus, which we looked at in the first or second episode of this series. All right, so I'm going to right-click and make a new connection. The name can be anything you want. I will call it demo.

The name can be anything you want. I will call it demo. And yeah, pay attention to the host and port here. We will stick with defaults, but later, we'll need to reference them from our php. So just remember, a host of localhost and a port of 3306. All right, next, the user was root. We don't have a password because it's local. And that should be it. So let's test it out. And I do get green, so I'm ready to connect.

Creating the Posts Table1:55

Yeah, and if we want, we can set it to always load the myapp database. And I'll save that. Okay, so now when I double-click, I'm directly within that database. All right, let's create our first table, and we will use the very common example of blog posts. Okay, so I just used the word table. At least initially, you can think of it sort of like an Excel spreadsheet. That's a common comparison, or even something like a form. We've all filled out a web form before. So imagine you're using WordPress or something, and you want to create your first blog post.

We've all filled out a web form before. So imagine you're using WordPress or something, and you want to create your first blog post. What would some of those form inputs be? Well, you might have something like, what is the title of this post? So I will call the column name title. The data type, you're going to see all sorts of types here. Trust me, you will mostly use 10% of these. But the common ones are things like varchar or varchar. That would be any number of characters up to 255. You could have text, and that would be useful for long-form text.

That would be any number of characters up to 255. You could have text, and that would be useful for long-form text. You could have timestamps. You could have decimals and floats and integers. Again, there's a lot here. You will mostly use five or six. So in this case, why don't we use varchar 255? Okay, next, is nullable? Can the title be null? Well, ask yourself, would it make sense to have a blog post with no title?

Can the title be null? Well, ask yourself, would it make sense to have a blog post with no title? Probably not. So let's set it to no. And then do we want there to be a default value? Not really. So that is our first column. But notice how everything has a background of green in TablePlus. That's because it will not persist until you save it. So I will press Command S or Control S. And now I have created a new table that for now

Understanding Primary Keys3:35

That's because it will not persist until you save it. So I will press Command S or Control S. And now I have created a new table that for now only has a title. And then we have our ID, which is the primary key. For now, think of it as a unique auto-incrementing number that represents a specific row within your table. So yeah, what we have right now is the structure of the table. But we don't actually have any data within it, as you see here. Okay, let's do it. I will double-click.

Inserting Sample Records4:04

Okay, let's do it. I will double-click. And notice the ID will auto-increment and it'll start at 1. So you can see there is a default value there. So that means I only need to worry about title. My first blog post. And again, notice how it's green. It won't persist until I save. So Command S. And notice it sets the ID to 1. If I do another one, let's say my second blog post.

So Command S. And notice it sets the ID to 1. If I do another one, let's say my second blog post. And once again, Command S. Now the ID is 2. So this auto-increments and it's a unique identifier that points or represents this specific row. All right, so think about everything we covered in this video. We created our first database called MyApp, and we connected to localhost on port 3306. Try to remember that. And then we defined our first table within it. And right now, that post table just consists of an auto-incrementing ID, the primary key,

And then we defined our first table within it. And right now, that post table just consists of an auto-incrementing ID, the primary key, and then a title. But of course, in real life, you would add things like the body of the post, maybe a status for the post. Are we in draft form? Has it been published? Has it been archived? And then later, you could have relationships to other tables. For example, it might make sense to include the person who created the post, the User.

Relational Database Concepts5:14

And then later, you could have relationships to other tables. For example, it might make sense to include the person who created the Post, the User who wrote the Post. So think about it. If I added a second table called Users, I could then draw a line from a record in the users table to a record in the post table. And that would be my way of saying, this User created that Post. And that User down here created this Post. And this is what it means to have a relational database. Okay, so you know what?

And this is what it means to have a relational database. Okay, so you know what? My original plan for this video was to also write some php to connect to the database. But on second thought, that probably needs to be its own lesson. So to wrap up, and this is also your homework, all I want you to do is play around and get comfortable. Maybe add a new column, create a new table, update a record, and you'll be good to go.

DatabasesTablesColumns

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟