Data Types

When you create a new table, you must decide on the data types for each column. You already encountered some of the general column types, such as VARCHAR and INT, in Lesson 14. Now let's look at the most common data types in more detail.

Data Types

For a complete list of the data types you can use, refer to Appendix D, "MariaDB Data Type Reference," which can be found on the book's website at www.samspublishing.com.


INTEGER

An integer is a whole number. The range of values that can be stored in an integer data type depends on the size of the integerMariaDB has five different sizes:

Unsigned Values

If an integer column is declared UNSIGNED, then it cannot store negative values. Doing so doubles the maximum values it can store.


In the sample database, quantity is defined as TINYINT UNSIGNEDit is expected that no single order will include more than 255 of the same product. The column is declared UNSIGNED because it is nonsense to allow a negative quantity.

DECIMAL

DECIMAL is an exact, fixed-point number. It is declared as DECIMAL(M,D), where M is the total number of digits and D is the number of digits after the decimal point.

In the sample database, we used DECIMAL for both the weight and price columns on the products table. Each was declared as DECIMAL(6,2), meaning that the largest value that could be stored in each is 9999.99six numbers in total, with two appearing after the decimal point.

Previous Page Next Page