TINYINT DataType in MySQL

  • In MySQL Workbench, the TINYINT data type is used to store small integer values that have a relatively small range. It is a one-byte integer data type that can represent whole numbers. The TINYINT data type can be further categorized into two variations: signed and unsigned.
Let's explore the details of the TINYINT data type:
  • TINYINT (Signed):
    • Size: 1 byte (8 bits)
    • Range: The signed TINYINT data type can store values from -128 to 127.
    • Usage: Use the signed TINYINT when you need to store small integer values that can be both positive and negative.
Example of creating a table with a signed TINYINT column:


    CREATE TABLE example_table (
        id TINYINT,
        age TINYINT
    );

  • TINYINT UNSIGNED (Unsigned):
    • Size: 1 byte (8 bits)
    • Range: The unsigned TINYINT data type can store values from 0 to 255.
    • Usage: Use the unsigned TINYINT when you need to store small integer values that are always non-negative (positive).
Example of creating a table with an unsigned TINYINT column:


    CREATE TABLE example_table (
        user_id TINYINT UNSIGNED,
        points TINYINT UNSIGNED
    );

  • TINYINT data type is commonly used when storage space is a concern, and the values you need to store fall within the range offered by this data type. It is especially useful when you want to optimize storage for small, non-negative values, such as representing age, quantity, or status flags.
  • However, keep in mind that if you expect to store larger integer values, or values that can be both positive and negative, you may need to consider using larger integer data types such as SMALLINT, INT, or BIGINT.
When choosing between signed and unsigned TINYINT, consider the nature of the data you are dealing with:
  • Use Signed TINYINT: When you need to represent small integer values that can be both positive and negative.
  • Use Unsigned TINYINT: When you need to represent small integer values that are guaranteed to be non-negative (positive) and you want to maximize the range of positive values.
  • Remember that choosing the appropriate data type is crucial for data accuracy and efficient storage in your MySQL database. Using a data type that is too large may lead to wasted storage space, while using a data type that is too small can result in data truncation or loss of precision.

No comments:

Post a Comment

Date and Time related aggregation functions ($year, $month, $dayOfMonth, $hour, $minute, $second, $dayOfWeek, $dateToString, $dateSubtract, $dateAdd, $isoWeek, $isoDayOfWeek, $dateTrunc, $dateFromString, $dateToParts, $dateFromParts, $dateDiff, $week)

In this blog post, we will explore Date/Time-related Aggregation Functions in MongoDB. MongoDB provides a robust set of aggregation operator...