After understanding what PostgreSQL is and installing it, in this article I will introduce you to the data types in PostgresSQL.
Numeric Types
The numeric data types in postgresSQL have the following types, see the table below:
Type | Size | About |
---|---|---|
smallint | 2 bytes | -32768 to +32768 |
integer | 4 bytes | -2147483648 to +2147483648 |
bigint | 8 bytes | -9223372036854775808 to +9223372036854775808 |
decimal | variable | this is a dynamic data type so there can be up to 131072 digits before the decimal point and 16383 digits after the decimal point |
numeric | variable | this is a dynamic data type so there can be up to 131072 digits before the decimal point and 16383 digits after the decimal point |
real | 4 bytes | real numeric data type, with precision up to 6 decimal places |
double precision | 8 bytes | real numeric data type, with precision up to 15 decimal places |
smallserial | 2 bytes | 1 to 32767 |
serial | 4 bytes | 1 to 2147483647 |
bigserial | 8 bytes | 1 to 9223372036854775807 |
Umm, is there anything to note here, you guys are no strangers to the above data types, right? Here I will have a little note between some of the above types.
Type numeric, with this guy when using you will have 3 ways to use as follows:
NUMERIC( precision , scale )
NUMERIC( precision )
NUMERIC
In the above declaration, we will have 2 parameters, precision (maximum precision) and scale (maximum scale), as simple as this. For example, we have the number 374.2341 so according to this number, precision=7 and scale=4 and in integers then of course scale will be 0.
Next is the difference between numeric and double precision:
In this example, I will use the following command:
SELECT x, round(x::numeric) AS num_round, round(x::double precision) AS dbl_round FROM generate_series(-3.5, 3.5, 1) as x;
Let me explain a bit the above query for those of you who are not clear: we will use the generate_series function, this function will pass in 3 parameters respectively: the starting number, the ending number, and the unit. The above statement will generate numbers from -3.5 to 3.5 with each number separated by 1 unit, the round function I will use to round, before rounding, I will use the :: syntax as above to force the type to return. 2 types above, from which we can compare the difference between the two data types above.
As you can see from the above results, the two data types numeric and double precision have a difference when rounding right. According to the above results, the numeric guy will round according to the principle that if the decimal number at the position to be rounded >=0.5 will be rounded up and <0.5 will be rounded down, and for the double precision guy, it will follow the principle of rounding to the nearest even number. For example 3.5, the double precision guy will round up to 4 because 4 will be closer to 3.5 than 2 and with 2.5 it will round down to 2 because 2 will be closer to 2.5 than 4. With the above example, you should note when using the above data types, to avoid data errors.
I’ll just come here today, I’m a bit lazy today, I’ll write more tomorrow….
Remember to support me with 1 vote to give me more motivation
Monetary Types
This is a data type that stores a currency amount with the precision set:
Type | Size | About |
---|---|---|
money | 8 bytes | -92233720368547758.08 to +92233720368547758.07 |