This is the first of our Learn MySQL Tutorial. In this post we shall learn how to create and use Databases and tables in MySQL using MySQL terminal. In case you don’t know about running MySQL terminal, check this post to learn.
Databases are main objects in MySQL. Every data is stored inside Databases. But it can not be stored directly inside it! Tables are used to store Related Data inside a Database. So basically, whenever we wish to store some data, then we need to create a Database and then a Table, and split that data into parts and store inside separate columns of the Table. A MySQL server can contain infinite amount of Databases, and a database can contain as many Tables you want.
Let us see, how to create the Database and tables…
Analyzing the Data we want to store:
In this tutorial, we are assuming that you want to store the following data:
- First name, Last name, email and mobile of every client you have got through your website.
For this we are going to use one table, client_info to store our data.
Creating the DataBase:
We are going to name the database as “my_clients”. Now follow the steps to CREATE and USE it:
- Launch your MySQL terminal;
- Now type the following command and hit enter…
CREATE DATABASE my_clients;
- You have now successfully created the database. So, the basic command of creating a Database is:
CREATE DATABASE db_name;
- Now to use the database for the successive operation, we need to execute the following command:
USE my_clients;
- Basically to use any database you just need to type
USE db_name;
Note that each MySQL command must end with a semi-colon(;) and then you need to type enter to execute it.
Creating the Table:
As said before, we need to create two tables for now. Also we need to decide the column names for each of the tables. Basically each row in a MySQL table stores a unique set of data. consider the following table:
first_name | last_name | phone_num | |
Name1 | Surname1 | email1 | phone number1 |
Name2 | Surname2 | email2 | phone number2 |
MySQL databases contains these types of table where each row contains a specific data, separated in different types by columns. Now we are to create this table inside the database we have made…
- Make sure you are using the database you have just created.
- Now just enter the following command
CREATE TABLE client_info
(
first_name VARCHAR(60),
last_name VARCHAR(60),
email VARCHAR(60),
phone_num INT
)
; - Here we have used multiple lines to enter a single command. You need to enter it like this in your MySQL terminal.
- So, the basic command for creating a Table is:
CREATE TABLE client_info
(
column_name1 column_type1,
column_name2 column_type2,
… …,
column_nameN column_typeN
)
;
DATATYPES:
Note we have used some parameters like VARCHAR, INT etc. These are datatypes. We need to define the types of data in every column. VARCHAR(character_length) means a string of variable character having character range in between 0 and character_length. Obviously INT means integer only. You should now be getting why we have used VARCHAR(60) in first_name, last_name etc and INT in phone_num. More on that coming on our next tutorial. Also we shall see how to insert data inside a MySQL table on our next post.
So that was the first basic tutorial. I hope it was easy for… More complex stuffs coming on this series very soon… Read each of them to learn the basics and advance usage of MySQL. Then we shall see how to use them using PHP.
Hi swashata, would you please tell me how to create a admin page that it can store a text like news with date,author,.. and show it for visitors in onother page like news.htm, Thanks.
Hi,
this awesome!!!!!!! really great article, thanks for sharing with us.
here is also a nice one article on how to create database and table in MySQL step by step, you may check it by following link….
http://www.mindstick.com/Articles/6e284d44-26e1-4caa-bf6e-b396c67cf6f1/?Creating%20Database%20and%20Table%E2%80%99s%20in%20MySQL
this was very helpfull to me . and i bookmarked your site. thank you for help. good work.
I think this is one of the most vital information for me. And i’m happy studying your article. But wanna statement on few common things, The website style is great, the articles is in reality excellent : D. Just right task, cheers
Pingback: MySQL FLOAT vs DEC - DECIMAL & FLOAT datatype of MySQL & Difference
How do we create fixed size table?
eg – my table should have just 50 entries and work in the format of FIFO