DB/PostgreSQL

[PostgreSQL] PostgreSQL의 숫자 타입에 대해서 알아보자.

빙기때침식곡 2022. 6. 6. 21:35
반응형

PSQL에서는 다양한 데이터 타입에 대해서 지원한다. 

하나하나 알아보자.

 

 

1. Numeric Types (숫자 타입)

 

(1) Integer (int, int4)

 - 4 byte

 - 일반적인 선택

 - -2147483648 ~ +2147483648

 

(2) smallInt (int2)

 - 2 byte

 - generally only used if disk space is at a premium (일반적으로 디스크 용량이 없을 때)

 - -32768 ~ +32767

 

(3) bigInt (int8)

 - 8 byte

 - be used when the range of the integer type is insufficient

(integer의 범위가 충분하지 않을 때 보통 사용한다.)

 - -9223372036854775808 ~ +9223372036854775808

 

 

 

2. Arbitrary Precision Numbers (임의의 정밀한 숫자)

 

numeric (decimal)

 - variable

 - can store numbers with a  large number of digits

 - 소수점 위 131072 자리까지 소주점 아래 16383까지

 

NUMERIC(precision, scale)
NUMERIC(precision)
NUMERIC

 

ex) 32.5141

 - precision (정밀도) : 6

 - sacale (스케일) : 4

 

 

 

3. Floating-Point Type (부동 소수점 타입)

 

IEEE 754를 따르고 있고, 부동 소수점 타입을 다룰 땐 약간의 오차가 발생할 수 있다.

정확하게 변환 할 수 없고 근사값으로 저장되므로 정확한 연산에 적합하진 않다.

 

(1) real (float4)

 - 소수점 최대 6자리

 

(2) double precision (float8)

 - 소수점 최대 15자리

 

단, 둘 다 0으로 끝나는 소수점은 채워주지 않는다.

 

ex)

CREATE TABLE number_data_types(
	numeric_column numeric(20, 5),
	real_column real,
	double_column double precision
);

INSERT INTO number_data_types VALUES (.7, .7, .7);
INSERT INTO number_data_types VALUES (2.13579, 2.13579, 2.13579);
INSERT INTO number_data_types VALUES (2.1357987654, 2.1357987654, 2.1357987654);

 

result

 

 

create table numtest3 (num1 real, num2 double precision);
CREATE TABLE

insert into numtest3 values (9.4475658, 52.75120024568652456);
select * from numtest3;

 

result

각각의 데이터 형 이상의 자릿수의 값을 저장하려고 할 때, 반올림 저장될 수 있다.

 

 

** 부동 소수점 타입이 오차가 발생하는 이유

 

10진수 : 9.1234567

2진수 : 1001.000111111001101011011011...

정규화 : 1.001000111111001101011011011...

 

10진수일 때 유한소수인 것을 2진수로 바꾸면 무한소수가 된다.

 

float 자료형을 사용 = 32비트 (부호(1비트), 지수(8비트), 가수(23비트))

double = 64비트 (부호(1비트), 지수(11비트), 가수(52비트))

여기서 1.001000111111001101011011011… 무한소수를 모두 저장할 수 없기 때문에 23비트만 저장

짤리는 부분 때문에 오차가 생김

 

 

4. Serial Type (시리얼 타입)

 

 

"To create an auto-incrementing column"

(자동 증가 컬럼을 만들기 위함)

 

타 DB의 Auto_increment와 유사

 

(1) smallserial (serial2)

 

(2) serial (serial4)

 

(3) bigserial (serial8)

 

 

 

REFERENCES

 

PostgreSQL : Documentation: 13: PostgreSQL 13.7 Documentation

 

postgrespro.com

 

반응형