Database Management System (DBMS) / SQL(STRUCTURED QUERY LANGUAGE)

SQL Datatypes (Variable Datatypes vs Fixed Datatypes)

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

VARCHAR(n) (Variable)

CHAR(n) (Fixed)

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