13.09.2023

How to create tables in MySQL?

Introduction

The modern DBMS system can be controlled by GUI and CLI of MySql Client, but what is the difference?

Command Line Interface (CLI) control of MySQL offers several advantages and is essential for various reasons:

In summary, CLI control of MySQL is indispensable for its efficiency, scripting capabilities, resource efficiency, and flexibility. It is a valuable tool for both beginners and experienced database administrators, offering precise control over database operations and facilitating automation and customization of tasks. Therefore learning of administrate and control DBMS we will see on CLI, let's check!

Creation user

Taking a closer look at MySQL, assuming it's installed as database software with administrative privileges, our first step involves gaining access to the MySQL system, and we can do this by executing the following command:

mysql -u root -p -h localhost

Screenshot №1 — Login

Let's have look at the currently set of databases by the typing command below:

SHOW DATABASES;

Screenshot №2 — List of databases

Create database, for example with product from grocery online-store, by entering command:

CREATE DATABASE product;

Screenshot №3 — Create DB

In the MySQL there is state of using database by default, all enter command will apply to the active DB, therefore we need to set such base:

USE product

Screenshot №4 — USE command

In that case unnecessary to use semicolon at the end of  the entering command, let's check active database:

SELECT DATABASE();

Screenshot №5 — Active DB

Now we can create tables with the same name of our category of product, but at the first we need to check them:

SHOW TABLES;

Then type next command:

CREATE TABLE product (name VARCHAR(20), tag VARCHAR(20), id INT NOT NULL, expiration DATE, production DATE);

Let's explain syntax of creation tables for MySQL:

Screenshot №6 — Creation of table

If you will use command again:

SHOW TABLES

Then you can see created table for our database, let's check our parameters and configuration:

DESCRIBE product

Screenshot №7 — Table of data

By the default settings all types of data have value NULL, that mean nothing in cell. But we can change it by parameters, that we described above.

Conclusion

In this instructional journey, we delved into CLI control of MySQL by exploring various commands and actions, from accessing the MySQL system to creating databases and tables. By mastering the command-line interface, users gain a deeper understanding of how to administer and control DBMS effectively.