postgresql
  1. postgresql

PostgreSQL - (PostgreSQL Tutorial)

PostgreSQL is a powerful, open-source relational database management system (RDBMS) known for its stability, reliability, and scalability. It offers a unique set of features and capabilities that make it the preferred database for many applications. In this tutorial, we'll guide you through the basics of PostgreSQL.

Syntax

-- CREATE a table
CREATE TABLE table_name (
   column1 datatype1,
   column2 datatype2,
   .....
   columnN datatypeN
);

-- INSERT a record
INSERT INTO table_name (column1, column2, column3, ......columnN) 
VALUES (value1, value2, value3, ....., valueN);

-- SELECT statement
SELECT column1, column2, columnN 
FROM table_name;

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

-- DELETE statement
DELETE FROM table_name WHERE [condition];

Example

Let's take the example of creating a table, inserting a record, selecting records from the table, updating the record, and deleting the record.

-- CREATE a table
CREATE TABLE employees (
   emp_id   SERIAL PRIMARY KEY,
   emp_name VARCHAR(50) NOT NULL,
   emp_age  INT  NOT NULL,
   emp_address VARCHAR(255) NOT NULL,
   emp_salary  REAL  NOT NULL
);

-- INSERT a record
INSERT INTO employees (emp_name, emp_age, emp_address, emp_salary) 
VALUES ('John Doe', 30, '123 Main St., Anytown USA', 50000);

-- SELECT statement
SELECT * 
FROM employees;

-- UPDATE statement
UPDATE employees 
SET emp_salary = 55000 
WHERE emp_id = 1;

-- DELETE statement
DELETE FROM employees 
WHERE emp_id = 1;

Explanation

In the above example, we created a table called employees, which had four columns: emp_id, emp_name, emp_age, emp_address, and emp_salary. We then inserted a record into the table with the INSERT INTO statement. We used the SELECT statement to select all of the records from the employees table. We updated the salary of the first employee in the table using the UPDATE statement. Finally, we deleted the first record from the table using the DELETE statement.

Use

PostgreSQL is widely used as a backend database for various types of applications, including web applications, mobile applications, and enterprise-level software systems. It is known for its robustness, scalability, and stability.

Important Points

  • PostgreSQL supports many advanced features, including full-text search, partitioning, and JSON support.
  • It is compliant with SQL standards and supports ACID (Atomicity, Consistency, Isolation, Durability) properties.
  • PostgreSQL supports a wide range of programming languages, including C/C++, Java, Python, and Ruby.
  • PostgreSQL is open source and free to use, with no licensing fees.

Summary

In this tutorial, we introduced PostgreSQL, a powerful and reliable relational database management system. We provided basic syntax for creating a table, inserting data, selecting data, updating data, and deleting data. We also discussed the use cases and important features of PostgreSQL. With this knowledge, you can start exploring PostgreSQL and building powerful database-backed applications.

Published on:
PostgreSQL Views