postgresql数据库——数据类型总结
postgresql数据库的 数据类型?
postgresql支持多种数据类型,主要有:整数类型、浮点数类型、任意精度数值、日期时间类型、字符串类型、二进制类型、布尔类型和数组类型等。 1、整数类型 ? ?postgresql提供了多种数值数据类型,不同的数据类型提供不同的取值范围,可以存储的值范围越大,所需要的存储空间也会越大。 ? postgresql提供的整数类型有:mallint、int、bigint。 《类型名称》? 《说明》? ? ?《存储需求》? ?《取值范围》 smallint? ?小范围的整数? ? ? ?2字节? ? ? ? -32768----32767 int? ? ? ? 普通大小的整数? ? ?4字节? ? ? ? -2147483648----2147483647 bigint? ? ?大整数? ? ? ? ? ? 8字节? ? ? ? -9223372036854775808----9223372036854775807 2、浮点数类型: postgresql中使用浮点数来表示小数。浮点类型有两种:real和double precision,如下所示: 《类型名称》? ? ? ? ? ? 《说明》? ? ? ? ?《存储需求》?? real? ? ? ? ? ? ? ? ?6位十进制数字精度? ? ? ? ? 4字节 double precision? ? ?15位十进制数字精度? ? ? ? ?8字节 3、任意精度类型 ? postgresql中使用numeric表示任意精度的类型是数值,使用numeric(m,n)来表示,其中m称为精度,表示总共的位数;n称为标度,是表示小数的位数。例如:563.129的精度为9,标度为3 注意:numeric的有效取值范围由M和D的值决定。如果改变M而固定D,那么其取值范围将随M的变大而变大。另外,如果用户指定的精度超出精度外,就会四舍五入进行处理。如下: testdb=# create table emp (t numeric(5,1),y numeric(5,3));?? testdb=# insert into emp values (9.12,9.156); testdb=# select * from emp; ? t? |? ?y? ? -----+------- ?9.1 | 9.156 4、日期与时间类型: ? ?postgresql中有多种表示日期的数据类型,主要有:time、date、timestamp和interval。每一个类型都有合法的取值范围,当指定确实不合法的值时系统将“零”值插入数据库中。如下类型介绍: 《类型名称》? ? ?《含义》? ? ? ? ? ? 《日期范围》? ? ? ? ?《存储需求》? ? ? 《存储格式》 time? ? ? ? 只用于一日内的时间? ?00:00:00---24:00:00? ? ? ? 8字节? ? ? ? ? ?HH:MM:SS? ? ? date? ? ? ? 只用于日期? ? ? ? ? 4713BC---58784897AD? ? ? ? ?4字节? ? ? ? ? YYYY-MM-DD? ? timestamp? ?日期和时间? ? ? ? ? 4713BC---58784897AD? ? ? ? ?8字节? ? ? ?YYYY-MM-DD HH:MM:SS? 注意:time和timestamp类型,默认情况下为without time zone(不带时区);如果需要,可以设置为带时区(with time zone ) 4.1、time:类型使用 time类型用在时间上,在存储时需要8字节,格式为:HH:MM:SS。HH表示小时;MM表示分钟;SS表示秒。 例子: testdb=# create table test (t time); testdb=# insert into test values ('11:10:25 '),('23:45');? ?---注意,第一个时间值最后有一个空格, testdb=# select * from test; ? ? t? ? ? ---------- ?11:10:25 ?23:45:00 (2 rows) 从上面的结果可以看出,两个时间都正确插入,而且格式都为:HH:MM:SS testdb=# insert into test values (current_time),(now());? ? ---插入系统当前时间 INSERT 0 2 testdb=# select * from test; ? ? ? ? t? ? ? ?? ----------------- ?11:10:25 ?23:45:00 ?04:56:57.034863 ?04:56:57.034863 (4 rows) 从上面可以看出,会显示时区; 4.2、date:类型 ? ?date类型用在日期值时,没有时间部分,存储需要4字节。日期格式为:'YYYY-MM-DD',其中YYYY表示年;MM表示月;DD表示日;。在给date类型的字段赋值时,可以使用字符串类型或者数字类型的数据插入,只要符合date的日期格式即可。 ①:以“YYYY-MM-DD”或者“YYYYMMDD”字符串格式表示的日期。例如,输入‘2018-12-31’或者‘20181231’,插入数据库的日期都为2018-12-31. ②:以“YY-MM-DD”或者“YYMMDD”字符串格式表示的日期,在这里YY表示两位的年值。postgresql使用以下规则解释两位年值:“00-69”转换年值为‘2000--2069’;‘70-99’转换年值为‘1970-1999’; ③:利用current_date或者now()插入当前系统日期。 例子: testdb=# create table emp (d date); testdb=# insert into emp values ('1998-08-08'),('19980808'),('20180808'); testdb=# insert into emp values (now());? ---插入系统当前值 testdb=# select * from emp; ? ? ?d? ? ?? ------------ ?1998-08-08 ?1998-08-08 ?2018-08-08 ?2018-06-23 (4 rows) 注意:now()函数返回日期和时间值,在保存到数据库时,只保留了日期部分。 4.3、timestamp:类型 timestamp的日期格式为YYYY-MM-DD HH:MM:SS。在存储时需要8字节。如: testdb=# create table emp (ts timestamp); testdb=# insert into emp values ('2018-08-10 11:00:02'),(now()); testdb=# select * from emp; ? ? ? ? ? ? ts? ? ? ? ? ? ? --------------------------- ?2018-08-10 11:00:02 ?2018-06-23 05:14:17.52538 (2 rows) 4.4、创建带时区的日期和时间类型 testdb=# create table emp (t time with time zone); CREATE TABLE testdb=# insert into emp values ('12:10:05 PST'),('12:10:10'); INSERT 0 2 testdb=# select * from emp; ? ? ? t? ? ?? ------------- ?12:10:05-08 ?12:10:10-04 (2 rows) 其中,PST为西8区,如果不指定时区,默认是东8区。 5、字符串类型: ? ?字符串类型用来存储字符串数据,除了可以存储字符串数据之外,还可以存储其他数据,比如图片和声音的二进制数据。除了可以进行区分或者不区分大小写的字符串比较外,还可以进行模式匹配查找。postgresql的字符串类型包括:char、varchar和text。 《类型名称》? ? ? ? ? ? ? ? ? ? ? 《说明》 char(n)? ? ? ? ? ? ? 固定长度非二进制字符串,不足补空白 varchar(n)? ? ? ? ? ?变长非二进制字符串,有长度限制 text? ? ? ? ? ? ? ? ?变长非二进制字符串,无长度限制 注意: ? ?char(n) varchar(n),其中n是一个正整数。表示可以存储n个字符的字符串。如果要存储的字符串比声明的长度短,类型为char的数值将会用空白填满;而类型为varchar的数值将只存储短些的字符串。 ? ?char类型中填充的空白是无意义的。例如,比较两个char值的时候填充的空白都会被忽略,在转换成其他字符串类型的时候,char值里面的空白会被删除,注意在varchar和text数值里,结尾的空白是有意思的。 例子: testdb=# create table emp (v1 char(5),v2 varchar(10)); CREATE TABLE testdb=# insert into emp values ('ab','ab '),('abc','abc'),('ab? ?','ab? ?'); INSERT 0 3 testdb=# select * from emp; ? v1? ?|? v2? ? -------+------- ?ab? ? | ab? ?abc? ?| abc ?ab? ? | ab? ? (3 rows) testdb=# select concat('(',v1,')'),concat('(',v2,')') from emp; ?concat? | concat?? ---------+--------- ?(ab? ?) | (ab ) ?(abc? ) | (abc) ?(ab? ?) | (ab? ?) (3 rows) --text类型: ? postgresql提供text类型,它可以存储任何长度的字符串。虽然text类型不是sql标准,但是许多其他sql数据库系统也有。 例子: testdb=# create table emp (t1 text); testdb=# insert into emp values ('ab'),('agcd'),('ab? ?'); testdb=# select * from emp; ? t1? ? ------- ?ab ?agcd ?ab? ? (3 rows) testdb=# select concat('(',t1,')') from emp; ?concat?? --------- ?(ab) ?(agcd) ?(ab? ?) (3 rows) 6、二进制类型 ? ?postgresql支持两类字符型数据:文本字符串和二进制字符串,postgresql提供了bytea类型,用于存储二进制字符串。bytea类型存储空间为4字节加上实际的二进制字符串。 testdb=# create table emp (b bytea);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?^ testdb=# insert into emp values (E' 00'); INSERT 0 1 testdb=# select * from emp; ? b? ? ------ ?x00 (1 row) 7、布尔类型 ? ?postgresql提供了Boolean布尔数据类型。Boolean用1字节来存储,提供了true(真),false(假)两个值。另外,用户可以使用其他有效文本值替代true和false。替代true的文本值为‘t’‘true’‘y’,'1'‘yes’;替代false的文本值为‘f’‘n’,'0' testdb=# create table emp (b boolean); CREATE TABLE testdb=# insert into emp values (true),(false),('y'),('no'),('0'); INSERT 0 5 testdb=# select * from emp; ?b? --- ?t ?f ?t ?f ?f (5 rows) 8、数组类型 postgresql允许将字段定义成定长或变长的一维或多维数组。数组类型可以是任何基本类型或用户定义类型。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |