MySQL 8.0.22 | How to create the database and table

Student Kim
Buzz Code
Published in
3 min readNov 10, 2020

What is the ‘Database’ and ‘ Table’?

‘Database’ is the place where you save the information, and the ‘Table’ is where you can actually organize the information.
According to my teacher, you can think of database as your room and the table is like your wardrobe. if you try to put all your cloths in the room without organizing, the room would get so messy, that’s the when you need a wardrobe, so you can organize the cloths by its type or use. In MySQL, cloths will be the information!

1. Create and show database

First, let’s see what kind of database you already have. When you type
show databases; in MySQL, you will see this.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| database1 |
| database2 |
| database3 |
+--------------------+
3 rows in set (0.01 sec)

means, I already have 3 databases(database1, database2, database3) here.

Now let’s create a new database. Let’s name our database as ‘kim’. This time, you type create database kim;

mysql> create database kim;
Query OK, 1 row affected (0.01 sec)

Then again, type show databases;

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| database1 |
| database2 |
| database3 |
| kim |
+--------------------+
4 rows in set (0.00 sec)

Now we have our new database ‘kim’ in your databases.

2. Create Table & desc

I am going to make a table in the database named kim. So we have to type
use databaseName; and to see if there’s any column type show tables;

mysql> use kim;
Database changed
mysql> show tables;
Empty set (0.01 sec)

There’s nothing so you see empty.

So let’s create one. I am going to put id, name, phone, age in my table, and name my table ‘student’.

mysql> create table student(
-> id varchar(8),
-> name varchar(10),
-> phone varchar(13),
-> age int
-> );
Query OK, 0 rows affected (0.04 sec)

Next to the columns you’re seeing ‘varchar’ and ‘int’.
Varchar means the data type of its column is letters. And the numbers in the next to the varchar means it’s maximum length. So there’s id varchar(8) , It means if you put more than 8 letters in the id, there will be an error.
Int means the data type of its column is number. But as you can see phone column is also set with varchar. Isn’t the phone ‘number’ is number?
Why? It’s easy if you think int as the numbers that you can calculate. We are definitely not gonna subtract the phone numbers, so that’s why we use varchar here.

To see the result of what we’ve done, put desc databaseName.tableName;

mysql> desc kim.student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | varchar(8) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| phone | varchar(13) | YES | | NULL | |
| age | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

There, we made these database and table very simply.

I posted ‘how to insert or select data and the where clause’, check out through the following link.

--

--