7. SQL Data Types

Every column in a SQL table must have a data type defined. Data types are essential because they tell the database exactly what kind of information to expect (numbers, text, dates, etc.), how much space to reserve, and what operations (like arithmetic or sorting) are valid. Choosing the correct data type is critical for storage efficiency, data integrity, and query performance.

Here is a breakdown of the most common and important SQL data type categories:


1.  Numeric Types

Numeric types are used for storing different kinds of numbers, distinguishing between integers and decimals.

TypeDescriptionCommon Use Cases
INT / INTEGERA standard whole number (no decimals). Most systems offer variations like SMALLINT or BIGINT for different ranges.Primary Keys, counts, quantities.
DECIMAL / NUMERICStores exact fractional (decimal) numbers. You specify the precision and scale (e.g., DECIMAL(8, 2) for 8 total digits, 2 after the decimal).Currency, fixed-point arithmetic, financial calculations.
FLOAT / REALStores approximate, floating-point numbers. Less precise than DECIMAL due to how computers store them.Scientific measurements, non-financial ratios.

2. String (Text) Types

These types store characters, letters, and numbers. The choice here often comes down to storage efficiency, as discussed previously (CHAR vs. VARCHAR).

TypeDescriptionCommon Use Cases
VARCHAR(n)Variable-length string. Only occupies the space necessary for the data plus a small overhead. n defines the maximum length.Names, addresses, most text descriptions.
CHAR(n)Fixed-length string. Always occupies n characters of storage space, padding with spaces if the string is shorter.Fixed-length codes (e.g., state abbreviations, 'US', 'CA').
TEXTUsed for very large strings of text, like full articles or long notes. Doesn't require a defined maximum length.Blog content, comments, long descriptions.

3. Date and Time Types

These types ensure that time-based data is stored in a consistent, standardized format, making temporal queries (e.g., "sales last month") simple.

TypeDescriptionCommon Use Cases
DATEStores the date only (year, month, day).Birth dates, invoice dates, holidays.
TIMEStores the time only (hour, minute, second).Opening hours, event start times.
DATETIME / TIMESTAMPStores both the date and the time, often down to milliseconds. TIMESTAMP often tracks time zone or is relative to an epoch.Record creation/update times, transaction timestamps.

4. 💡 Boolean and Specialized Types

These types handle specific data needs beyond standard numbers or text.

TypeDescriptionCommon Use Cases
BOOLEAN / BOOLStores simple true/false values. Some databases use TINYINT (0 or 1) instead of a native boolean type.Flags (e.g., IsActive, IsPaid).
JSON / JSONBUsed to store data in the popular JavaScript Object Notation (JSON) format, allowing for flexible, semi-structured data within a relational table.Storing configuration settings, API responses, or complex nested user preferences.
ENUMAllows a column to hold one and only one value from a predefined list of allowed string values (e.g., 'Small', 'Medium', 'Large').Status codes, severity levels.



Comments

Popular Posts