PostgreSQL - Getting Started with the Basics

1.1 What is PostgreSQL

PostgreSQL is a powerful open source object-relational database management system (ORDBMS). Used to store data securely; supports best practices and allows them to be retrieved when processing a request.

PostgreSQL (also known as Post-gress-QL) is developed by the PostgreSQL Global Development Group (a worldwide team of volunteers). It is not controlled by any company or other private entity. It is open source and its source code is freely available.

PostgreSQL is cross-platform and can run on many operating systems such as Linux, FreeBSD, OS X, Solaris, and Microsoft Windows, etc.

1.2 Features of PostgreSQL

  • PostgreSQL runs on all major operating systems (i.e. Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64) and Windows, etc.).
  • PostgreSQL supports text, images, sound, and video, and includes programming interfaces for C/C++, Java, Perl, Python, Ruby, Tcl, and Open Database Connectivity (ODBC).
  • PostgreSQL supports many features of SQL, such as complex SQL queries, SQL subselects, foreign keys, triggers, views, transactions, multi-process concurrency control (MVCC), streaming replication (9.0), hot standby (9.0)).
  • In PostgreSQL, tables can be set to inherit their characteristics from a "parent" table.
  • Several extensions can be installed to add additional functionality to PostgreSQL.

2. PostgreSQL Tools

2.1 psql :

It is a command-line tool and the main tool for administering PostgreSQL. pgAdmin is a free and open source GUI administration tool for PostgreSQL.

2.2 phpPgAdmin:

It is a web-based administration tool for PostgreSQL written in PHP. It is developed based on the phpMyAdmin tool to manage MySQL functions. It can be used as a front-end tool for PostgreSQL.

2.3 pgFouine

It is a log analyzer that can create reports from PostgreSQL log files

3. PostgreSQL data types

There are mainly three types of data types in PotgreSQL. In addition, users can also use the CREATE TYPE SQL command to create their own custom data types.

Following are the main three types of data types in PostgreSQL:

  • numeric data type
  • string data type
  • date/time data type

3.1 Numeric data types

Used to specify the array data in the table

name

describe

storage size

scope

smallint

store integers, small range

2 bytes

-32768 to +32767

integer

Store integers. Use this type to store typical integers

4 bytes

-2147483648 to +2147483647

bigint

Store integers, large range.

8 bytes

-9223372036854775808 to 9223372036854775807

decimal

user-specified precision, exact

variable

Up to 131072 digits before the decimal point; up to 16383 digits after the decimal point.

numeric

user-specified precision, exact

variable

Up to 131072 digits before the decimal point; up to 16383 digits after the decimal point.

real

variable precision, imprecise

4 bytes

6 digits of precision

double

variable precision, imprecise

8 bytes

15 digits of precision

serial

auto increment integer

4 bytes

1 to 2147483647

bigserial

large auto-increment integer

8 bytes

1 to 9223372036854775807

3.2 String data type

The String data type is used to represent string type values

type of data

describe

char(size)

Here size is the number of characters to store. A fixed-length string with spaces padded on the right to characters of equal size.

character(size)

Here size is the number of characters to store. Fixed-length string. Spaces on the right are padded to characters of equal size.

varchar(size)

Here size is the number of characters to store. Variable length string.

character varying(size)

Here size is the number of characters to store. Variable length string.

text

Variable length string.

3.3 Date/Time Data Types

The date/time data type is used to represent columns that use date and time values.

name

describe

storage size

minimum value

maximum value

resolution

timestamp [ (p) ] [without time zone]

date and time (no time zone)

8 bytes

4713 bc

294276 ad

1 microsecond/14 digits

timestamp [ (p) ] with time zone

Include date and time, with time zone

8 bytes

4713 bc

294276 ad

date

date (no time)

4 bytes

4713 bc

5874897 ad

1 microsecond/14 digits

time [ (p) ] [ without time zone ]

time (no date)

8 bytes

00:00:00

24:00:00

1 microsecond/14 digits

time [ (p) ] with time zone

time only, with time zone

12 bytes

00:00:00+1459

24:00:00-1459

1 microsecond/14 digits

interval [ fields ] [ (p) ]

time interval

12 bytes

-178000000 years

178000000

1 microsecond/14 digits

