// MariaDB Data Types

MariaDB Data Types

คู่มืออ้างอิง Data Types ครบถ้วน — MariaDB รองรับ MySQL-compatible types พร้อมเพิ่มเติม UUID, JSON, Temporal และ dynamic columns

// Numeric Types

ประเภทตัวเลข

TypeStorageRangeการใช้งาน
TINYINT1 byte-128 ถึง 127Flags, Small Status codes
SMALLINT2 bytes-32,768 ถึง 32,767Age, Rating, Year
MEDIUMINT3 bytes-8.4M ถึง 8.4MMedium-size counters
INT / INTEGER4 bytes-2.1B ถึง 2.1BPrimary Keys, General Numbers
BIGINT8 bytes±9.2 × 10¹⁸Large IDs, Financial Transactions
DECIMAL(p,s)Variableถึง 65 digitsการเงิน — ความแม่นยำ 100%
FLOAT4 bytes~7 decimal digitsApproximate floating point
DOUBLE8 bytes~15 decimal digitsHigh-precision floating point
INT AUTO_INCREMENT4 bytes1 ถึง 2.1BAuto-increment Primary Key

💡 UNSIGNED types

MariaDB รองรับ UNSIGNED variants เช่น TINYINT UNSIGNED (0–255), INT UNSIGNED (0–4.2B) — เพิ่ม range เป็น 2 เท่าสำหรับค่า positive-only

// String Types

ประเภทข้อความ

TypeMax Sizeความแตกต่างการใช้งาน
CHAR(n)255 charsFixed-length, Pad spacesCountry codes, Status flags
VARCHAR(n)65,535 bytesVariable-lengthNames, Emails, URLs
TINYTEXT255 bytesTiny text columnShort notes
TEXT65,535 bytesStandard textArticles, Descriptions
MEDIUMTEXT16 MBMedium-sized textBlog posts, Long content
LONGTEXT4 GBVery large textBooks, Large documents
ENUM65,535 valuesPredefined listStatus, Category — efficient storage
SET64 membersMultiple values from listPermissions, Tags (bitmask)
-- ENUM และ SET ใช้ storage น้อยมาก CREATE TABLE orders ( id INT AUTO_INCREMENT PRIMARY KEY, status ENUM('pending', 'paid', 'shipped', 'cancelled'), perms SET('read', 'write', 'delete', 'admin') );
// Date & Time Types

วันที่และเวลา

TypeStorageRange
DATE3 bytes1000-01-01 ถึง 9999-12-31
TIME3 bytes-838:59:59 ถึง 838:59:59
DATETIME5 bytes1000-01-01 ถึง 9999-12-31
TIMESTAMP4 bytes1970–2038 (Unix range)
YEAR1 byte1901 ถึง 2155

⏰ Temporal Tables (ฟีเจอร์พิเศษ)

MariaDB รองรับ System-Versioned Tables ตาม SQL:2011 — เก็บประวัติทุก row อัตโนมัติ query ด้วย FOR SYSTEM_TIME AS OF

CREATE TABLE prices ( item VARCHAR(100), price DECIMAL(10,2) ) WITH SYSTEM VERSIONING; -- ดูข้อมูล ณ วันที่ผ่านมา SELECT * FROM prices FOR SYSTEM_TIME AS OF '2024-01-01';
// JSON Type

JSON — MariaDB 10.2+

📌 MariaDB JSON vs PostgreSQL JSONB

MariaDB JSON เก็บเป็น LONGTEXT แต่มี JSON validation และ JSON functions ครบถ้วน — ใช้ path expressions แบบ $.key ต่างจาก PostgreSQL ที่ใช้ ->

-- สร้าง Table ด้วย JSON CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), attrs JSON ); -- Insert JSON INSERT INTO products (name, attrs) VALUES ('T-Shirt', JSON_OBJECT('color', 'blue', 'size', 'M')); -- Query JSON path SELECT JSON_VALUE(attrs, '$.color') AS color FROM products; -- JSON_TABLE (MariaDB 10.6+) SELECT jt.* FROM products, JSON_TABLE(attrs, '$' COLUMNS( color VARCHAR(50) PATH '$.color', size VARCHAR(10) PATH '$.size' )) jt;
// Binary Types

Binary & BLOB Types

TypeMax Sizeการใช้งาน
BINARY(n)255 bytesFixed-length binary, checksums
VARBINARY(n)65,535 bytesVariable binary data
TINYBLOB255 bytesSmall binary objects
BLOB65 KBImages, small files
MEDIUMBLOB16 MBMedium files, audio
LONGBLOB4 GBLarge files, video
UUID (10.7+)16 bytesNative UUID type — stored efficiently

🆕 Native UUID Type (MariaDB 10.7+)

MariaDB 10.7 เพิ่ม native UUID data type ที่เก็บเป็น 16 bytes (แทน 36-byte string) — มี UUID() function และรองรับ UUID v4/v7 ใน MariaDB 11.4+