当前位置:网站首页>A summary of PostgreSQL data types. All the people are here

A summary of PostgreSQL data types. All the people are here

2022-06-23 04:06:00 It bond


hello ! Hello everyone , I am a 【IT bond 】, Jianghu people jeames007,10 year DBA Work experience
A highly motivated 【 Bloggers in big data field 】!
China DBA union (ACDU) member , Currently engaged in DBA And program programming ,B Station and Tencent classroom lecturer , Live broadcast volume breaking 10W
Good at mainstream data Oracle、MySQL、PG Operations and development , Backup recovery , Installation migration , performance optimization 、 Fault emergency treatment, etc .
If there is a pair of 【 database 】 Interested in 【 Cutie 】, Welcome to your attention 【IT bond 】
️️️ Thank you, big and small !️️️

Preface

This paper deals with PostgreSQL The data types are summarized comprehensively , I hope that's helpful

️ 1. value type

The following table lists them PostgreSQL Supported numeric types

 Insert picture description here

postgres=# create table jem (id serial ,name varchar(20));
postgres=# insert into jem(name) values(‘IT bond ’);

 Insert picture description here

️ 2. Type of currency

money Type stores monetary amounts with fixed decimal precision .
numeric、int and bigint The value of type can be converted to money,
Floating point numbers are not recommended to handle currency types , Because there is the possibility of rounding errors .
name storage capacity describe Range
money 8 byte The amount of money -92233720368547758.08 To +92233720368547758.07

️ 3. Character type

PostgreSQL Supported character types :
Serial number name & describe
1 varchar(n) Lengthening , Limited length
2 character(n), char(n) Fixed length , Fill in the blanks
3 text Lengthening , No length limit

️ 4. date / Time type

The following table lists them PostgreSQL Supported date and time types
 Insert picture description here

️ 5. Boolean type

PostgreSQL Supporting the standard boolean data type .
boolean Yes "true"( really ) or "false"( false ) Two states ,
The third kind of "unknown"( Unknown ) state , use NULL Express .

 Insert picture description here

️ 6. Enumeration type

An enumeration type is a data type that contains an ordered collection of static and values .
PostgtesSQL The enumeration types in are similar to C In language enum type .
Unlike other types, enumeration types need to use CREATE TYPE Command to create

postgres=# CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
 Create a few days of the week , As shown below :
postgres=# CREATE TYPE 
week AS ENUM ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
 Just like other types , Once created , Enumeration types can be used for table and function definitions .
postgres=# CREATE TABLE person (
name text,
current_mood mood
);
postgres=# INSERT INTO person VALUES ('Moe', 'happy');
postgres=# SELECT * FROM person WHERE current_mood = 'happy';

 Insert picture description here

️ 7. Enumeration type

Geometric data types represent two-dimensional planar objects .
The following table lists them PostgreSQL Supported geometry types .
Most basic types : spot . It is the basis of other types .

 Insert picture description here

️ 8. Network address type

PostgreSQL Provide for storage IPv4 、IPv6 、MAC Data type of address .
Using these data types to store network addresses is better than using plain text types ,
Because these types provide input error checking and special operations and functions

 Insert picture description here

️ 9. Bit string type

A bit string is a string 1 and 0 String . They can be used to store and visualize bitmasks .
We have two kinds of SQL A type of :bit(n) and bit varying(n), there n Is a positive integer .
bit Data of type must exactly match the length n, Trying to store shorter or longer data is wrong .
bit varying Type data is the longest n Variable length type of ; Longer strings will be rejected . Write a without length bit Equivalent to bit(1),
Having no length bit varying It means no length limit .

️10. Text search type

Full text retrieval is to find those searches that match a query through the collection of natural language documents .
PostgreSQL Two data types are provided to support full-text retrieval :

 Insert picture description here

️11.UUID type

uuid Data types are used to store RFC 4122,ISO/IEF 9834-8:2005 And the universal unique identifier defined by relevant standards (UUID). ( Some systems consider this data type to be a globally unique identifier , or GUID.)
This identifier is generated by an algorithm 128 Bit identifier ,
Make it impossible for it to use the same algorithm in a module known to use the same identifier as that generated in other ways .
therefore , For distributed systems , This identifier can provide better uniqueness guarantee than sequence ,
Because sequences can only be guaranteed to be unique in a single database .
UUID Written as a sequence of lowercase hexadecimal digits , Divided into groups by characters ,
Especially a group 8 Digit number +3 Group 4 Digit number + A group of 12 Digit number , in total 32 A number stands for 128 position ,
One such standard UUID Examples are as follows :
a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11

️12.XML type

xml  Data types can be used to store XML data . 
 take  XML  Data stored in  text  The advantage of type is that it can check the input value for well structured ,
  And it also supports functions to check its type security . 
 To use this data type , Compile with  configure --with-libxml.
