0% found this document useful (0 votes)
19 views2 pages

MySQL Data Types Overview Cheat Sheet

This cheat sheet provides an overview of commonly used MySQL data types, including numeric, string, date & time, and special types, along with examples. It highlights the appropriate use cases for each type, such as using INT for IDs, DECIMAL for currency, and VARCHAR for text fields. Additionally, it includes a sample Employee table structure demonstrating these data types in practice.

Uploaded by

akds.singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

MySQL Data Types Overview Cheat Sheet

This cheat sheet provides an overview of commonly used MySQL data types, including numeric, string, date & time, and special types, along with examples. It highlights the appropriate use cases for each type, such as using INT for IDs, DECIMAL for currency, and VARCHAR for text fields. Additionally, it includes a sample Employee table structure demonstrating these data types in practice.

Uploaded by

akds.singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

📘 MySQL Data Types Cheat Sheet

This cheat sheet summarizes the most commonly used MySQL data types, with descriptions
and examples.

🔢 Numeric Types

 INT / INTEGER → Whole numbers (e.g., Age INT → 15, 20)

 TINYINT → Very small numbers (-128 to 127), often used for flags (0 = No, 1 = Yes)

 BIGINT → Very large integers (e.g., population, bank balances)

 DECIMAL(M,D) → Exact fixed-point numbers (best for money).


Example: DECIMAL(8,2) → 12345.67 (8 total digits, 2 after decimal)

 FLOAT / DOUBLE → Approximate floating-point numbers (best for scientific


calculations, not money).

🔤 String Types

 CHAR(n) → Fixed-length string (always n chars, padded with spaces).

 VARCHAR(n) → Variable-length string (most common for names, text).

 TEXT → Large text field (up to 65,535 characters).

 ENUM → One value from a fixed list.


Example: ENUM('Male','Female','Other')

 SET → Multiple values from a fixed list.


Example: SET('Maths','Science','English')

📅 Date & Time Types

 DATE → YYYY-MM-DD → Example: 2025-08-21

 TIME → HH:MM:SS → Example: 14:35:00

 DATETIME → YYYY-MM-DD HH:MM:SS → Example: 2025-08-21 14:35:00

 TIMESTAMP → Like DATETIME, but auto-updates with current time.


🎯 Special Types

 BOOLEAN / BOOL → Stored as TINYINT(1) (0 = False, 1 = True).

 BLOB → Binary Large Object (e.g., images, files, videos).

✅ Example: Employee Table

CREATE TABLE Employees (

EmpID INT AUTO_INCREMENT PRIMARY KEY,

FirstName VARCHAR(50),

LastName VARCHAR(50),

Salary DECIMAL(10,2),

Gender ENUM('Male','Female','Other'),

HireDate DATE,

IsActive BOOLEAN

);

📌 Teaching Tips

 Use INT for IDs and counts.

 Use DECIMAL for currency (exact values).

 Use VARCHAR for most text fields.

 Use DATE / DATETIME for temporal data.

 Use ENUM for predefined choices.

 Use BLOB only when you must store large files in the DB.

Common questions

Powered by AI

BLOB is suitable for storing images directly in the database, ensuring data integrity and simplifying transactional management. However, it can significantly increase database size, impacting performance. Using a file system to store images with path references in the database can improve retrieval speed and reduce the database load but complicates data management and backup processes .

Using TEXT is beneficial for very large text fields exceeding 65,535 characters, such as blog posts or articles, but it lacks indexing capabilities which can affect retrieval speed compared to VARCHAR(n). VARCHAR is more efficient for shorter text due to better indexing support and defined length limits, aiding in query optimization and memory usage .

BIGINT is necessary for applications requiring storage of very large integers, such as national populations or substantial financial figures exceeding the range of standard INT. However, using BIGINT can increase storage space requirements and impact performance due to the larger data size, thus requiring careful consideration of needs versus resource use .

Using ENUM is beneficial for predefined categorical values as it restricts entries to specified options, reducing entry errors and storage size since ENUMs are internally represented by integer indexes. VARCHAR, however, is more flexible by allowing any string input, which might lead to inconsistent data entry if not controlled programmatically .

TIMESTAMP's auto-update feature is useful in event logging because it automatically records the current server time whenever a row is modified. This ensures the logs have the most recent timestamp by default, minimizing manual intervention for time updates and improving accuracy and reliability over using DATETIME, which requires explicit setting .

DECIMAL(M,D) stores numbers as fixed-point decimals, capturing exact values which is crucial for financial data where precision in cents is important, unlike integers with implied decimals that might introduce errors through manual scaling and recalculations, leading to rounding or truncation issues .

Choosing DECIMAL over FLOAT/DOUBLE for financial calculations ensures higher precision and accuracy since DECIMAL is a fixed-point number, representing exact numeric data values, whereas FLOAT/DOUBLE are floating-point and can introduce rounding errors. This is crucial for financial transactions where exact values are necessary .

The choice should depend on whether time-of-day details are relevant. DATE suffices when only a day-level granularity is needed, saving storage space. DATETIME should be used when exact timestamps are necessary, such as event scheduling or precise time records, ensuring comprehensive temporal data capture .

BOOLEAN is stored as TINYINT(1), where 0 represents false and 1 represents true, making them functionally identical in storage. The trade-offs are primarily semantic; BOOLEAN improves code readability and makes the purpose of the field clear (binary state), while TINYINT offers more explicit numeric flexibility, potentially useful if extending beyond binary states in the future .

Using CHAR(n) is advantageous when the string length is always fixed, as it provides consistent performance for storage and retrieval due to its fixed memory allocation. This can be useful for fields like country codes or ID numbers, where the length is constant, and avoids performance overhead related to the dynamic nature of VARCHAR .

You might also like