Boolean type:

name

describe

storage size

boolean

It specifies the state of true or false.

1 byte

4. Basic sql statement

Create a database:

CREATE DATABASE database_name;

Delete the database:

drop database testdb;

Create table:

CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ) );

drop table:

drop table student2;

Create schema (schema):

A schema (also called a schema) is a specified collection of tables. It can also contain views, indexes, sequences, data types, operators and functions.

CREATE SCHEMA schema_name;

Insert data:

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN);

data query:

SELECT "column1", "column2".."column" FROM "table_name";

update data:

UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];

delete data:

DELETE FROM table_name WHERE [condition];

Order by statement:

SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC];

Group By:

SELECT column-list FROM table_name WHERE [conditions ] GROUP BY column1, column2....columnN ORDER BY column1, column2....columnN

Having statement:

SELECT column1, column2 FROM table1, table2 WHERE [ conditions ] GROUP BY column1, column2 HAVING [ conditions ] ORDER BY column1, column2

PostgreSQL conditions are used to get more specific results from the database. They are usually used with WHERE clause. Conditions with clauses are like double-layer filters.

Following is the list of PostgreSQL conditions:

  • AND condition
  • OR condition
  • AND & OR conditions
  • NOT condition
  • LIKE condition
  • IN condition
  • NOT IN condition
  • BETWEEN condition

AND condition:

SELECT column1, column2, ..... columnN FROM table_name WHERE [search_condition] AND [search_condition];

OR condition:

SELECT column1, column2, ..... columnN FROM table_name WHERE [search_condition] OR [search_condition];

Combine AND and OR:

SELECT column1, column2, ..... columnN FROM table_name WHERE [search_condition] AND [search_condition] OR [search_condition];

NOT conditions:

NOT condition is used with WHERE clause to negate the condition in the query

SELECT column1, column2, ..... columnN FROM table_name WHERE [search_condition] NOT [condition];

Like conditions:

SELECT column1, column2, ..... columnN FROM table_name WHERE [search_condition] LIKE [condition];

IN conditions:

SELECT column1, column2, ..... columnN FROM table_name WHERE [search_condition] IN [condition];

NOT IN condition:

SELECT column1, column2, ..... columnN FROM table_name WHERE [search_condition] NOT IN [condition];

Between conditions:

SELECT column1, column2, ..... columnN FROM table_name WHERE [search_condition] BETWEEN [condition];

In PostgreSQL, there are the following types of connections:

  • Inner join (INNER JOIN)
  • LEFT OUTER JOIN
  • RIGHT OUTER JOIN
  • Full connection (FULL OUTER JOIN)
  • Cross connection (CROSS JOIN)

inner join

PostgreSQL internal joins are also known as joins or simple joins. This is the most common connection type. This join returns all rows from multiple tables that satisfy the join condition.

As shown below −

0

SELECT table1.columns, table2.columns FROM table1 INNER JOIN table2 ON table1.common_filed = table2.common_field;

left outer join

A left outer join returns all rows from the left table specified in the "ON" condition, and returns only rows from the other table that satisfy the condition.

As shown in the figure below:

0

SELECT table1.columns, table2.columns FROM table1 LEFT OUTER JOIN table2 ON table1.common_filed = table2.common_field;

right outer join

A right outer join returns all rows from the right table specified in the "ON" condition, and returns only rows from the other table that satisfy the condition.

As shown in the figure below:

As shown in the figure below (blue part) -

0

SELECT table1.columns, table2.columns FROM table1 RIGHT OUTER JOIN table2 ON table1.common_filed = table2.common_field;

full outer join

A full outer join returns all rows from the left table and the left table. It puts NULL where the join condition is not satisfied.

As shown in the figure below (blue part) -

0

SELECT table1.columns, table2.columns FROM table1 FULL OUTER JOIN table2 ON table1.common_filed = table2.common_field;

cross connection

ostgreSQL CROSS JOIN matches each row of the first table with each row of the second table. It is also known as the Cartesian product. If table1 has "x" rows and table2 has "y" rows, the resulting table will have (x * y) rows.

SELECT coloums FROM table1 CROSS JOIN table2

Guess you like

Origin blog.csdn.net/m0_56324585/article/details/131728016