1. 程式人生 > 其它 >postgresql中網路地址型別和布林型別

postgresql中網路地址型別和布林型別

建立測試使用者,表空間,以及資料庫,賦予許可權

postgres=# create role pguser with encrypted password 'pguser';
CREATE ROLE
postgres=# create tablespace tbs_mydb owner pguser location '/data/10/tbs_mydb';
CREATE TABLESPACE                                                            ^
postgres=# create database mydb with owner=pguser template=template0 encoding='UTF8' tablespace=tbs_mydb;
CREATE DATABASE
postgres=# grant all on database mydb to pguser with grant option;
GRANT
postgres=# grant all on tablespace tbs_mydb to pguser;
GRANT
postgres=# alter role pguser login;
ALTER ROLE

1.布林型別

字元型別名稱 儲存長度 描述
boolean 1byte 值為TRUE或者FALSE,0,1,yes,no,t,f,y,n

mydb=> create table test_bool(a boolean,b boolean);
CREATE TABLE

mydb=> insert into test_bool values('true','false');
INSERT 0 1
mydb=> select * from test_bool;
 a | b 
---+---
 t | f
(1 row)

mydb=> insert into test_bool values('y','n');
INSERT 0 1
mydb=> select * from test_bool;
 a | b 
---+---
 t | f
 t | f
(2 rows)

mydb=> insert into test_bool values('t','f');
INSERT 0 1
mydb=> select * from test_bool;
 a | b 
---+---
 t | f
 t | f
 t | f
(3 rows)

mydb=> insert into test_bool values('1','0');
INSERT 0 1
mydb=> select * from test_bool;
 a | b 
---+---
 t | f
 t | f
 t | f
 t | f
(4 rows)


HINT:  You will need to rewrite or cast the expression.
mydb=> insert into test_bool values(null,null);
INSERT 0 1

HINT:  You will need to rewrite or cast the expression.
mydb=> select * from test_bool;注意插入null之後,是由資料,只不過資料值為空
 a | b 
---+---
 t | f
 t | f
 t | f
 t | f
   | 
(5 rows)

2.網路地址型別

字元型別名稱 儲存長度 描述
cidr 7/19位元組 IPV4/IPV6網路
inet 7/19位元組 IPV4/IPV6網路
macaddr 7/19位元組 MAC地址
macaddr8 7/19位元組 MAC地址(EUI-64格式)

inet和cidr型別儲存格式為IP地址/掩碼,如果掩碼省略,則IPV4掩碼為32,IPV6掩碼為128

mydb=> create table test_ipaddres(a cidr,b inet);
CREATE TABLE
mydb=> 
mydb=> \d+ test_ipaddres 
                             Table "public.test_ipaddres"
 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description 
--------+------+-----------+----------+---------+---------+--------------+-------------
 a      | cidr |           |          |         | main    |              | 
 b      | inet |           |          |         | main    |              | 

mydb=> insert into test_ipaddres values ('192.168.1.10/32','192.168.1.10/16');
INSERT 0 1
mydb=> 
mydb=> select * from test_ipaddres 
mydb-> ;
        a        |        b        
-----------------+-----------------
 192.168.1.10/32 | 192.168.1.10/16

**inet和cidr型別的資料都會對資料進行是否合法的檢查**
mydb=> select '192.168.1.300'::cidr;
ERROR:  invalid input syntax for type cidr: "192.168.1.300"
LINE 1: select '192.168.1.300'::cidr;
               ^
mydb=> select '192.168.1.300'::inet;
ERROR:  invalid input syntax for type inet: "192.168.1.300"
LINE 1: select '192.168.1.300'::inet;
               ^
mydb=> 
**cidr會預設輸出掩碼資訊,inet不會輸出掩碼資訊**
mydb=> select '192.168.1.100'::inet;
     inet      
---------------
 192.168.1.100
(1 row)

mydb=> select '192.168.1.100'::cidr;
       cidr       
------------------
 192.168.1.100/32
(1 row)

mydb=> 
**cidr會對IP和掩碼進行合法性檢查,inet不會**
mydb=> select '192.168.1.100/24'::inet;
       inet       
------------------
 192.168.1.100/24
(1 row)

mydb=> 
mydb=> 
mydb=> select '192.168.1.100/24'::cidr;
ERROR:  invalid cidr value: "192.168.1.100/24"
LINE 1: select '192.168.1.100/24'::cidr;
               ^
DETAIL:  Value has bits set to right of mask.
mydb=> 

取IP值
mydb=> select host(cidr '192.168.1.232/32');
     host      
---------------
 192.168.1.232
(1 row)
取IP和掩碼
mydb=> select text(cidr '192.168.1.232/32');
       text       
------------------
 192.168.1.232/32
(1 row)

取子網掩碼
mydb=> select netmask(cidr '192.168.1.232/32');
     netmask     
-----------------
 255.255.255.255
(1 row)