Different Data Types Used in Relational Databases
String Data Types
- CHAR(size)
- Description: Fixed-length string.
- Example: CHAR(6) for storing a fixed-length ID like P@ssWd.
- VARCHAR(size)
- Description: Variable-length string.
- Example: VARCHAR(255) for names or email addresses.
- TEXT
- Description: Large text field, up to 65,535 characters.
- Example: Storing product descriptions.
- ENUM
- Description: String field with predefined values.
- Example: ENUM('Male', 'Female', 'Other') for gender.
- SET
- Description: String field that can hold multiple values from a predefined list.
- Example: SET('Reading', 'Traveling', 'Sports') for hobbies.
String data types are ideal for storing textual information, but they cannot be used for numerical calculations or date comparisons.
Numeric Data Types
- INT
- Description: Whole numbers.
- Example: INT for storing quantities or IDs.
- FLOAT(p)
- Description: Floating-point numbers.
- Example: FLOAT(7,2) for storing prices like 19.99.
- BOOL
- Description: Boolean values (0 for false, 1 for true).
- Example: BOOL for flags like isActive.
- BIT (SIZE)
- Description: Stores binary values from 1 - 64 bits in size
- Example: BIT (4) to store the number 3: 0011
Numeric data types are essential for calculations, but using them for non-numeric data (e.g., phone numbers) can lead to errors.
Date/Time Data Types
- DATE
- Description: Stores dates in YYYY-MM-DD format.
- Example: DATE for birthdates.
- DATETIME
- Description: Stores date and time in YYYY-MM-DD hh:mm:ss format.
- Example: DATETIME for recording transaction timestamps.
- TIMESTAMP
- Description: Automatically stores the current date and time.
- Example: TIMESTAMP for tracking record updates.
Date/time data types enable chronological sorting and date-based calculations, which are impossible with string representations of dates.
Importance of Data Type Consistency
- Data Integrity
- Ensures that data is stored and processed correctly.
- Example: Storing a date as a STRING prevents date-based calculations.
- Performance Optimization
- Efficient data storage and retrieval.
- Example: Using INT instead of VARCHAR for IDs reduces storage space and improves query performance.
- Validation and Constraints
- Enforces rules on data entry.
- Example: ENUM restricts values to a predefined list, preventing invalid entries.
Always choose the most appropriate data type for each attribute to ensure data integrity and optimize database performance.
Potential Effects of Choosing the Wrong Data Type
- Data Inconsistency
- Storing numbers as strings can lead to errors in calculations.
- Performance Issues
- Using TEXT for short strings wastes storage space and slows down queries.
- Data Loss or Truncation
- Using CHAR(10) for a string longer than 10 characters will truncate the data.
- Inaccurate Calculations
- Storing prices as INT instead of FLOAT can lead to rounding errors.
A common mistake is using INTEGER for numerical data like phone numbers.
- While it may seem like a number, an integer cannot
- start with a ‘0’
- have + or # in it, which some phone numbers need.
- Common Practice is to use VARCHAR for phone numbers, however:
- Use input validation at the application level to ensure correct formats (don’t rely on the database alone).
- Be careful with length: VARCHAR(15–20) usually works, depending on your region.
Practical Examples
- Name: VARCHAR(100)
- ID Number: INT
- Vegan: BOOL
- Height: FLOAT(3,2)
- Address: TEXT
- Ticket Cost: FLOAT(5,2)
- Quantity: INT
- Why is it important to use FLOAT instead of INT for storing prices?
- What issues might arise if dates are stored as VARCHAR instead of DATE?