What are the different data types in MySQL

MySQL supports a variety of data types that can be used to define the types of data a table column can hold. These data types are categorized into several groups:

Numeric Data Types

This category includes types that store numbers, both integers and decimals. Common numeric types are:

  • TINYINT: A very small integer.
  • SMALLINT: A small integer.
  • MEDIUMINT: A medium-sized integer.
  • INT: A standard integer.
  • BIGINT: A large integer.
  • FLOAT: A floating-point number.
  • DOUBLE: A double-precision floating-point number.
  • DECIMAL: A fixed-point number.

Date and Time Data Types

These types store date and time values:

  • DATE: A date.
  • TIME: A time.
  • DATETIME: A combination of date and time.
  • TIMESTAMP: A timestamp value.
  • YEAR: A year.

String Data Types

This category consists of types that store text values:

  • CHAR: A fixed-length string.
  • VARCHAR: A variable-length string.
  • TINYTEXT: A very small text string.
  • TEXT: A text string.
  • MEDIUMTEXT: A medium-sized text string.
  • LONGTEXT: A large text string.
  • BINARY: A fixed-length binary string.
  • VARBINARY: A variable-length binary string.
  • TINYBLOB: A very small binary object.
  • BLOB: A binary object.
  • MEDIUMBLOB: A medium-sized binary object.
  • LONGBLOB: A large binary object.

Example

Here's a PHP example of creating a MySQL table with various data types:

<?php // Create connection $conn = new mysqli("localhost", "username", "password", "database"); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // sql to create table $sql = "CREATE TABLE Users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL, password VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table Users created successfully"; } else { echo "Error creating table: " . $conn->error; } $conn->close(); ?>

MySQL data types numeric data types string data types date and time data types