MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
MySQL supports a variety of data types in several categories: numeric types, date and time types, string (character and byte) types, and spatial types. Here's an overview of the most commonly used ones:
Numeric Types: Numeric types are used to store numeric data.
INT: A standard integer. The range for signed integers is -2147483648 to 2147483647, and for unsigned integers, it is 0 to 4294967295.
TINYINT: A small integer. The range for signed integers is -128 to 127, and for unsigned integers, it is 0 to 255.
SMALLINT: A small integer. The range for signed integers is -32768 to 32767, and for unsigned integers, it is 0 to 65535.
MEDIUMINT: A medium-sized integer. The range for signed integers is -8388608 to 8388607, and for unsigned integers, it is 0 to 16777215.
BIGINT: A large integer. The range for signed integers is -9223372036854775808 to 9223372036854775807, and for unsigned integers, it is 0 to 18446744073709551615.
FLOAT and DOUBLE: For floating-point numbers.
DECIMAL: For precise fixed-point numbers, often used for financial data.
Date and Time Types: These types are used to store date and time.
DATE: For date values in 'YYYY-MM-DD' format.
TIME: For time values in 'hh:mm:ss' format.
DATETIME: For combined date and time values in 'YYYY-MM-DD hh:mm:ss' format.
TIMESTAMP: Similar to DATETIME, but with automatic updating for record creation and modification.
YEAR: For year values.
String Types: String types are used to store string data.
CHAR: Fixed-length character data, up to 255 characters.
VARCHAR: Variable-length character data, up to 65535 characters.
TEXT: For long text data.
BINARY and VARBINARY: For binary string data.
BLOB: For binary large object data, such as images or other binary files.
ENUM: For columns that have a known, fixed set of values.
Spatial Types: Spatial types are used to store geometric and geographical information.
GEOMETRY: A type that can store any type of spatial data.
POINT: A point in a 2D space.
LINESTRING: A curve in a 2D space.
POLYGON: A polygon in a 2D space.
Each of these types has its own strengths and weaknesses, and is best suited to certain types of data. Choosing the right data type for your data is important to ensure the functionality and performance of your database.
MySQL Numeric Data Types:
-- Example using numeric types CREATE TABLE numeric_table ( int_col INT, float_col FLOAT, double_col DOUBLE, decimal_col DECIMAL(10, 2) );
MySQL String Data Types:
-- Example using string types CREATE TABLE string_table ( varchar_col VARCHAR(255), char_col CHAR(10), text_col TEXT );
MySQL Data Type Conversion:
CAST
or CONVERT
.-- Example of explicit conversion SELECT CAST(int_col AS VARCHAR) FROM numeric_table;
MySQL Data Types for Indexing:
-- Example of indexing CREATE INDEX idx_numeric_col ON numeric_table(int_col);
MySQL Spatial Data Types:
-- Example using spatial types CREATE TABLE spatial_table ( location POINT );
MySQL JSON Data Type Usage:
-- Example using JSON type CREATE TABLE json_table ( data JSON );
MySQL ENUM Data Type Examples:
-- Example using ENUM CREATE TABLE enum_table ( status ENUM('active', 'inactive', 'pending') );
MySQL SET Data Type in Queries:
-- Example query with SET SELECT * FROM set_table WHERE 'value' IN (col_set);
MySQL Data Types for Internationalization:
-- Example with character set and collation CREATE TABLE i18n_table ( text_col VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci );