Before getting started with PostgreSQL queries we should understand some basics of SQL-
What is SQL?
DataTypes in PostgreSQL
Common SQL queries Examples
What is SQL
SQL is short for Structured Query Language. SQL is used to perform all types of data operations in relational database management systems (RDBMS). SQL is a powerful language where we can perform a wide range of operations:
execute queries
fetch data
insert, update, and delete records in a database (DML operations)
create new objects in a database (DDL operations)
set permissions on tables, procedures, functions, and views(DCL)
DataTypes in PostgreSQL
The following data types are supported by PostgreSQL:
Boolean : A boolean/bool data type is stored in the database according to the following:
1, yes, y, t, true values are converted to true
0, no, false, f values are converted to false
Character : It has three character data types : CHAR(n), VARCHAR(n), and TEXT.
CHAR(n): is used for data(string) with a fixed-length of characters with padded spaces.
VARCHAR(n) : is the variable-length character string. Similar to CHAR(n), it can store “n” length data. But unlike CHAR(n) no padding is done
TEXT : is the variable-length character string. It can store data with unlimited length.
Numeric: It has 2 types of numbers namely, integers and floating-point numbers.
Integer:
Small Integer(SMALLINT) has size 2 bytes.
Integer (INT) has size of 4 bytes.
Serial(SERIAL) works similar to the integers except these are automatically generated in the columns by PostgreSQL
Floating-point number:
float(n) is used for floating-point numbers with n precision and can have a maximum of 8-bytes.
float8 or real is used to represent 4-byte floating-point numbers.
A real number N(d,p) meaning with d number of digits and p number of decimal points after, are part of numeric or numeric(d, p). These are generally very precise.
4. Temporal data type:This data type is used to store date-time data. PostgreSQL has 5 temporal data type:
DATE : stores the dates only.
TIME : stores the time of day values
TIMESTAMP : stores both date and time values.
TIMESTAMPTZ: stores a timezone-aware timestamp data type.
INTERVAL :stores periods of time.
5. Array :an array column can be used to store an array of strings or an array of integers etc
6. JSON supports 2 types of JSON types namely JSON and JSONB(Binary JSON).
The JSON data type is used to store plain JSON data that get parsed every time it’s called by a query.
JSONB data type is used to store JSON data in a binary format. It is one hand makes querying data faster whereas slows down the data insertion process as it supports indexing of table data.
7.UUID: The UUID data type allows you to store Universal Unique Identifiers
8.hstore [ stores key-value pair]
9.Special :supports some special data types that are related to network or geometric.
Common SQL queries Examples :Some examples of common and useful PostgreSQL queries:
1. CREATE TABLE query : will create a new, empty table in the database.
2. Insert query : The INSERT command is used to insert data into a table:
3. SELECT querywithout WHERE condition:
4. UPDATE query: is used to update the data or row(s) of a database table.
Here we are changing age of the student whose name is John:
5. DELETE query: is used to delete row(s) of a database table. This command can be used with/without Where condition, but if Where condition is missing, this command will delete all rows.
Here we are deleting one row whose age is 28:
6.COUNT query: Is used to count number of rows in a table
I hope this blog was insightful and that you will get inspired by these examples.
Comments