// MariaDB Data Types
MariaDB Data Types
คู่มืออ้างอิง Data Types ครบถ้วน — MariaDB รองรับ MySQL-compatible types พร้อมเพิ่มเติม UUID, JSON, Temporal และ dynamic columns
// Numeric Types
ประเภทตัวเลข
| Type | Storage | Range | การใช้งาน |
|---|---|---|---|
TINYINT | 1 byte | -128 ถึง 127 | Flags, Small Status codes |
SMALLINT | 2 bytes | -32,768 ถึง 32,767 | Age, Rating, Year |
MEDIUMINT | 3 bytes | -8.4M ถึง 8.4M | Medium-size counters |
INT / INTEGER | 4 bytes | -2.1B ถึง 2.1B | Primary Keys, General Numbers |
BIGINT | 8 bytes | ±9.2 × 10¹⁸ | Large IDs, Financial Transactions |
DECIMAL(p,s) | Variable | ถึง 65 digits | การเงิน — ความแม่นยำ 100% |
FLOAT | 4 bytes | ~7 decimal digits | Approximate floating point |
DOUBLE | 8 bytes | ~15 decimal digits | High-precision floating point |
INT AUTO_INCREMENT | 4 bytes | 1 ถึง 2.1B | Auto-increment Primary Key |
💡 UNSIGNED types
MariaDB รองรับ UNSIGNED variants เช่น TINYINT UNSIGNED (0–255), INT UNSIGNED (0–4.2B) — เพิ่ม range เป็น 2 เท่าสำหรับค่า positive-only
// String Types
ประเภทข้อความ
| Type | Max Size | ความแตกต่าง | การใช้งาน |
|---|---|---|---|
CHAR(n) | 255 chars | Fixed-length, Pad spaces | Country codes, Status flags |
VARCHAR(n) | 65,535 bytes | Variable-length | Names, Emails, URLs |
TINYTEXT | 255 bytes | Tiny text column | Short notes |
TEXT | 65,535 bytes | Standard text | Articles, Descriptions |
MEDIUMTEXT | 16 MB | Medium-sized text | Blog posts, Long content |
LONGTEXT | 4 GB | Very large text | Books, Large documents |
ENUM | 65,535 values | Predefined list | Status, Category — efficient storage |
SET | 64 members | Multiple values from list | Permissions, 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
วันที่และเวลา
| Type | Storage | Range |
|---|---|---|
DATE | 3 bytes | 1000-01-01 ถึง 9999-12-31 |
TIME | 3 bytes | -838:59:59 ถึง 838:59:59 |
DATETIME | 5 bytes | 1000-01-01 ถึง 9999-12-31 |
TIMESTAMP | 4 bytes | 1970–2038 (Unix range) |
YEAR | 1 byte | 1901 ถึง 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
| Type | Max Size | การใช้งาน |
|---|---|---|
BINARY(n) | 255 bytes | Fixed-length binary, checksums |
VARBINARY(n) | 65,535 bytes | Variable binary data |
TINYBLOB | 255 bytes | Small binary objects |
BLOB | 65 KB | Images, small files |
MEDIUMBLOB | 16 MB | Medium files, audio |
LONGBLOB | 4 GB | Large files, video |
UUID (10.7+) | 16 bytes | Native 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+