How to create a database MySQL

What is MySQL

MySQL is a relational database management system, Database is organized data into more than one table. MySQL is a database system used for web application and its run on the server. The MySQL database is reliable, fast, easy to use. Mysql is a programming language used to create, insert, update, and extract data from the relational database (RDMS).

MySQL Create Database, Table, and Data Types

In this tutorial, we will learn how to create a database Mysql.
There are multiple ways to create a database MySQL but in this tutorial, we will learn the below 3 ways.

  1. By executing a SQL script/Query
  2. By using Command line
  3. By using the phpMyAdmin user interface

Table of Contents

  1. Create database MySQL
  2. Create Table
  3. Data Types
  4. Removing Databases (DROP DATABASE [IF EXISTS])

1. Create database MySQL

We need to write the CREATE DATABASE command to create a database MySQL. CREATE DATABASE is a SQL command which is used to create a database.


Now we will create a database named “testdemo” by executing the below SQL command line.

CREATE DATABASE is a SQL command

CREATE DATABASE testdemo;

Or you can also create a database by using the phpMyAdmin below the user interface. In this interface, just you need to simply enter the database name in the text field and submit. After submitting your database has been created in MySQL.

CREATE DATABASE MYSQL

IF NOT EXISTS

The IF NOT EXISTS is an optional clause of the SQL statement. The IF NOT EXISTS clause statement prevents to check the existence of a database by making a new database that already exists in the database server. You can’t create 2 databases with identical names in a MySQL database server.

CREATE DATABASE IF NOT EXISTS testdemo;

SHOW DATABASES

SHOW DATABASES statement will show the list of all the databases in the MySQL database server.
You can check the database that you are created on the MySQL server by executing the SHOW DATABASES statement on the MySQL database server.

SHOW DATABASES statement

DROP DATABASE (Removing Databases) [IF EXISTS]

If we want to delete the DATABASE so we need to execute the following command on the database server. The IF EXISTS is an optional clause of the statement prevents to check a database before removing a database that does not exist in the database server.

DROP DATABASE [IF EXISTS] testdemo

Collation

A collation is a set of rules that defines examine and sort character strings. Every collation in MySQL belongs to a single character set. Each character set has a minimum of one collation, and most have two or more collations.

SHOW CHARACTER SET

Default Collations of the character set as follows

CharsetDescriptionDefault collationMaxlen
big5Big5 Traditional Chinesebig5_chinese_ci2
dec8DEC West Europeandec8_swedish_ci1
cp850DOS West Europeancp850_general_ci1
hp8HP West Europeanhp8_english_ci1
koi8rKOI8-R Relcom Russiankoi8r_general_ci1
latin1cp1252 West Europeanlatin1_swedish_ci1
latin2ISO 8859-2 Central Europeanlatin2_general_ci1
swe77bit Swedishswe7_swedish_ci1
asciiUS ASCIIascii_general_ci1
ujisEUC-JP Japaneseujis_japanese_ci3
sjisShift-JIS Japanesesjis_japanese_ci2
hebrewISO 8859-8 Hebrewhebrew_general_ci1
tis620TIS620 Thaitis620_thai_ci1
euckrEUC-KR Koreaneuckr_korean_ci2
koi8uKOI8-U Ukrainiankoi8u_general_ci1
gb2312GB2312 Simplified Chinesegb2312_chinese_ci2
greekISO 8859-7 Greekgreek_general_ci1
cp1250Windows Central Europeancp1250_general_ci1
gbkGBK Simplified Chinesegbk_chinese_ci2
latin5ISO 8859-9 Turkishlatin5_turkish_ci1
armscii8ARMSCII-8 Armenianarmscii8_general_ci1
utf8UTF-8 Unicodeutf8_general_ci3
ucs2UCS-2 Unicodeucs2_general_ci2
cp866DOS Russiancp866_general_ci1
keybcs2DOS Kamenicky Czech-Slovakkeybcs2_general_ci1
macceMac Central Europeanmacce_general_ci1
macromanMac West Europeanmacroman_general_ci1
cp852DOS Central Europeancp852_general_ci1
latin7ISO 8859-13 Balticlatin7_general_ci1
utf8mb4UTF-8 Unicodeutf8mb4_general_ci4
cp1251Windows Cyrilliccp1251_general_ci1
utf16UTF-16 Unicodeutf16_general_ci4
utf16leUTF-16LE Unicodeutf16le_general_ci4
cp1256Windows Arabiccp1256_general_ci1
cp1257Windows Balticcp1257_general_ci1
utf32UTF-32 Unicodeutf32_general_ci4
binaryBinary pseudo charsetbinary1
geostd8GEOSTD8 Georgiangeostd8_general_ci1
cp932SJIS for Windows Japanesecp932_japanese_ci2
eucjpmsUJIS for Windows Japaneseeucjpms_japanese_ci3
gb18030China National Standard GB18030gb18030_chinese_ci4

2. Create Table

Create table statement is used to create a table in the MySQL database.

Following the below Create table statement.

DROP TABLE IF EXISTS `testrecords`;
CREATE TABLE IF NOT EXISTS `testrecords` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fullName` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `mobile` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

We can also create a table by using the PHPMYADMIN interface. The interface looks like.

Create table statement

The following way to create a table using PHP PDO.

<?php
class Database
{
     
    private $host = "localhost";
    private $db_name = "testdemo";
    private $username = "root";
    private $password = "";
    public $conn;
     
    public function dbConnection()
	{
     
	    $this->conn = null;    
        try
		{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
			$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);	
			
// Mysql create table
  $sql = "CREATE TABLE IF NOT EXISTS `testrecords` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fullName` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `mobile` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
)";
$conn->exec($sql);
echo "Table has been created successfully";
        }
		catch(PDOException $exception)
		{
            echo "Connection error: " . $exception->getMessage();
        }
         
        return $this->conn;
    }
}
?>

3. Data Types

MySQL uses many different data types its categories into 5 categories.

MySQL data types
  1. Numeric data types
  2. Date and Time
  3. String data types
  4. Spatial data types
  5. JSON

Numeric data types

The list of the following numeric data types below.

  • INT 
  • TINYINT 
  • SMALLINT 
  • MEDIUMINT 
  • BIGINT 
  • FLOAT(M,D)
  • DOUBLE(M,D)
  • DECIMAL(M,D)

Date and Time

  1. DATE
  2. DATETIME
  3. TIMESTAMP
  4. TIME
  5. YEAR

String data types

  • CHAR
  • VARCHAR
  • BLOB or TEXT
  • TINYBLOB or TINYTEXT
  • MEDIUMBLOB or MEDIUMTEXT
  • LONGBLOB or LONGTEXT
  • ENUM 

Spatial data types

  • GEOMETRY
  • POINTS
  • LINESTRING
  • POLYGON
  • MULTIPOINT
  • MULTILINESTRING
  • MULTIPOLIGON
  • GEOMETRY COLLECTION
MYSQL table with data types