...

/

Data Types in SQL

Data Types in SQL

Learn about data types in SQL.

Imagine you’re at a store, deciding how to organize the products on the shelves. Some items are electronics, while others are kitchenware or stationery. Similarly, in a database, every piece of data has a type that determines how it can be stored, manipulated, and queried. Just as it would be inefficient to use one kind of shelf for all product types, SQL data types help ensure data is handled in the most efficient way possible. For example, product prices are stored as numbers, customer names as text, and order dates as dates.

Let's explore different data types in SQL and understand how they help us store and manage data efficiently. We'll go step by step to:

  • Understand various data types in SQL and their applications.

  • Identify and work with numeric, string, and date data types.

  • Discuss the use of comments to write elaborative SQL codes.

Understanding data types in SQL

Data types in SQL define the nature of data that a column can hold such as numbers, text, or dates. They help in enforcing data integrity and optimizing the database's storage. Using the correct data type for each column ensures efficient data processing and avoids errors during data insertion or querying.

For example, to store products in an online store, we need to consider how we represent their attributes effectively. The product names should be stored as strings rather than numbers because product names often include alphabets, spaces, and special characters, which are essential for accurately identifying and describing them. Using a numeric data type would be insufficient, as it cannot capture the variety of characters that make up a product's name. This ensures that when we search for or display product information, we can do so accurately and in a user-friendly format.

Similarly, numbers should be stored as numbers and not as strings because numerical operations, such as addition, subtraction, or comparisons, are more efficient and straightforward with numeric data types. It prevents performance issues and avoids the need for extra conversion steps.

With this understanding, let's see different data types in the following.

Numeric data types

Numeric data types store numerical values and come in various types based on size and precision. In SQL, the common numeric data types include:

  • INT: It is used to store whole numbers without decimal points. It's useful for counts, quantities, and identifiers. For example, in the Products table, if we need to store the number of items available, we'll use an INT data type. This ensures that only whole numbers are stored, which prevents issues like storing 0.5 or 1.5 as the number of items, which wouldn't make sense for a quantity of items in stock.

  • DECIMAL(p, s): It is used to store numbers with fixed precision (p) and scale (s). It's ideal for monetary values where precision is important, like prices. For example, to manage prices accurately up to two decimal places and having a maximum of ten digits, we can use DECIMAL(10, 2). The largest value that can be stored in this case is 99999999.99, as the ten digits account for both the whole number and the decimal places.

String data types

String data types store sequences of characters. This is important for columns like names, email addresses, and descriptions. In SQL, common string data types include:

  • CHAR(n): It is used to store fixed-length character strings. This means that if we store a string shorter than the defined length n, it will still occupy the full length by padding with spaces. For example, with CHAR(2), if we store 'U', it will be stored in the backend as 'U ' (the character 'U' followed by a space). Therefore, the best use of CHAR(n) is for consistent length data entries, such as country codes. For instance, we can use CHAR(2) for codes like 'US', 'UK', and so on.

  • VARCHAR(n): It is used to store variable-length character strings up to n characters. It only uses as much space as the string length, n (plus an extra 1–2 bytesbytes for length information). VARCHAR(n) is efficient for storing text data that varies in length. For instance, we can use VARCHAR(50) to store product names up to 50 characters. A product name like 'Phone' will be stored as a 5 character-long string, without any extra space padding to make it 50 characters long.

Let's quickly look at the key differences between CHAR and VARCHAR:

CHAR(n)

VARCHAR(n)

Always takes the defined space.

Takes only as much space as needed.

Pads with spaces to match length.

Use exactly the space needed for the data.

Maximum length is up to 255 characters.

Maximum length is up to 65,535 characters.

Faster for short, consistent data.

More efficient for long, varying data.

Date and time data types

Date and time data types are used to store date, time, or a combination of both. These are crucial for tracking processes like when transactions occur, when an order was placed, or when products are added to the store. In SQL, common date and time types include:

  • DATE: It is used to store dates in the format YYYY-MM-DD. For example, if an order was placed on 9th of December 2024, we can record it as 2024-12-09.

  • TIME: It is used to store time in the format HH:MM:SS. For example, if an order was placed at half past two in the afternoon, we can record it as 14:30:00.

  • DATETIME: It combines date and time into one field. For example, if an order was placed on 9th of December 2024 at half past two in the afternoon, we can record that as 2024-12-09 14:30:00.

Other data types

Other than the basic types, SQL offers specialized data types tailored for unique use cases and advanced functionality.

  • BOOLEAN: It is used to store the values TRUE or FALSE. For example, we can use it when we need to store the result of conditions like "Is the order delivered?".

  • TINYINT(M): It is used for columns that have a limited range of integer values. It uses 1 byte of storage space. The M refers to the “display width” (adding 0 on the left, if number of digits is less than M), not the storage size. In MySQL, BOOLEAN type is effectively a synonym for TINYINT(1).

  • BLOB: It is used to store large binary objects like images or files.

  • TEXT: It is used to store large text content, such as a blog post, product description, or user comments.

  • ENUM: It is a user-defined data type used to store one of a predefined list of values. The domain of allowable values for an ENUM is established directly by the user. For example, we can use ENUM to store the "Order Status" with predefined values like "Pending," "Shipped," "Delivered," or "Canceled."

  • SET: It is a user-defined data type used to store multiple predefined values in a single field. Similar to ENUM, the domain of allowable values for a SET is determined directly by the user. For instance, we can use SET to store "Hobbies" with predefined values such as "Reading," "Traveling," "Gaming," and "Cooking," allowing a user to select more than one hobby.

Best practices for using data types

Let's look at some tips for effectively using data types and writing helpful comments to improve the database design and code maintainability.

  • Selecting and using the right data type for each column ensures efficient storage and accurate data representation. For example, apply VARCHAR for strings when the length can vary, and CHAR when the length is fixed.

  • Document the SQL code with comments to explain complex queries or important details.

Test your understanding

We’ve learned about different data types in SQL, so now it’s time to put that knowledge into practice with an exercise.

Task

You are provided with a list of components and their potential matches. Your task is to review each component and match it with the appropriate corresponding data type.

Match The Answer
Select an option from the left-hand side

The count of times a product has been purchased.

VARCHAR(300)

A customer’s SSN ( a fixed size 9 digit social security number).

INT

A product description that can be up to 300 characters.

CHAR(9)

A product’s weight.

BOOLEAN

Whether a product is active, yes, or no.

DECIMAL


We've now explored the fundamental data types in SQL and how they are used in a database. We learned how to use numeric, string, and date types to store different kinds of data, as well as the importance of writing clear and maintainable code with comments. Understanding these basics is essential as we move forward in designing and interacting with databases.