xml  Can be stored by XML The standard definition is well formed " file ",
  And by  XML  Standard  XMLDecl? content  Defined " Content " fragment ,  In general ,
  This means that content fragments can have multiple top-level elements or character nodes .
  xmlvalue IS DOCUMENT  Expressions can be used to determine a particular  xml  Is the value a complete file or a fragment of content .
 Using functions  xmlparse:  To generate... From character data  xml  Type value :
XMLPARSE (DOCUMENT '<?xml version="1.0"?>
<book><title>Manual</title><chapter>...</chapter></book>')
XMLPARSE (CONTENT 'abc<foo>bar</foo><bar>foo</bar>')

️13.JSON type

json Data types can be used to store JSON(JavaScript Object Notation) data ,
Such data can also be stored as text,
however json The data type makes it easier to check that each stored value is available JSON value .
In addition, there are related functions to deal with json data :

 Insert picture description here

️14. An array type

PostgreSQL Allows fields to be defined as multidimensional arrays of variable length .
The array type can be any basic type or user-defined type , Enumeration type or composite type .

14.1 Declaration array

When creating a table , We can declare arrays , The way is as follows :

#pay_by_quarter Is a one-dimensional integer array 、schedule An array of two-dimensional text types .
postgres=# CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);
## We can also use “ARRAY” keyword
postgres=# CREATE TABLE sal_emp (
name text,
pay_by_quarter integer ARRAY[4],
schedule text[][]
);

14.2 Insert value

# Insert values using curly braces {}, The elements are in {} Separated by commas :
postgres=# INSERT INTO sal_emp VALUES (‘Bill’,
‘{10000, 10000, 10000, 10000}’,
‘{ {“meeting”, “lunch”}, {“training”, “presentation”}}’);
postgres=# INSERT INTO sal_emp
VALUES (‘Carol’,’{20000, 25000, 25000, 25000}’,
‘{ {“breakfast”, “consulting”}, {“meeting”, “lunch”}}’);

 Insert picture description here

14.3 Access array

Now we can run some queries on this table .
First , We show how to access an element of an array . This query retrieves the names of employees whose salaries changed in the second quarter :
postgres=# SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];

 Insert picture description here

14.4 Modify array

## We can modify the value of the array :
postgres=# UPDATE sal_emp SET pay_by_quarter = ‘{25000,25000,27000,27000}’
WHERE name = ‘Carol’;
## Or use ARRAY Constructor Syntax
UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000]
WHERE name = ‘Carol’;

 Insert picture description here

14.5 Retrieve... From the array

## To search for values in an array , You must check every value of the array
SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
pay_by_quarter[2] = 10000 OR
pay_by_quarter[3] = 10000 OR
pay_by_quarter[4] = 10000;
## in addition , You can use the following statement to find out that the values of all elements in the array are equal to 10000 The line of :
SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);

 Insert picture description here

️15. The compound type

A composite type represents the structure of a row or a record ;
It's actually just a list of field names and their data types .
PostgreSQL Allow composite types to be used like simple data types .
such as , A field of a table can be declared as a composite type .

️16. Range type

The range data type represents the value of an element type within a certain range .
for example ,timestamp The range may be used to represent the time range within which a conference room is scheduled .
PostgreSQL The built-in range types are :
int4range — integer The scope of the
int8range —bigint The scope of the
numrange —numeric The scope of the
tsrange —timestamp without time zone The scope of the
tstzrange —timestamp with time zone The scope of the
daterange —date The scope of the
Besides , You can define your own scope type :

CREATE TABLE reservation (room int, during tsrange);
INSERT INTO reservation VALUES
(1108, ‘[2010-01-01 14:30, 2010-01-01 15:30)’);
– contain
SELECT int4range(10, 20) @> 3;
– overlap
SELECT numrange(11.1, 22.2) && numrange(20.0, 30.0);
– Extract the upper boundary
SELECT upper(int8range(15, 25));
– Calculation crossover
SELECT int4range(10, 20) * int4range(15, 25);
– Whether the range is empty
SELECT isempty(numrange(1, 5));

️17. Object identifier type

PostgreSQL Use object identifiers internally (OID) As the primary key of various system tables .
meanwhile , The system will not add a to the table created by the user OID System fields ( Unless it is stated that WITH OIDS Or configure parameters default_with_oids Set to on ).oid Type represents an object identifier . in addition to oid There are several aliases :regproc, regprocedure, regoper, regoperator, regclass, regtype, regconfig, and regdictionary.

 Insert picture description here

️18. Pseudo type

PostgreSQL The type system contains a series of special-purpose entries ,
They are called pseudo types by category . A pseudo type cannot be used as the data type of a field ,
But it can be used to declare the parameter or result type of a function .
Pseudo types in a function do not simply accept and return some kind of SQL Useful in the case of data types .
The following table lists all pseudo types :

 Insert picture description here
Everybody likes it 、 Collection 、 Focus on 、 Comments WeChat official account

原网站

版权声明
本文为[It bond]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211734002836.html