SQL data types define the kind of data a column can store. They ensure:
๐ Data integrity
โ๏ธ Efficient storage
๐ง Correct query processing
๐งฎ Numeric Data Types
๐ข INT, SMALLINT, BIGINT โ For whole numbers
๐ฃ DECIMAL(p, s), NUMERIC(p, s) โ For precise decimal values (used in money, percentages)
๐ง FLOAT, REAL, DOUBLE โ For approximate decimal numbers (can lose precision)
๐ Character/String Data Types
Variable-Length Types
๐งถ VARCHAR(n) โ Stores up to n characters
๐งฐ Allocates only the space needed for actual data
๐ก Efficient for values of varying lengths
๐ Example:
name VARCHAR(50)Stores "Swastika" using just 8 characters, not the full 50
Fixed-Length Types
๐งฐ CHAR(n) โ Always uses exactly n characters
๐งฑ Pads with spaces if the value is shorter
๐ Example:
code CHAR(4)Stores "AB" as "AB " (with 2 spaces)
โ๏ธ Variable vs Fixed: Key Differences
Feature |
|
|
|---|---|---|
Length Usage | Stores only used characters | Always uses full length |
Storage | Space-efficient | May waste space |
Performance | Slightly slower on updates | Faster for fixed-length |
Use Case | Names, emails, descriptions | PINs, codes, fixed-size IDs |
๐ Date and Time Data Types
๐ DATE โ Stores date (YYYY-MM-DD)
โฐ TIME โ Stores time (HH:MM:SS)
๐ DATETIME โ Stores both date and time
๐ฏ๏ธ TIMESTAMP โ Auto-updated with current date & time (useful for logs)
๐งฑ Other Data Types
๐ฆ BLOB โ Binary Large Object (images, files)
๐ TEXT โ Long-form text
๐ข BOOLEAN โ Stores TRUE / FALSE (some databases use TINYINT)
โ Summary
๐งต Use VARCHAR for fields with variable lengths (e.g., names, emails)
๐ Use CHAR for fields with fixed lengths (e.g., country codes, OTPs)
๐พ Choosing the right type improves performance, storage efficiency, and data accuracy