Skip to main content

Introduction of datatypes in PostgreSQL

Before going to learn the next steps like create database, table, function and its other thing we have to know about the datatypes, so in this article we will learn about the datatypes. It will be beneficial for us when we are doing architecture design in PostgreSQL, it will give several benefits like consistency, Validation, Compactness, and performance. Consistency means we have to choose proper datatype for a column so that we can get the consistent and fastest result. Validation means, if we use proper datatype for a column we can easily validate data which is out of scope from the outside. Compactness means, we will use appropriate datatype as per required data, if data comes in single value then we have to choose data type accordingly. Proper datatype according to data scope will increase the performance which is a very important factor in any database architecture design. PostgreSQL supports a wide set of the datatype, it also provides the facility to create user-defined datatype as per user requirement.
Let’s start a brief description of datatypes in PostgreSQL.
smallint: it is small range integer, can store 2bytes of data which range is -32768 to +32767.
integer: its storage size is 4bytes and range is -2147483648 to +2147483647
bigint: it is large range integer; the range is -9223372036854775808 to 9223372036854775807.
decimal: range is up to 131072 digits before the decimal point; up to 16383 digits after the decimal point.
numeric: range is up to 131072 digits before the decimal point; up to 16383 digits after the decimal point.
real: 4bytes storage size and range is 6 decimal digits precision.
double precision: 8-bytes storage size and range is 15 decimal digits precision.
smallserial: 2-byte storage size and range is 1 to 32767.
serial: 4 bytes storage size and the range is 1 to 2147483647.
bigserial: 8 bytes storage size and the range is 1 to 9223372036854775807.
money: 8 bytes storage size and the range is -92233720368547758.08 to +92233720368547758.07.
------Character type--------
character varying(n): Variable-length bit string, variable length with the limit.
character varchar(n): Variable-length bit string, variable length with the limit.
character(n): fixed-length character string.
char(n): fixed-length character string.
-------Binary Data Types---------
bytea: 1 or 4 bytes plus the actual binary string storage size
-------Date/Time data types---------
timestamp[(p)][without time zone]: 8 bytes storage size, it's contain both date and time(no time zone).
timestamp[(p)][with time zone]: 8 bytes storage size, it's contain both date and time(with  time zone).
date: 4 bytes storage size, its contains date only (not a time of the day)
time[(p)][without time zone]: 12 bytes storage size, its contains times of demurely without time zone.
time[(p)][with time zone]: 12 bytes storage size, its contains times of demurely with time zone.
interval [fields ] [(p)]: 12 bytes storage size it contains time intervals.
------Boolean Type------
boolean: 1-byte storage size, state of true or false.
-------- Geometric Type-------
geometric data types represent two-dimensional spatial objects
point: 16 bytes storage size, its contain the coordinates of the two-dimensional blocks. For example (x, y) x is latitude and y is longitude, x and y is floating points.
line: 32 bytes storage size, the line connects the two points, so this data types column contains the two points as like [(x1, y1), (x2, y2)].
lseg: 32 bytes storage size, the line connects the two points, so this data types columns contains the two points as like [(x1, y1), (x2, y2)] online. Suppose (x1,y1) and (x2, y2) is line points and (x4, y4) and (x5,y5) belongs to the line, it means (x4, y4) and (x5, y5) is a segment of the line.
box: 32 bytes storage size, box means a geometrical draw of rectangular and square. boxes are represented by pairs of points that the opposite corner of the box value of type box are specified using the sentence like ((x1, y1), (x2, y2)).
Where (x1, y1) and (x2, y2) are two opposite corners of the box.
path: 16 bytes storage size and can be extended as 16+16n, path are represented by list of connected. Fast can be open where the first and last point in the list are considered not committed or closed where the first and last point are considered connected.
[(X1, Y1), (X2, Y2),…….(Xn, Yn)]
polygon: 40 + 16n storage size, polygons are very similar to the closed path but are stored differently and have their own set of support routines
[(X1, Y1), (X2, Y2),…….(Xn, Yn)]
circle: 24 bytes storage size, Circle means, a point and a radius and used like as <(x1, y1), r>.
-----------------------Network Address data types----------------------
PostgreSQL provides facility to store Ipv4, IPv6, and Mac addresses. It is better to use this type instead of plain Text type to store network addresses because this time offers input error checking any specialized operators and functions.
cidr: 7 bytes storage size, can store Ipv4 and Ipv6 networking data.
inet: 7 bytes storage size, can store Ipv4 and Ipv6 host networking data.
macaddr: 6 bytes storage size, can use to store MAC address


Popular posts from this blog

Remove special characters from string in SQL server

I faced many times an issue to remove special characters from a string. Suppose you are working on searching concept and you have to remove the special characters from search string due to query performance, there are many solution are available but T-SQL is easily resolved this issue.
Following query may help you to resolve your issue.

DECLARE@strVARCHAR(400) DECLARE@expresVARCHAR(50)='%[~,@,#,$,%,&,*,(,),.,!]%' SET@str='(remove) ~special~ *characters. from string in sql!' WHILEPATINDEX(@expres,@str)> 0 BEGIN SET@str=Replace(REPLACE(@str,SUBSTRING(@str,PATINDEX(@expres,@str), 1 ),''),'-',' ') END SELECT@str



What is difference between UNION and UNION ALL in SQL Server

We use UNION and UNION ALL operator to combine multiple results set into one result set.
UNION operator is used to combining multiple results set into one result set but removes any duplicate rows. Basically, UNION is used to performing a DISTINCT operation across all columns in the result set. UNION operator has the extra overhead of removing duplicate rows and sorting result.
UNION ALL operator use to combine multiple results set into one result set but it does not remove any duplicate result. Actually, this does not remove duplicate rows so it is faster than the UNION operator. If you want to combine multiple results and without duplicate records then use UNION otherwise UNION ALL is better.
Following some rules for using UNION/UNION ALL operator
1.The number of the column should be the same in the query's when you want to combine them. 2.The column should be of the same data type. 3.ORDER BY clause can be applied to the overall result set not within each result set.
4.Column name of …

Merge and Merge join transformation in SSIS

MERGE TRANSFORMATION
Using Merge Transformation we can combine two sorted data-set into single data-set basically Merge Transformation used to combines rows from two sorted data flows into one sorted data flow. Following tasks you may perform using Merge Transformation: 1.Suppose we have a scenario like, we need to merge data from a database table and excel means we want to merge data from two different data sources. For such type of scenario, you can use Merge Transformation. 2.If we want to merge data from two same structured tables but exists two different servers. 3.Sometimes we get an error due to data in a row, after correcting errors in the data we can re-merge rows easily. See below explanations may help you to understand Merge Transformation: I do evaluate here, you already know about the data source, data conversion, data flow, task flow, control flow etc. Note:Before Merge transformation, we need to sort the data using Sort Transformation. After sorting data add data path to Merge…